nfs4recover.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559
  1. /*
  2. * Copyright (c) 2004 The Regents of the University of Michigan.
  3. * Copyright (c) 2012 Jeff Layton <jlayton@redhat.com>
  4. * All rights reserved.
  5. *
  6. * Andy Adamson <andros@citi.umich.edu>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  28. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. */
  34. #include <crypto/hash.h>
  35. #include <linux/file.h>
  36. #include <linux/slab.h>
  37. #include <linux/namei.h>
  38. #include <linux/sched.h>
  39. #include <linux/fs.h>
  40. #include <linux/module.h>
  41. #include <net/net_namespace.h>
  42. #include <linux/sunrpc/rpc_pipe_fs.h>
  43. #include <linux/sunrpc/clnt.h>
  44. #include <linux/nfsd/cld.h>
  45. #include "nfsd.h"
  46. #include "state.h"
  47. #include "vfs.h"
  48. #include "netns.h"
  49. #define NFSDDBG_FACILITY NFSDDBG_PROC
  50. /* Declarations */
  51. struct nfsd4_client_tracking_ops {
  52. int (*init)(struct net *);
  53. void (*exit)(struct net *);
  54. void (*create)(struct nfs4_client *);
  55. void (*remove)(struct nfs4_client *);
  56. int (*check)(struct nfs4_client *);
  57. void (*grace_done)(struct nfsd_net *);
  58. };
  59. /* Globals */
  60. static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
  61. static int
  62. nfs4_save_creds(const struct cred **original_creds)
  63. {
  64. struct cred *new;
  65. new = prepare_creds();
  66. if (!new)
  67. return -ENOMEM;
  68. new->fsuid = GLOBAL_ROOT_UID;
  69. new->fsgid = GLOBAL_ROOT_GID;
  70. *original_creds = override_creds(new);
  71. put_cred(new);
  72. return 0;
  73. }
  74. static void
  75. nfs4_reset_creds(const struct cred *original)
  76. {
  77. revert_creds(original);
  78. }
  79. static void
  80. md5_to_hex(char *out, char *md5)
  81. {
  82. int i;
  83. for (i=0; i<16; i++) {
  84. unsigned char c = md5[i];
  85. *out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
  86. *out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
  87. }
  88. *out = '\0';
  89. }
  90. static int
  91. nfs4_make_rec_clidname(char *dname, const struct xdr_netobj *clname)
  92. {
  93. struct xdr_netobj cksum;
  94. struct crypto_shash *tfm;
  95. int status;
  96. dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
  97. clname->len, clname->data);
  98. tfm = crypto_alloc_shash("md5", 0, 0);
  99. if (IS_ERR(tfm)) {
  100. status = PTR_ERR(tfm);
  101. goto out_no_tfm;
  102. }
  103. cksum.len = crypto_shash_digestsize(tfm);
  104. cksum.data = kmalloc(cksum.len, GFP_KERNEL);
  105. if (cksum.data == NULL) {
  106. status = -ENOMEM;
  107. goto out;
  108. }
  109. {
  110. SHASH_DESC_ON_STACK(desc, tfm);
  111. desc->tfm = tfm;
  112. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  113. status = crypto_shash_digest(desc, clname->data, clname->len,
  114. cksum.data);
  115. shash_desc_zero(desc);
  116. }
  117. if (status)
  118. goto out;
  119. md5_to_hex(dname, cksum.data);
  120. status = 0;
  121. out:
  122. kfree(cksum.data);
  123. crypto_free_shash(tfm);
  124. out_no_tfm:
  125. return status;
  126. }
  127. /*
  128. * If we had an error generating the recdir name for the legacy tracker
  129. * then warn the admin. If the error doesn't appear to be transient,
  130. * then disable recovery tracking.
  131. */
  132. static void
  133. legacy_recdir_name_error(struct nfs4_client *clp, int error)
  134. {
  135. printk(KERN_ERR "NFSD: unable to generate recoverydir "
  136. "name (%d).\n", error);
  137. /*
  138. * if the algorithm just doesn't exist, then disable the recovery
  139. * tracker altogether. The crypto libs will generally return this if
  140. * FIPS is enabled as well.
  141. */
  142. if (error == -ENOENT) {
  143. printk(KERN_ERR "NFSD: disabling legacy clientid tracking. "
  144. "Reboot recovery will not function correctly!\n");
  145. nfsd4_client_tracking_exit(clp->net);
  146. }
  147. }
  148. static void
  149. nfsd4_create_clid_dir(struct nfs4_client *clp)
  150. {
  151. const struct cred *original_cred;
  152. char dname[HEXDIR_LEN];
  153. struct dentry *dir, *dentry;
  154. struct nfs4_client_reclaim *crp;
  155. int status;
  156. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  157. if (test_and_set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  158. return;
  159. if (!nn->rec_file)
  160. return;
  161. status = nfs4_make_rec_clidname(dname, &clp->cl_name);
  162. if (status)
  163. return legacy_recdir_name_error(clp, status);
  164. status = nfs4_save_creds(&original_cred);
  165. if (status < 0)
  166. return;
  167. status = mnt_want_write_file(nn->rec_file);
  168. if (status)
  169. goto out_creds;
  170. dir = nn->rec_file->f_path.dentry;
  171. /* lock the parent */
  172. inode_lock(d_inode(dir));
  173. dentry = lookup_one_len(dname, dir, HEXDIR_LEN-1);
  174. if (IS_ERR(dentry)) {
  175. status = PTR_ERR(dentry);
  176. goto out_unlock;
  177. }
  178. if (d_really_is_positive(dentry))
  179. /*
  180. * In the 4.1 case, where we're called from
  181. * reclaim_complete(), records from the previous reboot
  182. * may still be left, so this is OK.
  183. *
  184. * In the 4.0 case, we should never get here; but we may
  185. * as well be forgiving and just succeed silently.
  186. */
  187. goto out_put;
  188. status = vfs_mkdir(d_inode(dir), dentry, S_IRWXU);
  189. out_put:
  190. dput(dentry);
  191. out_unlock:
  192. inode_unlock(d_inode(dir));
  193. if (status == 0) {
  194. if (nn->in_grace) {
  195. crp = nfs4_client_to_reclaim(dname, nn);
  196. if (crp)
  197. crp->cr_clp = clp;
  198. }
  199. vfs_fsync(nn->rec_file, 0);
  200. } else {
  201. printk(KERN_ERR "NFSD: failed to write recovery record"
  202. " (err %d); please check that %s exists"
  203. " and is writeable", status,
  204. user_recovery_dirname);
  205. }
  206. mnt_drop_write_file(nn->rec_file);
  207. out_creds:
  208. nfs4_reset_creds(original_cred);
  209. }
  210. typedef int (recdir_func)(struct dentry *, struct dentry *, struct nfsd_net *);
  211. struct name_list {
  212. char name[HEXDIR_LEN];
  213. struct list_head list;
  214. };
  215. struct nfs4_dir_ctx {
  216. struct dir_context ctx;
  217. struct list_head names;
  218. };
  219. static int
  220. nfsd4_build_namelist(struct dir_context *__ctx, const char *name, int namlen,
  221. loff_t offset, u64 ino, unsigned int d_type)
  222. {
  223. struct nfs4_dir_ctx *ctx =
  224. container_of(__ctx, struct nfs4_dir_ctx, ctx);
  225. struct name_list *entry;
  226. if (namlen != HEXDIR_LEN - 1)
  227. return 0;
  228. entry = kmalloc(sizeof(struct name_list), GFP_KERNEL);
  229. if (entry == NULL)
  230. return -ENOMEM;
  231. memcpy(entry->name, name, HEXDIR_LEN - 1);
  232. entry->name[HEXDIR_LEN - 1] = '\0';
  233. list_add(&entry->list, &ctx->names);
  234. return 0;
  235. }
  236. static int
  237. nfsd4_list_rec_dir(recdir_func *f, struct nfsd_net *nn)
  238. {
  239. const struct cred *original_cred;
  240. struct dentry *dir = nn->rec_file->f_path.dentry;
  241. struct nfs4_dir_ctx ctx = {
  242. .ctx.actor = nfsd4_build_namelist,
  243. .names = LIST_HEAD_INIT(ctx.names)
  244. };
  245. struct name_list *entry, *tmp;
  246. int status;
  247. status = nfs4_save_creds(&original_cred);
  248. if (status < 0)
  249. return status;
  250. status = vfs_llseek(nn->rec_file, 0, SEEK_SET);
  251. if (status < 0) {
  252. nfs4_reset_creds(original_cred);
  253. return status;
  254. }
  255. status = iterate_dir(nn->rec_file, &ctx.ctx);
  256. inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
  257. list_for_each_entry_safe(entry, tmp, &ctx.names, list) {
  258. if (!status) {
  259. struct dentry *dentry;
  260. dentry = lookup_one_len(entry->name, dir, HEXDIR_LEN-1);
  261. if (IS_ERR(dentry)) {
  262. status = PTR_ERR(dentry);
  263. break;
  264. }
  265. status = f(dir, dentry, nn);
  266. dput(dentry);
  267. }
  268. list_del(&entry->list);
  269. kfree(entry);
  270. }
  271. inode_unlock(d_inode(dir));
  272. nfs4_reset_creds(original_cred);
  273. list_for_each_entry_safe(entry, tmp, &ctx.names, list) {
  274. dprintk("NFSD: %s. Left entry %s\n", __func__, entry->name);
  275. list_del(&entry->list);
  276. kfree(entry);
  277. }
  278. return status;
  279. }
  280. static int
  281. nfsd4_unlink_clid_dir(char *name, int namlen, struct nfsd_net *nn)
  282. {
  283. struct dentry *dir, *dentry;
  284. int status;
  285. dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name);
  286. dir = nn->rec_file->f_path.dentry;
  287. inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
  288. dentry = lookup_one_len(name, dir, namlen);
  289. if (IS_ERR(dentry)) {
  290. status = PTR_ERR(dentry);
  291. goto out_unlock;
  292. }
  293. status = -ENOENT;
  294. if (d_really_is_negative(dentry))
  295. goto out;
  296. status = vfs_rmdir(d_inode(dir), dentry);
  297. out:
  298. dput(dentry);
  299. out_unlock:
  300. inode_unlock(d_inode(dir));
  301. return status;
  302. }
  303. static void
  304. nfsd4_remove_clid_dir(struct nfs4_client *clp)
  305. {
  306. const struct cred *original_cred;
  307. struct nfs4_client_reclaim *crp;
  308. char dname[HEXDIR_LEN];
  309. int status;
  310. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  311. if (!nn->rec_file || !test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  312. return;
  313. status = nfs4_make_rec_clidname(dname, &clp->cl_name);
  314. if (status)
  315. return legacy_recdir_name_error(clp, status);
  316. status = mnt_want_write_file(nn->rec_file);
  317. if (status)
  318. goto out;
  319. clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  320. status = nfs4_save_creds(&original_cred);
  321. if (status < 0)
  322. goto out_drop_write;
  323. status = nfsd4_unlink_clid_dir(dname, HEXDIR_LEN-1, nn);
  324. nfs4_reset_creds(original_cred);
  325. if (status == 0) {
  326. vfs_fsync(nn->rec_file, 0);
  327. if (nn->in_grace) {
  328. /* remove reclaim record */
  329. crp = nfsd4_find_reclaim_client(dname, nn);
  330. if (crp)
  331. nfs4_remove_reclaim_record(crp, nn);
  332. }
  333. }
  334. out_drop_write:
  335. mnt_drop_write_file(nn->rec_file);
  336. out:
  337. if (status)
  338. printk("NFSD: Failed to remove expired client state directory"
  339. " %.*s\n", HEXDIR_LEN, dname);
  340. }
  341. static int
  342. purge_old(struct dentry *parent, struct dentry *child, struct nfsd_net *nn)
  343. {
  344. int status;
  345. if (nfs4_has_reclaimed_state(child->d_name.name, nn))
  346. return 0;
  347. status = vfs_rmdir(d_inode(parent), child);
  348. if (status)
  349. printk("failed to remove client recovery directory %pd\n",
  350. child);
  351. /* Keep trying, success or failure: */
  352. return 0;
  353. }
  354. static void
  355. nfsd4_recdir_purge_old(struct nfsd_net *nn)
  356. {
  357. int status;
  358. nn->in_grace = false;
  359. if (!nn->rec_file)
  360. return;
  361. status = mnt_want_write_file(nn->rec_file);
  362. if (status)
  363. goto out;
  364. status = nfsd4_list_rec_dir(purge_old, nn);
  365. if (status == 0)
  366. vfs_fsync(nn->rec_file, 0);
  367. mnt_drop_write_file(nn->rec_file);
  368. out:
  369. nfs4_release_reclaim(nn);
  370. if (status)
  371. printk("nfsd4: failed to purge old clients from recovery"
  372. " directory %pD\n", nn->rec_file);
  373. }
  374. static int
  375. load_recdir(struct dentry *parent, struct dentry *child, struct nfsd_net *nn)
  376. {
  377. if (child->d_name.len != HEXDIR_LEN - 1) {
  378. printk("nfsd4: illegal name %pd in recovery directory\n",
  379. child);
  380. /* Keep trying; maybe the others are OK: */
  381. return 0;
  382. }
  383. nfs4_client_to_reclaim(child->d_name.name, nn);
  384. return 0;
  385. }
  386. static int
  387. nfsd4_recdir_load(struct net *net) {
  388. int status;
  389. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  390. if (!nn->rec_file)
  391. return 0;
  392. status = nfsd4_list_rec_dir(load_recdir, nn);
  393. if (status)
  394. printk("nfsd4: failed loading clients from recovery"
  395. " directory %pD\n", nn->rec_file);
  396. return status;
  397. }
  398. /*
  399. * Hold reference to the recovery directory.
  400. */
  401. static int
  402. nfsd4_init_recdir(struct net *net)
  403. {
  404. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  405. const struct cred *original_cred;
  406. int status;
  407. printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
  408. user_recovery_dirname);
  409. BUG_ON(nn->rec_file);
  410. status = nfs4_save_creds(&original_cred);
  411. if (status < 0) {
  412. printk("NFSD: Unable to change credentials to find recovery"
  413. " directory: error %d\n",
  414. status);
  415. return status;
  416. }
  417. nn->rec_file = filp_open(user_recovery_dirname, O_RDONLY | O_DIRECTORY, 0);
  418. if (IS_ERR(nn->rec_file)) {
  419. printk("NFSD: unable to find recovery directory %s\n",
  420. user_recovery_dirname);
  421. status = PTR_ERR(nn->rec_file);
  422. nn->rec_file = NULL;
  423. }
  424. nfs4_reset_creds(original_cred);
  425. if (!status)
  426. nn->in_grace = true;
  427. return status;
  428. }
  429. static void
  430. nfsd4_shutdown_recdir(struct net *net)
  431. {
  432. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  433. if (!nn->rec_file)
  434. return;
  435. fput(nn->rec_file);
  436. nn->rec_file = NULL;
  437. }
  438. static int
  439. nfs4_legacy_state_init(struct net *net)
  440. {
  441. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  442. int i;
  443. nn->reclaim_str_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
  444. sizeof(struct list_head),
  445. GFP_KERNEL);
  446. if (!nn->reclaim_str_hashtbl)
  447. return -ENOMEM;
  448. for (i = 0; i < CLIENT_HASH_SIZE; i++)
  449. INIT_LIST_HEAD(&nn->reclaim_str_hashtbl[i]);
  450. nn->reclaim_str_hashtbl_size = 0;
  451. return 0;
  452. }
  453. static void
  454. nfs4_legacy_state_shutdown(struct net *net)
  455. {
  456. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  457. kfree(nn->reclaim_str_hashtbl);
  458. }
  459. static int
  460. nfsd4_load_reboot_recovery_data(struct net *net)
  461. {
  462. int status;
  463. status = nfsd4_init_recdir(net);
  464. if (status)
  465. return status;
  466. status = nfsd4_recdir_load(net);
  467. if (status)
  468. nfsd4_shutdown_recdir(net);
  469. return status;
  470. }
  471. static int
  472. nfsd4_legacy_tracking_init(struct net *net)
  473. {
  474. int status;
  475. /* XXX: The legacy code won't work in a container */
  476. if (net != &init_net) {
  477. pr_warn("NFSD: attempt to initialize legacy client tracking in a container ignored.\n");
  478. return -EINVAL;
  479. }
  480. status = nfs4_legacy_state_init(net);
  481. if (status)
  482. return status;
  483. status = nfsd4_load_reboot_recovery_data(net);
  484. if (status)
  485. goto err;
  486. return 0;
  487. err:
  488. nfs4_legacy_state_shutdown(net);
  489. return status;
  490. }
  491. static void
  492. nfsd4_legacy_tracking_exit(struct net *net)
  493. {
  494. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  495. nfs4_release_reclaim(nn);
  496. nfsd4_shutdown_recdir(net);
  497. nfs4_legacy_state_shutdown(net);
  498. }
  499. /*
  500. * Change the NFSv4 recovery directory to recdir.
  501. */
  502. int
  503. nfs4_reset_recoverydir(char *recdir)
  504. {
  505. int status;
  506. struct path path;
  507. status = kern_path(recdir, LOOKUP_FOLLOW, &path);
  508. if (status)
  509. return status;
  510. status = -ENOTDIR;
  511. if (d_is_dir(path.dentry)) {
  512. strcpy(user_recovery_dirname, recdir);
  513. status = 0;
  514. }
  515. path_put(&path);
  516. return status;
  517. }
  518. char *
  519. nfs4_recoverydir(void)
  520. {
  521. return user_recovery_dirname;
  522. }
  523. static int
  524. nfsd4_check_legacy_client(struct nfs4_client *clp)
  525. {
  526. int status;
  527. char dname[HEXDIR_LEN];
  528. struct nfs4_client_reclaim *crp;
  529. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  530. /* did we already find that this client is stable? */
  531. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  532. return 0;
  533. status = nfs4_make_rec_clidname(dname, &clp->cl_name);
  534. if (status) {
  535. legacy_recdir_name_error(clp, status);
  536. return status;
  537. }
  538. /* look for it in the reclaim hashtable otherwise */
  539. crp = nfsd4_find_reclaim_client(dname, nn);
  540. if (crp) {
  541. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  542. crp->cr_clp = clp;
  543. return 0;
  544. }
  545. return -ENOENT;
  546. }
  547. static const struct nfsd4_client_tracking_ops nfsd4_legacy_tracking_ops = {
  548. .init = nfsd4_legacy_tracking_init,
  549. .exit = nfsd4_legacy_tracking_exit,
  550. .create = nfsd4_create_clid_dir,
  551. .remove = nfsd4_remove_clid_dir,
  552. .check = nfsd4_check_legacy_client,
  553. .grace_done = nfsd4_recdir_purge_old,
  554. };
  555. /* Globals */
  556. #define NFSD_PIPE_DIR "nfsd"
  557. #define NFSD_CLD_PIPE "cld"
  558. /* per-net-ns structure for holding cld upcall info */
  559. struct cld_net {
  560. struct rpc_pipe *cn_pipe;
  561. spinlock_t cn_lock;
  562. struct list_head cn_list;
  563. unsigned int cn_xid;
  564. };
  565. struct cld_upcall {
  566. struct list_head cu_list;
  567. struct cld_net *cu_net;
  568. struct completion cu_done;
  569. struct cld_msg cu_msg;
  570. };
  571. static int
  572. __cld_pipe_upcall(struct rpc_pipe *pipe, struct cld_msg *cmsg)
  573. {
  574. int ret;
  575. struct rpc_pipe_msg msg;
  576. struct cld_upcall *cup = container_of(cmsg, struct cld_upcall, cu_msg);
  577. memset(&msg, 0, sizeof(msg));
  578. msg.data = cmsg;
  579. msg.len = sizeof(*cmsg);
  580. ret = rpc_queue_upcall(pipe, &msg);
  581. if (ret < 0) {
  582. goto out;
  583. }
  584. wait_for_completion(&cup->cu_done);
  585. if (msg.errno < 0)
  586. ret = msg.errno;
  587. out:
  588. return ret;
  589. }
  590. static int
  591. cld_pipe_upcall(struct rpc_pipe *pipe, struct cld_msg *cmsg)
  592. {
  593. int ret;
  594. /*
  595. * -EAGAIN occurs when pipe is closed and reopened while there are
  596. * upcalls queued.
  597. */
  598. do {
  599. ret = __cld_pipe_upcall(pipe, cmsg);
  600. } while (ret == -EAGAIN);
  601. return ret;
  602. }
  603. static ssize_t
  604. cld_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
  605. {
  606. struct cld_upcall *tmp, *cup;
  607. struct cld_msg __user *cmsg = (struct cld_msg __user *)src;
  608. uint32_t xid;
  609. struct nfsd_net *nn = net_generic(file_inode(filp)->i_sb->s_fs_info,
  610. nfsd_net_id);
  611. struct cld_net *cn = nn->cld_net;
  612. if (mlen != sizeof(*cmsg)) {
  613. dprintk("%s: got %zu bytes, expected %zu\n", __func__, mlen,
  614. sizeof(*cmsg));
  615. return -EINVAL;
  616. }
  617. /* copy just the xid so we can try to find that */
  618. if (copy_from_user(&xid, &cmsg->cm_xid, sizeof(xid)) != 0) {
  619. dprintk("%s: error when copying xid from userspace", __func__);
  620. return -EFAULT;
  621. }
  622. /* walk the list and find corresponding xid */
  623. cup = NULL;
  624. spin_lock(&cn->cn_lock);
  625. list_for_each_entry(tmp, &cn->cn_list, cu_list) {
  626. if (get_unaligned(&tmp->cu_msg.cm_xid) == xid) {
  627. cup = tmp;
  628. list_del_init(&cup->cu_list);
  629. break;
  630. }
  631. }
  632. spin_unlock(&cn->cn_lock);
  633. /* couldn't find upcall? */
  634. if (!cup) {
  635. dprintk("%s: couldn't find upcall -- xid=%u\n", __func__, xid);
  636. return -EINVAL;
  637. }
  638. if (copy_from_user(&cup->cu_msg, src, mlen) != 0)
  639. return -EFAULT;
  640. complete(&cup->cu_done);
  641. return mlen;
  642. }
  643. static void
  644. cld_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  645. {
  646. struct cld_msg *cmsg = msg->data;
  647. struct cld_upcall *cup = container_of(cmsg, struct cld_upcall,
  648. cu_msg);
  649. /* errno >= 0 means we got a downcall */
  650. if (msg->errno >= 0)
  651. return;
  652. complete(&cup->cu_done);
  653. }
  654. static const struct rpc_pipe_ops cld_upcall_ops = {
  655. .upcall = rpc_pipe_generic_upcall,
  656. .downcall = cld_pipe_downcall,
  657. .destroy_msg = cld_pipe_destroy_msg,
  658. };
  659. static struct dentry *
  660. nfsd4_cld_register_sb(struct super_block *sb, struct rpc_pipe *pipe)
  661. {
  662. struct dentry *dir, *dentry;
  663. dir = rpc_d_lookup_sb(sb, NFSD_PIPE_DIR);
  664. if (dir == NULL)
  665. return ERR_PTR(-ENOENT);
  666. dentry = rpc_mkpipe_dentry(dir, NFSD_CLD_PIPE, NULL, pipe);
  667. dput(dir);
  668. return dentry;
  669. }
  670. static void
  671. nfsd4_cld_unregister_sb(struct rpc_pipe *pipe)
  672. {
  673. if (pipe->dentry)
  674. rpc_unlink(pipe->dentry);
  675. }
  676. static struct dentry *
  677. nfsd4_cld_register_net(struct net *net, struct rpc_pipe *pipe)
  678. {
  679. struct super_block *sb;
  680. struct dentry *dentry;
  681. sb = rpc_get_sb_net(net);
  682. if (!sb)
  683. return NULL;
  684. dentry = nfsd4_cld_register_sb(sb, pipe);
  685. rpc_put_sb_net(net);
  686. return dentry;
  687. }
  688. static void
  689. nfsd4_cld_unregister_net(struct net *net, struct rpc_pipe *pipe)
  690. {
  691. struct super_block *sb;
  692. sb = rpc_get_sb_net(net);
  693. if (sb) {
  694. nfsd4_cld_unregister_sb(pipe);
  695. rpc_put_sb_net(net);
  696. }
  697. }
  698. /* Initialize rpc_pipefs pipe for communication with client tracking daemon */
  699. static int
  700. nfsd4_init_cld_pipe(struct net *net)
  701. {
  702. int ret;
  703. struct dentry *dentry;
  704. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  705. struct cld_net *cn;
  706. if (nn->cld_net)
  707. return 0;
  708. cn = kzalloc(sizeof(*cn), GFP_KERNEL);
  709. if (!cn) {
  710. ret = -ENOMEM;
  711. goto err;
  712. }
  713. cn->cn_pipe = rpc_mkpipe_data(&cld_upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
  714. if (IS_ERR(cn->cn_pipe)) {
  715. ret = PTR_ERR(cn->cn_pipe);
  716. goto err;
  717. }
  718. spin_lock_init(&cn->cn_lock);
  719. INIT_LIST_HEAD(&cn->cn_list);
  720. dentry = nfsd4_cld_register_net(net, cn->cn_pipe);
  721. if (IS_ERR(dentry)) {
  722. ret = PTR_ERR(dentry);
  723. goto err_destroy_data;
  724. }
  725. cn->cn_pipe->dentry = dentry;
  726. nn->cld_net = cn;
  727. return 0;
  728. err_destroy_data:
  729. rpc_destroy_pipe_data(cn->cn_pipe);
  730. err:
  731. kfree(cn);
  732. printk(KERN_ERR "NFSD: unable to create nfsdcld upcall pipe (%d)\n",
  733. ret);
  734. return ret;
  735. }
  736. static void
  737. nfsd4_remove_cld_pipe(struct net *net)
  738. {
  739. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  740. struct cld_net *cn = nn->cld_net;
  741. nfsd4_cld_unregister_net(net, cn->cn_pipe);
  742. rpc_destroy_pipe_data(cn->cn_pipe);
  743. kfree(nn->cld_net);
  744. nn->cld_net = NULL;
  745. }
  746. static struct cld_upcall *
  747. alloc_cld_upcall(struct cld_net *cn)
  748. {
  749. struct cld_upcall *new, *tmp;
  750. new = kzalloc(sizeof(*new), GFP_KERNEL);
  751. if (!new)
  752. return new;
  753. /* FIXME: hard cap on number in flight? */
  754. restart_search:
  755. spin_lock(&cn->cn_lock);
  756. list_for_each_entry(tmp, &cn->cn_list, cu_list) {
  757. if (tmp->cu_msg.cm_xid == cn->cn_xid) {
  758. cn->cn_xid++;
  759. spin_unlock(&cn->cn_lock);
  760. goto restart_search;
  761. }
  762. }
  763. init_completion(&new->cu_done);
  764. new->cu_msg.cm_vers = CLD_UPCALL_VERSION;
  765. put_unaligned(cn->cn_xid++, &new->cu_msg.cm_xid);
  766. new->cu_net = cn;
  767. list_add(&new->cu_list, &cn->cn_list);
  768. spin_unlock(&cn->cn_lock);
  769. dprintk("%s: allocated xid %u\n", __func__, new->cu_msg.cm_xid);
  770. return new;
  771. }
  772. static void
  773. free_cld_upcall(struct cld_upcall *victim)
  774. {
  775. struct cld_net *cn = victim->cu_net;
  776. spin_lock(&cn->cn_lock);
  777. list_del(&victim->cu_list);
  778. spin_unlock(&cn->cn_lock);
  779. kfree(victim);
  780. }
  781. /* Ask daemon to create a new record */
  782. static void
  783. nfsd4_cld_create(struct nfs4_client *clp)
  784. {
  785. int ret;
  786. struct cld_upcall *cup;
  787. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  788. struct cld_net *cn = nn->cld_net;
  789. /* Don't upcall if it's already stored */
  790. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  791. return;
  792. cup = alloc_cld_upcall(cn);
  793. if (!cup) {
  794. ret = -ENOMEM;
  795. goto out_err;
  796. }
  797. cup->cu_msg.cm_cmd = Cld_Create;
  798. cup->cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
  799. memcpy(cup->cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
  800. clp->cl_name.len);
  801. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
  802. if (!ret) {
  803. ret = cup->cu_msg.cm_status;
  804. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  805. }
  806. free_cld_upcall(cup);
  807. out_err:
  808. if (ret)
  809. printk(KERN_ERR "NFSD: Unable to create client "
  810. "record on stable storage: %d\n", ret);
  811. }
  812. /* Ask daemon to create a new record */
  813. static void
  814. nfsd4_cld_remove(struct nfs4_client *clp)
  815. {
  816. int ret;
  817. struct cld_upcall *cup;
  818. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  819. struct cld_net *cn = nn->cld_net;
  820. /* Don't upcall if it's already removed */
  821. if (!test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  822. return;
  823. cup = alloc_cld_upcall(cn);
  824. if (!cup) {
  825. ret = -ENOMEM;
  826. goto out_err;
  827. }
  828. cup->cu_msg.cm_cmd = Cld_Remove;
  829. cup->cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
  830. memcpy(cup->cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
  831. clp->cl_name.len);
  832. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
  833. if (!ret) {
  834. ret = cup->cu_msg.cm_status;
  835. clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  836. }
  837. free_cld_upcall(cup);
  838. out_err:
  839. if (ret)
  840. printk(KERN_ERR "NFSD: Unable to remove client "
  841. "record from stable storage: %d\n", ret);
  842. }
  843. /* Check for presence of a record, and update its timestamp */
  844. static int
  845. nfsd4_cld_check(struct nfs4_client *clp)
  846. {
  847. int ret;
  848. struct cld_upcall *cup;
  849. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  850. struct cld_net *cn = nn->cld_net;
  851. /* Don't upcall if one was already stored during this grace pd */
  852. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  853. return 0;
  854. cup = alloc_cld_upcall(cn);
  855. if (!cup) {
  856. printk(KERN_ERR "NFSD: Unable to check client record on "
  857. "stable storage: %d\n", -ENOMEM);
  858. return -ENOMEM;
  859. }
  860. cup->cu_msg.cm_cmd = Cld_Check;
  861. cup->cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
  862. memcpy(cup->cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
  863. clp->cl_name.len);
  864. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
  865. if (!ret) {
  866. ret = cup->cu_msg.cm_status;
  867. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  868. }
  869. free_cld_upcall(cup);
  870. return ret;
  871. }
  872. static void
  873. nfsd4_cld_grace_done(struct nfsd_net *nn)
  874. {
  875. int ret;
  876. struct cld_upcall *cup;
  877. struct cld_net *cn = nn->cld_net;
  878. cup = alloc_cld_upcall(cn);
  879. if (!cup) {
  880. ret = -ENOMEM;
  881. goto out_err;
  882. }
  883. cup->cu_msg.cm_cmd = Cld_GraceDone;
  884. cup->cu_msg.cm_u.cm_gracetime = (int64_t)nn->boot_time;
  885. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
  886. if (!ret)
  887. ret = cup->cu_msg.cm_status;
  888. free_cld_upcall(cup);
  889. out_err:
  890. if (ret)
  891. printk(KERN_ERR "NFSD: Unable to end grace period: %d\n", ret);
  892. }
  893. static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops = {
  894. .init = nfsd4_init_cld_pipe,
  895. .exit = nfsd4_remove_cld_pipe,
  896. .create = nfsd4_cld_create,
  897. .remove = nfsd4_cld_remove,
  898. .check = nfsd4_cld_check,
  899. .grace_done = nfsd4_cld_grace_done,
  900. };
  901. /* upcall via usermodehelper */
  902. static char cltrack_prog[PATH_MAX] = "/sbin/nfsdcltrack";
  903. module_param_string(cltrack_prog, cltrack_prog, sizeof(cltrack_prog),
  904. S_IRUGO|S_IWUSR);
  905. MODULE_PARM_DESC(cltrack_prog, "Path to the nfsdcltrack upcall program");
  906. static bool cltrack_legacy_disable;
  907. module_param(cltrack_legacy_disable, bool, S_IRUGO|S_IWUSR);
  908. MODULE_PARM_DESC(cltrack_legacy_disable,
  909. "Disable legacy recoverydir conversion. Default: false");
  910. #define LEGACY_TOPDIR_ENV_PREFIX "NFSDCLTRACK_LEGACY_TOPDIR="
  911. #define LEGACY_RECDIR_ENV_PREFIX "NFSDCLTRACK_LEGACY_RECDIR="
  912. #define HAS_SESSION_ENV_PREFIX "NFSDCLTRACK_CLIENT_HAS_SESSION="
  913. #define GRACE_START_ENV_PREFIX "NFSDCLTRACK_GRACE_START="
  914. static char *
  915. nfsd4_cltrack_legacy_topdir(void)
  916. {
  917. int copied;
  918. size_t len;
  919. char *result;
  920. if (cltrack_legacy_disable)
  921. return NULL;
  922. len = strlen(LEGACY_TOPDIR_ENV_PREFIX) +
  923. strlen(nfs4_recoverydir()) + 1;
  924. result = kmalloc(len, GFP_KERNEL);
  925. if (!result)
  926. return result;
  927. copied = snprintf(result, len, LEGACY_TOPDIR_ENV_PREFIX "%s",
  928. nfs4_recoverydir());
  929. if (copied >= len) {
  930. /* just return nothing if output was truncated */
  931. kfree(result);
  932. return NULL;
  933. }
  934. return result;
  935. }
  936. static char *
  937. nfsd4_cltrack_legacy_recdir(const struct xdr_netobj *name)
  938. {
  939. int copied;
  940. size_t len;
  941. char *result;
  942. if (cltrack_legacy_disable)
  943. return NULL;
  944. /* +1 is for '/' between "topdir" and "recdir" */
  945. len = strlen(LEGACY_RECDIR_ENV_PREFIX) +
  946. strlen(nfs4_recoverydir()) + 1 + HEXDIR_LEN;
  947. result = kmalloc(len, GFP_KERNEL);
  948. if (!result)
  949. return result;
  950. copied = snprintf(result, len, LEGACY_RECDIR_ENV_PREFIX "%s/",
  951. nfs4_recoverydir());
  952. if (copied > (len - HEXDIR_LEN)) {
  953. /* just return nothing if output will be truncated */
  954. kfree(result);
  955. return NULL;
  956. }
  957. copied = nfs4_make_rec_clidname(result + copied, name);
  958. if (copied) {
  959. kfree(result);
  960. return NULL;
  961. }
  962. return result;
  963. }
  964. static char *
  965. nfsd4_cltrack_client_has_session(struct nfs4_client *clp)
  966. {
  967. int copied;
  968. size_t len;
  969. char *result;
  970. /* prefix + Y/N character + terminating NULL */
  971. len = strlen(HAS_SESSION_ENV_PREFIX) + 1 + 1;
  972. result = kmalloc(len, GFP_KERNEL);
  973. if (!result)
  974. return result;
  975. copied = snprintf(result, len, HAS_SESSION_ENV_PREFIX "%c",
  976. clp->cl_minorversion ? 'Y' : 'N');
  977. if (copied >= len) {
  978. /* just return nothing if output was truncated */
  979. kfree(result);
  980. return NULL;
  981. }
  982. return result;
  983. }
  984. static char *
  985. nfsd4_cltrack_grace_start(time_t grace_start)
  986. {
  987. int copied;
  988. size_t len;
  989. char *result;
  990. /* prefix + max width of int64_t string + terminating NULL */
  991. len = strlen(GRACE_START_ENV_PREFIX) + 22 + 1;
  992. result = kmalloc(len, GFP_KERNEL);
  993. if (!result)
  994. return result;
  995. copied = snprintf(result, len, GRACE_START_ENV_PREFIX "%ld",
  996. grace_start);
  997. if (copied >= len) {
  998. /* just return nothing if output was truncated */
  999. kfree(result);
  1000. return NULL;
  1001. }
  1002. return result;
  1003. }
  1004. static int
  1005. nfsd4_umh_cltrack_upcall(char *cmd, char *arg, char *env0, char *env1)
  1006. {
  1007. char *envp[3];
  1008. char *argv[4];
  1009. int ret;
  1010. if (unlikely(!cltrack_prog[0])) {
  1011. dprintk("%s: cltrack_prog is disabled\n", __func__);
  1012. return -EACCES;
  1013. }
  1014. dprintk("%s: cmd: %s\n", __func__, cmd);
  1015. dprintk("%s: arg: %s\n", __func__, arg ? arg : "(null)");
  1016. dprintk("%s: env0: %s\n", __func__, env0 ? env0 : "(null)");
  1017. dprintk("%s: env1: %s\n", __func__, env1 ? env1 : "(null)");
  1018. envp[0] = env0;
  1019. envp[1] = env1;
  1020. envp[2] = NULL;
  1021. argv[0] = (char *)cltrack_prog;
  1022. argv[1] = cmd;
  1023. argv[2] = arg;
  1024. argv[3] = NULL;
  1025. ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
  1026. /*
  1027. * Disable the upcall mechanism if we're getting an ENOENT or EACCES
  1028. * error. The admin can re-enable it on the fly by using sysfs
  1029. * once the problem has been fixed.
  1030. */
  1031. if (ret == -ENOENT || ret == -EACCES) {
  1032. dprintk("NFSD: %s was not found or isn't executable (%d). "
  1033. "Setting cltrack_prog to blank string!",
  1034. cltrack_prog, ret);
  1035. cltrack_prog[0] = '\0';
  1036. }
  1037. dprintk("%s: %s return value: %d\n", __func__, cltrack_prog, ret);
  1038. return ret;
  1039. }
  1040. static char *
  1041. bin_to_hex_dup(const unsigned char *src, int srclen)
  1042. {
  1043. int i;
  1044. char *buf, *hex;
  1045. /* +1 for terminating NULL */
  1046. buf = kmalloc((srclen * 2) + 1, GFP_KERNEL);
  1047. if (!buf)
  1048. return buf;
  1049. hex = buf;
  1050. for (i = 0; i < srclen; i++) {
  1051. sprintf(hex, "%2.2x", *src++);
  1052. hex += 2;
  1053. }
  1054. return buf;
  1055. }
  1056. static int
  1057. nfsd4_umh_cltrack_init(struct net *net)
  1058. {
  1059. int ret;
  1060. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1061. char *grace_start = nfsd4_cltrack_grace_start(nn->boot_time);
  1062. /* XXX: The usermode helper s not working in container yet. */
  1063. if (net != &init_net) {
  1064. pr_warn("NFSD: attempt to initialize umh client tracking in a container ignored.\n");
  1065. kfree(grace_start);
  1066. return -EINVAL;
  1067. }
  1068. ret = nfsd4_umh_cltrack_upcall("init", NULL, grace_start, NULL);
  1069. kfree(grace_start);
  1070. return ret;
  1071. }
  1072. static void
  1073. nfsd4_cltrack_upcall_lock(struct nfs4_client *clp)
  1074. {
  1075. wait_on_bit_lock(&clp->cl_flags, NFSD4_CLIENT_UPCALL_LOCK,
  1076. TASK_UNINTERRUPTIBLE);
  1077. }
  1078. static void
  1079. nfsd4_cltrack_upcall_unlock(struct nfs4_client *clp)
  1080. {
  1081. smp_mb__before_atomic();
  1082. clear_bit(NFSD4_CLIENT_UPCALL_LOCK, &clp->cl_flags);
  1083. smp_mb__after_atomic();
  1084. wake_up_bit(&clp->cl_flags, NFSD4_CLIENT_UPCALL_LOCK);
  1085. }
  1086. static void
  1087. nfsd4_umh_cltrack_create(struct nfs4_client *clp)
  1088. {
  1089. char *hexid, *has_session, *grace_start;
  1090. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1091. /*
  1092. * With v4.0 clients, there's little difference in outcome between a
  1093. * create and check operation, and we can end up calling into this
  1094. * function multiple times per client (once for each openowner). So,
  1095. * for v4.0 clients skip upcalling once the client has been recorded
  1096. * on stable storage.
  1097. *
  1098. * For v4.1+ clients, the outcome of the two operations is different,
  1099. * so we must ensure that we upcall for the create operation. v4.1+
  1100. * clients call this on RECLAIM_COMPLETE though, so we should only end
  1101. * up doing a single create upcall per client.
  1102. */
  1103. if (clp->cl_minorversion == 0 &&
  1104. test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  1105. return;
  1106. hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
  1107. if (!hexid) {
  1108. dprintk("%s: can't allocate memory for upcall!\n", __func__);
  1109. return;
  1110. }
  1111. has_session = nfsd4_cltrack_client_has_session(clp);
  1112. grace_start = nfsd4_cltrack_grace_start(nn->boot_time);
  1113. nfsd4_cltrack_upcall_lock(clp);
  1114. if (!nfsd4_umh_cltrack_upcall("create", hexid, has_session, grace_start))
  1115. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  1116. nfsd4_cltrack_upcall_unlock(clp);
  1117. kfree(has_session);
  1118. kfree(grace_start);
  1119. kfree(hexid);
  1120. }
  1121. static void
  1122. nfsd4_umh_cltrack_remove(struct nfs4_client *clp)
  1123. {
  1124. char *hexid;
  1125. if (!test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  1126. return;
  1127. hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
  1128. if (!hexid) {
  1129. dprintk("%s: can't allocate memory for upcall!\n", __func__);
  1130. return;
  1131. }
  1132. nfsd4_cltrack_upcall_lock(clp);
  1133. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags) &&
  1134. nfsd4_umh_cltrack_upcall("remove", hexid, NULL, NULL) == 0)
  1135. clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  1136. nfsd4_cltrack_upcall_unlock(clp);
  1137. kfree(hexid);
  1138. }
  1139. static int
  1140. nfsd4_umh_cltrack_check(struct nfs4_client *clp)
  1141. {
  1142. int ret;
  1143. char *hexid, *has_session, *legacy;
  1144. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  1145. return 0;
  1146. hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
  1147. if (!hexid) {
  1148. dprintk("%s: can't allocate memory for upcall!\n", __func__);
  1149. return -ENOMEM;
  1150. }
  1151. has_session = nfsd4_cltrack_client_has_session(clp);
  1152. legacy = nfsd4_cltrack_legacy_recdir(&clp->cl_name);
  1153. nfsd4_cltrack_upcall_lock(clp);
  1154. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) {
  1155. ret = 0;
  1156. } else {
  1157. ret = nfsd4_umh_cltrack_upcall("check", hexid, has_session, legacy);
  1158. if (ret == 0)
  1159. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  1160. }
  1161. nfsd4_cltrack_upcall_unlock(clp);
  1162. kfree(has_session);
  1163. kfree(legacy);
  1164. kfree(hexid);
  1165. return ret;
  1166. }
  1167. static void
  1168. nfsd4_umh_cltrack_grace_done(struct nfsd_net *nn)
  1169. {
  1170. char *legacy;
  1171. char timestr[22]; /* FIXME: better way to determine max size? */
  1172. sprintf(timestr, "%ld", nn->boot_time);
  1173. legacy = nfsd4_cltrack_legacy_topdir();
  1174. nfsd4_umh_cltrack_upcall("gracedone", timestr, legacy, NULL);
  1175. kfree(legacy);
  1176. }
  1177. static const struct nfsd4_client_tracking_ops nfsd4_umh_tracking_ops = {
  1178. .init = nfsd4_umh_cltrack_init,
  1179. .exit = NULL,
  1180. .create = nfsd4_umh_cltrack_create,
  1181. .remove = nfsd4_umh_cltrack_remove,
  1182. .check = nfsd4_umh_cltrack_check,
  1183. .grace_done = nfsd4_umh_cltrack_grace_done,
  1184. };
  1185. int
  1186. nfsd4_client_tracking_init(struct net *net)
  1187. {
  1188. int status;
  1189. struct path path;
  1190. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1191. /* just run the init if it the method is already decided */
  1192. if (nn->client_tracking_ops)
  1193. goto do_init;
  1194. /*
  1195. * First, try a UMH upcall. It should succeed or fail quickly, so
  1196. * there's little harm in trying that first.
  1197. */
  1198. nn->client_tracking_ops = &nfsd4_umh_tracking_ops;
  1199. status = nn->client_tracking_ops->init(net);
  1200. if (!status)
  1201. return status;
  1202. /*
  1203. * See if the recoverydir exists and is a directory. If it is,
  1204. * then use the legacy ops.
  1205. */
  1206. nn->client_tracking_ops = &nfsd4_legacy_tracking_ops;
  1207. status = kern_path(nfs4_recoverydir(), LOOKUP_FOLLOW, &path);
  1208. if (!status) {
  1209. status = d_is_dir(path.dentry);
  1210. path_put(&path);
  1211. if (status)
  1212. goto do_init;
  1213. }
  1214. /* Finally, try to use nfsdcld */
  1215. nn->client_tracking_ops = &nfsd4_cld_tracking_ops;
  1216. printk(KERN_WARNING "NFSD: the nfsdcld client tracking upcall will be "
  1217. "removed in 3.10. Please transition to using "
  1218. "nfsdcltrack.\n");
  1219. do_init:
  1220. status = nn->client_tracking_ops->init(net);
  1221. if (status) {
  1222. printk(KERN_WARNING "NFSD: Unable to initialize client "
  1223. "recovery tracking! (%d)\n", status);
  1224. nn->client_tracking_ops = NULL;
  1225. }
  1226. return status;
  1227. }
  1228. void
  1229. nfsd4_client_tracking_exit(struct net *net)
  1230. {
  1231. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1232. if (nn->client_tracking_ops) {
  1233. if (nn->client_tracking_ops->exit)
  1234. nn->client_tracking_ops->exit(net);
  1235. nn->client_tracking_ops = NULL;
  1236. }
  1237. }
  1238. void
  1239. nfsd4_client_record_create(struct nfs4_client *clp)
  1240. {
  1241. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1242. if (nn->client_tracking_ops)
  1243. nn->client_tracking_ops->create(clp);
  1244. }
  1245. void
  1246. nfsd4_client_record_remove(struct nfs4_client *clp)
  1247. {
  1248. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1249. if (nn->client_tracking_ops)
  1250. nn->client_tracking_ops->remove(clp);
  1251. }
  1252. int
  1253. nfsd4_client_record_check(struct nfs4_client *clp)
  1254. {
  1255. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1256. if (nn->client_tracking_ops)
  1257. return nn->client_tracking_ops->check(clp);
  1258. return -EOPNOTSUPP;
  1259. }
  1260. void
  1261. nfsd4_record_grace_done(struct nfsd_net *nn)
  1262. {
  1263. if (nn->client_tracking_ops)
  1264. nn->client_tracking_ops->grace_done(nn);
  1265. }
  1266. static int
  1267. rpc_pipefs_event(struct notifier_block *nb, unsigned long event, void *ptr)
  1268. {
  1269. struct super_block *sb = ptr;
  1270. struct net *net = sb->s_fs_info;
  1271. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1272. struct cld_net *cn = nn->cld_net;
  1273. struct dentry *dentry;
  1274. int ret = 0;
  1275. if (!try_module_get(THIS_MODULE))
  1276. return 0;
  1277. if (!cn) {
  1278. module_put(THIS_MODULE);
  1279. return 0;
  1280. }
  1281. switch (event) {
  1282. case RPC_PIPEFS_MOUNT:
  1283. dentry = nfsd4_cld_register_sb(sb, cn->cn_pipe);
  1284. if (IS_ERR(dentry)) {
  1285. ret = PTR_ERR(dentry);
  1286. break;
  1287. }
  1288. cn->cn_pipe->dentry = dentry;
  1289. break;
  1290. case RPC_PIPEFS_UMOUNT:
  1291. if (cn->cn_pipe->dentry)
  1292. nfsd4_cld_unregister_sb(cn->cn_pipe);
  1293. break;
  1294. default:
  1295. ret = -ENOTSUPP;
  1296. break;
  1297. }
  1298. module_put(THIS_MODULE);
  1299. return ret;
  1300. }
  1301. static struct notifier_block nfsd4_cld_block = {
  1302. .notifier_call = rpc_pipefs_event,
  1303. };
  1304. int
  1305. register_cld_notifier(void)
  1306. {
  1307. return rpc_pipefs_notifier_register(&nfsd4_cld_block);
  1308. }
  1309. void
  1310. unregister_cld_notifier(void)
  1311. {
  1312. rpc_pipefs_notifier_unregister(&nfsd4_cld_block);
  1313. }