nfs4recover.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  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 <linux/file.h>
  35. #include <linux/slab.h>
  36. #include <linux/namei.h>
  37. #include <linux/crypto.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 *, time_t);
  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 hash_desc desc;
  95. struct scatterlist sg;
  96. int status;
  97. dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
  98. clname->len, clname->data);
  99. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  100. desc.tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
  101. if (IS_ERR(desc.tfm)) {
  102. status = PTR_ERR(desc.tfm);
  103. goto out_no_tfm;
  104. }
  105. cksum.len = crypto_hash_digestsize(desc.tfm);
  106. cksum.data = kmalloc(cksum.len, GFP_KERNEL);
  107. if (cksum.data == NULL) {
  108. status = -ENOMEM;
  109. goto out;
  110. }
  111. sg_init_one(&sg, clname->data, clname->len);
  112. status = crypto_hash_digest(&desc, &sg, sg.length, cksum.data);
  113. if (status)
  114. goto out;
  115. md5_to_hex(dname, cksum.data);
  116. status = 0;
  117. out:
  118. kfree(cksum.data);
  119. crypto_free_hash(desc.tfm);
  120. out_no_tfm:
  121. return status;
  122. }
  123. /*
  124. * If we had an error generating the recdir name for the legacy tracker
  125. * then warn the admin. If the error doesn't appear to be transient,
  126. * then disable recovery tracking.
  127. */
  128. static void
  129. legacy_recdir_name_error(struct nfs4_client *clp, int error)
  130. {
  131. printk(KERN_ERR "NFSD: unable to generate recoverydir "
  132. "name (%d).\n", error);
  133. /*
  134. * if the algorithm just doesn't exist, then disable the recovery
  135. * tracker altogether. The crypto libs will generally return this if
  136. * FIPS is enabled as well.
  137. */
  138. if (error == -ENOENT) {
  139. printk(KERN_ERR "NFSD: disabling legacy clientid tracking. "
  140. "Reboot recovery will not function correctly!\n");
  141. nfsd4_client_tracking_exit(clp->net);
  142. }
  143. }
  144. static void
  145. nfsd4_create_clid_dir(struct nfs4_client *clp)
  146. {
  147. const struct cred *original_cred;
  148. char dname[HEXDIR_LEN];
  149. struct dentry *dir, *dentry;
  150. struct nfs4_client_reclaim *crp;
  151. int status;
  152. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  153. dprintk("NFSD: nfsd4_create_clid_dir for \"%s\"\n", dname);
  154. if (test_and_set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  155. return;
  156. if (!nn->rec_file)
  157. return;
  158. status = nfs4_make_rec_clidname(dname, &clp->cl_name);
  159. if (status)
  160. return legacy_recdir_name_error(clp, status);
  161. status = nfs4_save_creds(&original_cred);
  162. if (status < 0)
  163. return;
  164. status = mnt_want_write_file(nn->rec_file);
  165. if (status)
  166. return;
  167. dir = nn->rec_file->f_path.dentry;
  168. /* lock the parent */
  169. mutex_lock(&dir->d_inode->i_mutex);
  170. dentry = lookup_one_len(dname, dir, HEXDIR_LEN-1);
  171. if (IS_ERR(dentry)) {
  172. status = PTR_ERR(dentry);
  173. goto out_unlock;
  174. }
  175. if (dentry->d_inode)
  176. /*
  177. * In the 4.1 case, where we're called from
  178. * reclaim_complete(), records from the previous reboot
  179. * may still be left, so this is OK.
  180. *
  181. * In the 4.0 case, we should never get here; but we may
  182. * as well be forgiving and just succeed silently.
  183. */
  184. goto out_put;
  185. status = vfs_mkdir(dir->d_inode, dentry, S_IRWXU);
  186. out_put:
  187. dput(dentry);
  188. out_unlock:
  189. mutex_unlock(&dir->d_inode->i_mutex);
  190. if (status == 0) {
  191. if (nn->in_grace) {
  192. crp = nfs4_client_to_reclaim(dname, nn);
  193. if (crp)
  194. crp->cr_clp = clp;
  195. }
  196. vfs_fsync(nn->rec_file, 0);
  197. } else {
  198. printk(KERN_ERR "NFSD: failed to write recovery record"
  199. " (err %d); please check that %s exists"
  200. " and is writeable", status,
  201. user_recovery_dirname);
  202. }
  203. mnt_drop_write_file(nn->rec_file);
  204. nfs4_reset_creds(original_cred);
  205. }
  206. typedef int (recdir_func)(struct dentry *, struct dentry *, struct nfsd_net *);
  207. struct name_list {
  208. char name[HEXDIR_LEN];
  209. struct list_head list;
  210. };
  211. static int
  212. nfsd4_build_namelist(void *arg, const char *name, int namlen,
  213. loff_t offset, u64 ino, unsigned int d_type)
  214. {
  215. struct list_head *names = arg;
  216. struct name_list *entry;
  217. if (namlen != HEXDIR_LEN - 1)
  218. return 0;
  219. entry = kmalloc(sizeof(struct name_list), GFP_KERNEL);
  220. if (entry == NULL)
  221. return -ENOMEM;
  222. memcpy(entry->name, name, HEXDIR_LEN - 1);
  223. entry->name[HEXDIR_LEN - 1] = '\0';
  224. list_add(&entry->list, names);
  225. return 0;
  226. }
  227. static int
  228. nfsd4_list_rec_dir(recdir_func *f, struct nfsd_net *nn)
  229. {
  230. const struct cred *original_cred;
  231. struct dentry *dir = nn->rec_file->f_path.dentry;
  232. struct {
  233. struct dir_context ctx;
  234. struct list_head names;
  235. } ctx;
  236. int status;
  237. status = nfs4_save_creds(&original_cred);
  238. if (status < 0)
  239. return status;
  240. status = vfs_llseek(nn->rec_file, 0, SEEK_SET);
  241. if (status < 0) {
  242. nfs4_reset_creds(original_cred);
  243. return status;
  244. }
  245. INIT_LIST_HEAD(&ctx.names);
  246. ctx.ctx.actor = nfsd4_build_namelist;
  247. status = iterate_dir(nn->rec_file, &ctx.ctx);
  248. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
  249. while (!list_empty(&ctx.names)) {
  250. struct name_list *entry;
  251. entry = list_entry(ctx.names.next, struct name_list, list);
  252. if (!status) {
  253. struct dentry *dentry;
  254. dentry = lookup_one_len(entry->name, dir, HEXDIR_LEN-1);
  255. if (IS_ERR(dentry)) {
  256. status = PTR_ERR(dentry);
  257. break;
  258. }
  259. status = f(dir, dentry, nn);
  260. dput(dentry);
  261. }
  262. list_del(&entry->list);
  263. kfree(entry);
  264. }
  265. mutex_unlock(&dir->d_inode->i_mutex);
  266. nfs4_reset_creds(original_cred);
  267. return status;
  268. }
  269. static int
  270. nfsd4_unlink_clid_dir(char *name, int namlen, struct nfsd_net *nn)
  271. {
  272. struct dentry *dir, *dentry;
  273. int status;
  274. dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name);
  275. dir = nn->rec_file->f_path.dentry;
  276. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
  277. dentry = lookup_one_len(name, dir, namlen);
  278. if (IS_ERR(dentry)) {
  279. status = PTR_ERR(dentry);
  280. goto out_unlock;
  281. }
  282. status = -ENOENT;
  283. if (!dentry->d_inode)
  284. goto out;
  285. status = vfs_rmdir(dir->d_inode, dentry);
  286. out:
  287. dput(dentry);
  288. out_unlock:
  289. mutex_unlock(&dir->d_inode->i_mutex);
  290. return status;
  291. }
  292. static void
  293. nfsd4_remove_clid_dir(struct nfs4_client *clp)
  294. {
  295. const struct cred *original_cred;
  296. struct nfs4_client_reclaim *crp;
  297. char dname[HEXDIR_LEN];
  298. int status;
  299. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  300. if (!nn->rec_file || !test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  301. return;
  302. status = nfs4_make_rec_clidname(dname, &clp->cl_name);
  303. if (status)
  304. return legacy_recdir_name_error(clp, status);
  305. status = mnt_want_write_file(nn->rec_file);
  306. if (status)
  307. goto out;
  308. clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  309. status = nfs4_save_creds(&original_cred);
  310. if (status < 0)
  311. goto out_drop_write;
  312. status = nfsd4_unlink_clid_dir(dname, HEXDIR_LEN-1, nn);
  313. nfs4_reset_creds(original_cred);
  314. if (status == 0) {
  315. vfs_fsync(nn->rec_file, 0);
  316. if (nn->in_grace) {
  317. /* remove reclaim record */
  318. crp = nfsd4_find_reclaim_client(dname, nn);
  319. if (crp)
  320. nfs4_remove_reclaim_record(crp, nn);
  321. }
  322. }
  323. out_drop_write:
  324. mnt_drop_write_file(nn->rec_file);
  325. out:
  326. if (status)
  327. printk("NFSD: Failed to remove expired client state directory"
  328. " %.*s\n", HEXDIR_LEN, dname);
  329. }
  330. static int
  331. purge_old(struct dentry *parent, struct dentry *child, struct nfsd_net *nn)
  332. {
  333. int status;
  334. if (nfs4_has_reclaimed_state(child->d_name.name, nn))
  335. return 0;
  336. status = vfs_rmdir(parent->d_inode, child);
  337. if (status)
  338. printk("failed to remove client recovery directory %s\n",
  339. child->d_name.name);
  340. /* Keep trying, success or failure: */
  341. return 0;
  342. }
  343. static void
  344. nfsd4_recdir_purge_old(struct nfsd_net *nn, time_t boot_time)
  345. {
  346. int status;
  347. nn->in_grace = false;
  348. if (!nn->rec_file)
  349. return;
  350. status = mnt_want_write_file(nn->rec_file);
  351. if (status)
  352. goto out;
  353. status = nfsd4_list_rec_dir(purge_old, nn);
  354. if (status == 0)
  355. vfs_fsync(nn->rec_file, 0);
  356. mnt_drop_write_file(nn->rec_file);
  357. out:
  358. nfs4_release_reclaim(nn);
  359. if (status)
  360. printk("nfsd4: failed to purge old clients from recovery"
  361. " directory %s\n", nn->rec_file->f_path.dentry->d_name.name);
  362. }
  363. static int
  364. load_recdir(struct dentry *parent, struct dentry *child, struct nfsd_net *nn)
  365. {
  366. if (child->d_name.len != HEXDIR_LEN - 1) {
  367. printk("nfsd4: illegal name %s in recovery directory\n",
  368. child->d_name.name);
  369. /* Keep trying; maybe the others are OK: */
  370. return 0;
  371. }
  372. nfs4_client_to_reclaim(child->d_name.name, nn);
  373. return 0;
  374. }
  375. static int
  376. nfsd4_recdir_load(struct net *net) {
  377. int status;
  378. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  379. if (!nn->rec_file)
  380. return 0;
  381. status = nfsd4_list_rec_dir(load_recdir, nn);
  382. if (status)
  383. printk("nfsd4: failed loading clients from recovery"
  384. " directory %s\n", nn->rec_file->f_path.dentry->d_name.name);
  385. return status;
  386. }
  387. /*
  388. * Hold reference to the recovery directory.
  389. */
  390. static int
  391. nfsd4_init_recdir(struct net *net)
  392. {
  393. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  394. const struct cred *original_cred;
  395. int status;
  396. printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
  397. user_recovery_dirname);
  398. BUG_ON(nn->rec_file);
  399. status = nfs4_save_creds(&original_cred);
  400. if (status < 0) {
  401. printk("NFSD: Unable to change credentials to find recovery"
  402. " directory: error %d\n",
  403. status);
  404. return status;
  405. }
  406. nn->rec_file = filp_open(user_recovery_dirname, O_RDONLY | O_DIRECTORY, 0);
  407. if (IS_ERR(nn->rec_file)) {
  408. printk("NFSD: unable to find recovery directory %s\n",
  409. user_recovery_dirname);
  410. status = PTR_ERR(nn->rec_file);
  411. nn->rec_file = NULL;
  412. }
  413. nfs4_reset_creds(original_cred);
  414. if (!status)
  415. nn->in_grace = true;
  416. return status;
  417. }
  418. static int
  419. nfs4_legacy_state_init(struct net *net)
  420. {
  421. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  422. int i;
  423. nn->reclaim_str_hashtbl = kmalloc(sizeof(struct list_head) *
  424. CLIENT_HASH_SIZE, GFP_KERNEL);
  425. if (!nn->reclaim_str_hashtbl)
  426. return -ENOMEM;
  427. for (i = 0; i < CLIENT_HASH_SIZE; i++)
  428. INIT_LIST_HEAD(&nn->reclaim_str_hashtbl[i]);
  429. nn->reclaim_str_hashtbl_size = 0;
  430. return 0;
  431. }
  432. static void
  433. nfs4_legacy_state_shutdown(struct net *net)
  434. {
  435. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  436. kfree(nn->reclaim_str_hashtbl);
  437. }
  438. static int
  439. nfsd4_load_reboot_recovery_data(struct net *net)
  440. {
  441. int status;
  442. status = nfsd4_init_recdir(net);
  443. if (!status)
  444. status = nfsd4_recdir_load(net);
  445. if (status)
  446. printk(KERN_ERR "NFSD: Failure reading reboot recovery data\n");
  447. return status;
  448. }
  449. static int
  450. nfsd4_legacy_tracking_init(struct net *net)
  451. {
  452. int status;
  453. /* XXX: The legacy code won't work in a container */
  454. if (net != &init_net) {
  455. WARN(1, KERN_ERR "NFSD: attempt to initialize legacy client "
  456. "tracking in a container!\n");
  457. return -EINVAL;
  458. }
  459. status = nfs4_legacy_state_init(net);
  460. if (status)
  461. return status;
  462. status = nfsd4_load_reboot_recovery_data(net);
  463. if (status)
  464. goto err;
  465. return 0;
  466. err:
  467. nfs4_legacy_state_shutdown(net);
  468. return status;
  469. }
  470. static void
  471. nfsd4_shutdown_recdir(struct nfsd_net *nn)
  472. {
  473. if (!nn->rec_file)
  474. return;
  475. fput(nn->rec_file);
  476. nn->rec_file = NULL;
  477. }
  478. static void
  479. nfsd4_legacy_tracking_exit(struct net *net)
  480. {
  481. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  482. nfs4_release_reclaim(nn);
  483. nfsd4_shutdown_recdir(nn);
  484. nfs4_legacy_state_shutdown(net);
  485. }
  486. /*
  487. * Change the NFSv4 recovery directory to recdir.
  488. */
  489. int
  490. nfs4_reset_recoverydir(char *recdir)
  491. {
  492. int status;
  493. struct path path;
  494. status = kern_path(recdir, LOOKUP_FOLLOW, &path);
  495. if (status)
  496. return status;
  497. status = -ENOTDIR;
  498. if (S_ISDIR(path.dentry->d_inode->i_mode)) {
  499. strcpy(user_recovery_dirname, recdir);
  500. status = 0;
  501. }
  502. path_put(&path);
  503. return status;
  504. }
  505. char *
  506. nfs4_recoverydir(void)
  507. {
  508. return user_recovery_dirname;
  509. }
  510. static int
  511. nfsd4_check_legacy_client(struct nfs4_client *clp)
  512. {
  513. int status;
  514. char dname[HEXDIR_LEN];
  515. struct nfs4_client_reclaim *crp;
  516. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  517. /* did we already find that this client is stable? */
  518. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  519. return 0;
  520. status = nfs4_make_rec_clidname(dname, &clp->cl_name);
  521. if (status) {
  522. legacy_recdir_name_error(clp, status);
  523. return status;
  524. }
  525. /* look for it in the reclaim hashtable otherwise */
  526. crp = nfsd4_find_reclaim_client(dname, nn);
  527. if (crp) {
  528. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  529. crp->cr_clp = clp;
  530. return 0;
  531. }
  532. return -ENOENT;
  533. }
  534. static struct nfsd4_client_tracking_ops nfsd4_legacy_tracking_ops = {
  535. .init = nfsd4_legacy_tracking_init,
  536. .exit = nfsd4_legacy_tracking_exit,
  537. .create = nfsd4_create_clid_dir,
  538. .remove = nfsd4_remove_clid_dir,
  539. .check = nfsd4_check_legacy_client,
  540. .grace_done = nfsd4_recdir_purge_old,
  541. };
  542. /* Globals */
  543. #define NFSD_PIPE_DIR "nfsd"
  544. #define NFSD_CLD_PIPE "cld"
  545. /* per-net-ns structure for holding cld upcall info */
  546. struct cld_net {
  547. struct rpc_pipe *cn_pipe;
  548. spinlock_t cn_lock;
  549. struct list_head cn_list;
  550. unsigned int cn_xid;
  551. };
  552. struct cld_upcall {
  553. struct list_head cu_list;
  554. struct cld_net *cu_net;
  555. struct task_struct *cu_task;
  556. struct cld_msg cu_msg;
  557. };
  558. static int
  559. __cld_pipe_upcall(struct rpc_pipe *pipe, struct cld_msg *cmsg)
  560. {
  561. int ret;
  562. struct rpc_pipe_msg msg;
  563. memset(&msg, 0, sizeof(msg));
  564. msg.data = cmsg;
  565. msg.len = sizeof(*cmsg);
  566. /*
  567. * Set task state before we queue the upcall. That prevents
  568. * wake_up_process in the downcall from racing with schedule.
  569. */
  570. set_current_state(TASK_UNINTERRUPTIBLE);
  571. ret = rpc_queue_upcall(pipe, &msg);
  572. if (ret < 0) {
  573. set_current_state(TASK_RUNNING);
  574. goto out;
  575. }
  576. schedule();
  577. set_current_state(TASK_RUNNING);
  578. if (msg.errno < 0)
  579. ret = msg.errno;
  580. out:
  581. return ret;
  582. }
  583. static int
  584. cld_pipe_upcall(struct rpc_pipe *pipe, struct cld_msg *cmsg)
  585. {
  586. int ret;
  587. /*
  588. * -EAGAIN occurs when pipe is closed and reopened while there are
  589. * upcalls queued.
  590. */
  591. do {
  592. ret = __cld_pipe_upcall(pipe, cmsg);
  593. } while (ret == -EAGAIN);
  594. return ret;
  595. }
  596. static ssize_t
  597. cld_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
  598. {
  599. struct cld_upcall *tmp, *cup;
  600. struct cld_msg __user *cmsg = (struct cld_msg __user *)src;
  601. uint32_t xid;
  602. struct nfsd_net *nn = net_generic(filp->f_dentry->d_sb->s_fs_info,
  603. nfsd_net_id);
  604. struct cld_net *cn = nn->cld_net;
  605. if (mlen != sizeof(*cmsg)) {
  606. dprintk("%s: got %zu bytes, expected %zu\n", __func__, mlen,
  607. sizeof(*cmsg));
  608. return -EINVAL;
  609. }
  610. /* copy just the xid so we can try to find that */
  611. if (copy_from_user(&xid, &cmsg->cm_xid, sizeof(xid)) != 0) {
  612. dprintk("%s: error when copying xid from userspace", __func__);
  613. return -EFAULT;
  614. }
  615. /* walk the list and find corresponding xid */
  616. cup = NULL;
  617. spin_lock(&cn->cn_lock);
  618. list_for_each_entry(tmp, &cn->cn_list, cu_list) {
  619. if (get_unaligned(&tmp->cu_msg.cm_xid) == xid) {
  620. cup = tmp;
  621. list_del_init(&cup->cu_list);
  622. break;
  623. }
  624. }
  625. spin_unlock(&cn->cn_lock);
  626. /* couldn't find upcall? */
  627. if (!cup) {
  628. dprintk("%s: couldn't find upcall -- xid=%u\n", __func__, xid);
  629. return -EINVAL;
  630. }
  631. if (copy_from_user(&cup->cu_msg, src, mlen) != 0)
  632. return -EFAULT;
  633. wake_up_process(cup->cu_task);
  634. return mlen;
  635. }
  636. static void
  637. cld_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  638. {
  639. struct cld_msg *cmsg = msg->data;
  640. struct cld_upcall *cup = container_of(cmsg, struct cld_upcall,
  641. cu_msg);
  642. /* errno >= 0 means we got a downcall */
  643. if (msg->errno >= 0)
  644. return;
  645. wake_up_process(cup->cu_task);
  646. }
  647. static const struct rpc_pipe_ops cld_upcall_ops = {
  648. .upcall = rpc_pipe_generic_upcall,
  649. .downcall = cld_pipe_downcall,
  650. .destroy_msg = cld_pipe_destroy_msg,
  651. };
  652. static struct dentry *
  653. nfsd4_cld_register_sb(struct super_block *sb, struct rpc_pipe *pipe)
  654. {
  655. struct dentry *dir, *dentry;
  656. dir = rpc_d_lookup_sb(sb, NFSD_PIPE_DIR);
  657. if (dir == NULL)
  658. return ERR_PTR(-ENOENT);
  659. dentry = rpc_mkpipe_dentry(dir, NFSD_CLD_PIPE, NULL, pipe);
  660. dput(dir);
  661. return dentry;
  662. }
  663. static void
  664. nfsd4_cld_unregister_sb(struct rpc_pipe *pipe)
  665. {
  666. if (pipe->dentry)
  667. rpc_unlink(pipe->dentry);
  668. }
  669. static struct dentry *
  670. nfsd4_cld_register_net(struct net *net, struct rpc_pipe *pipe)
  671. {
  672. struct super_block *sb;
  673. struct dentry *dentry;
  674. sb = rpc_get_sb_net(net);
  675. if (!sb)
  676. return NULL;
  677. dentry = nfsd4_cld_register_sb(sb, pipe);
  678. rpc_put_sb_net(net);
  679. return dentry;
  680. }
  681. static void
  682. nfsd4_cld_unregister_net(struct net *net, struct rpc_pipe *pipe)
  683. {
  684. struct super_block *sb;
  685. sb = rpc_get_sb_net(net);
  686. if (sb) {
  687. nfsd4_cld_unregister_sb(pipe);
  688. rpc_put_sb_net(net);
  689. }
  690. }
  691. /* Initialize rpc_pipefs pipe for communication with client tracking daemon */
  692. static int
  693. nfsd4_init_cld_pipe(struct net *net)
  694. {
  695. int ret;
  696. struct dentry *dentry;
  697. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  698. struct cld_net *cn;
  699. if (nn->cld_net)
  700. return 0;
  701. cn = kzalloc(sizeof(*cn), GFP_KERNEL);
  702. if (!cn) {
  703. ret = -ENOMEM;
  704. goto err;
  705. }
  706. cn->cn_pipe = rpc_mkpipe_data(&cld_upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
  707. if (IS_ERR(cn->cn_pipe)) {
  708. ret = PTR_ERR(cn->cn_pipe);
  709. goto err;
  710. }
  711. spin_lock_init(&cn->cn_lock);
  712. INIT_LIST_HEAD(&cn->cn_list);
  713. dentry = nfsd4_cld_register_net(net, cn->cn_pipe);
  714. if (IS_ERR(dentry)) {
  715. ret = PTR_ERR(dentry);
  716. goto err_destroy_data;
  717. }
  718. cn->cn_pipe->dentry = dentry;
  719. nn->cld_net = cn;
  720. return 0;
  721. err_destroy_data:
  722. rpc_destroy_pipe_data(cn->cn_pipe);
  723. err:
  724. kfree(cn);
  725. printk(KERN_ERR "NFSD: unable to create nfsdcld upcall pipe (%d)\n",
  726. ret);
  727. return ret;
  728. }
  729. static void
  730. nfsd4_remove_cld_pipe(struct net *net)
  731. {
  732. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  733. struct cld_net *cn = nn->cld_net;
  734. nfsd4_cld_unregister_net(net, cn->cn_pipe);
  735. rpc_destroy_pipe_data(cn->cn_pipe);
  736. kfree(nn->cld_net);
  737. nn->cld_net = NULL;
  738. }
  739. static struct cld_upcall *
  740. alloc_cld_upcall(struct cld_net *cn)
  741. {
  742. struct cld_upcall *new, *tmp;
  743. new = kzalloc(sizeof(*new), GFP_KERNEL);
  744. if (!new)
  745. return new;
  746. /* FIXME: hard cap on number in flight? */
  747. restart_search:
  748. spin_lock(&cn->cn_lock);
  749. list_for_each_entry(tmp, &cn->cn_list, cu_list) {
  750. if (tmp->cu_msg.cm_xid == cn->cn_xid) {
  751. cn->cn_xid++;
  752. spin_unlock(&cn->cn_lock);
  753. goto restart_search;
  754. }
  755. }
  756. new->cu_task = current;
  757. new->cu_msg.cm_vers = CLD_UPCALL_VERSION;
  758. put_unaligned(cn->cn_xid++, &new->cu_msg.cm_xid);
  759. new->cu_net = cn;
  760. list_add(&new->cu_list, &cn->cn_list);
  761. spin_unlock(&cn->cn_lock);
  762. dprintk("%s: allocated xid %u\n", __func__, new->cu_msg.cm_xid);
  763. return new;
  764. }
  765. static void
  766. free_cld_upcall(struct cld_upcall *victim)
  767. {
  768. struct cld_net *cn = victim->cu_net;
  769. spin_lock(&cn->cn_lock);
  770. list_del(&victim->cu_list);
  771. spin_unlock(&cn->cn_lock);
  772. kfree(victim);
  773. }
  774. /* Ask daemon to create a new record */
  775. static void
  776. nfsd4_cld_create(struct nfs4_client *clp)
  777. {
  778. int ret;
  779. struct cld_upcall *cup;
  780. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  781. struct cld_net *cn = nn->cld_net;
  782. /* Don't upcall if it's already stored */
  783. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  784. return;
  785. cup = alloc_cld_upcall(cn);
  786. if (!cup) {
  787. ret = -ENOMEM;
  788. goto out_err;
  789. }
  790. cup->cu_msg.cm_cmd = Cld_Create;
  791. cup->cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
  792. memcpy(cup->cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
  793. clp->cl_name.len);
  794. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
  795. if (!ret) {
  796. ret = cup->cu_msg.cm_status;
  797. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  798. }
  799. free_cld_upcall(cup);
  800. out_err:
  801. if (ret)
  802. printk(KERN_ERR "NFSD: Unable to create client "
  803. "record on stable storage: %d\n", ret);
  804. }
  805. /* Ask daemon to create a new record */
  806. static void
  807. nfsd4_cld_remove(struct nfs4_client *clp)
  808. {
  809. int ret;
  810. struct cld_upcall *cup;
  811. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  812. struct cld_net *cn = nn->cld_net;
  813. /* Don't upcall if it's already removed */
  814. if (!test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  815. return;
  816. cup = alloc_cld_upcall(cn);
  817. if (!cup) {
  818. ret = -ENOMEM;
  819. goto out_err;
  820. }
  821. cup->cu_msg.cm_cmd = Cld_Remove;
  822. cup->cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
  823. memcpy(cup->cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
  824. clp->cl_name.len);
  825. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
  826. if (!ret) {
  827. ret = cup->cu_msg.cm_status;
  828. clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  829. }
  830. free_cld_upcall(cup);
  831. out_err:
  832. if (ret)
  833. printk(KERN_ERR "NFSD: Unable to remove client "
  834. "record from stable storage: %d\n", ret);
  835. }
  836. /* Check for presence of a record, and update its timestamp */
  837. static int
  838. nfsd4_cld_check(struct nfs4_client *clp)
  839. {
  840. int ret;
  841. struct cld_upcall *cup;
  842. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  843. struct cld_net *cn = nn->cld_net;
  844. /* Don't upcall if one was already stored during this grace pd */
  845. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  846. return 0;
  847. cup = alloc_cld_upcall(cn);
  848. if (!cup) {
  849. printk(KERN_ERR "NFSD: Unable to check client record on "
  850. "stable storage: %d\n", -ENOMEM);
  851. return -ENOMEM;
  852. }
  853. cup->cu_msg.cm_cmd = Cld_Check;
  854. cup->cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
  855. memcpy(cup->cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
  856. clp->cl_name.len);
  857. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
  858. if (!ret) {
  859. ret = cup->cu_msg.cm_status;
  860. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  861. }
  862. free_cld_upcall(cup);
  863. return ret;
  864. }
  865. static void
  866. nfsd4_cld_grace_done(struct nfsd_net *nn, time_t boot_time)
  867. {
  868. int ret;
  869. struct cld_upcall *cup;
  870. struct cld_net *cn = nn->cld_net;
  871. cup = alloc_cld_upcall(cn);
  872. if (!cup) {
  873. ret = -ENOMEM;
  874. goto out_err;
  875. }
  876. cup->cu_msg.cm_cmd = Cld_GraceDone;
  877. cup->cu_msg.cm_u.cm_gracetime = (int64_t)boot_time;
  878. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
  879. if (!ret)
  880. ret = cup->cu_msg.cm_status;
  881. free_cld_upcall(cup);
  882. out_err:
  883. if (ret)
  884. printk(KERN_ERR "NFSD: Unable to end grace period: %d\n", ret);
  885. }
  886. static struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops = {
  887. .init = nfsd4_init_cld_pipe,
  888. .exit = nfsd4_remove_cld_pipe,
  889. .create = nfsd4_cld_create,
  890. .remove = nfsd4_cld_remove,
  891. .check = nfsd4_cld_check,
  892. .grace_done = nfsd4_cld_grace_done,
  893. };
  894. /* upcall via usermodehelper */
  895. static char cltrack_prog[PATH_MAX] = "/sbin/nfsdcltrack";
  896. module_param_string(cltrack_prog, cltrack_prog, sizeof(cltrack_prog),
  897. S_IRUGO|S_IWUSR);
  898. MODULE_PARM_DESC(cltrack_prog, "Path to the nfsdcltrack upcall program");
  899. static bool cltrack_legacy_disable;
  900. module_param(cltrack_legacy_disable, bool, S_IRUGO|S_IWUSR);
  901. MODULE_PARM_DESC(cltrack_legacy_disable,
  902. "Disable legacy recoverydir conversion. Default: false");
  903. #define LEGACY_TOPDIR_ENV_PREFIX "NFSDCLTRACK_LEGACY_TOPDIR="
  904. #define LEGACY_RECDIR_ENV_PREFIX "NFSDCLTRACK_LEGACY_RECDIR="
  905. static char *
  906. nfsd4_cltrack_legacy_topdir(void)
  907. {
  908. int copied;
  909. size_t len;
  910. char *result;
  911. if (cltrack_legacy_disable)
  912. return NULL;
  913. len = strlen(LEGACY_TOPDIR_ENV_PREFIX) +
  914. strlen(nfs4_recoverydir()) + 1;
  915. result = kmalloc(len, GFP_KERNEL);
  916. if (!result)
  917. return result;
  918. copied = snprintf(result, len, LEGACY_TOPDIR_ENV_PREFIX "%s",
  919. nfs4_recoverydir());
  920. if (copied >= len) {
  921. /* just return nothing if output was truncated */
  922. kfree(result);
  923. return NULL;
  924. }
  925. return result;
  926. }
  927. static char *
  928. nfsd4_cltrack_legacy_recdir(const struct xdr_netobj *name)
  929. {
  930. int copied;
  931. size_t len;
  932. char *result;
  933. if (cltrack_legacy_disable)
  934. return NULL;
  935. /* +1 is for '/' between "topdir" and "recdir" */
  936. len = strlen(LEGACY_RECDIR_ENV_PREFIX) +
  937. strlen(nfs4_recoverydir()) + 1 + HEXDIR_LEN;
  938. result = kmalloc(len, GFP_KERNEL);
  939. if (!result)
  940. return result;
  941. copied = snprintf(result, len, LEGACY_RECDIR_ENV_PREFIX "%s/",
  942. nfs4_recoverydir());
  943. if (copied > (len - HEXDIR_LEN)) {
  944. /* just return nothing if output will be truncated */
  945. kfree(result);
  946. return NULL;
  947. }
  948. copied = nfs4_make_rec_clidname(result + copied, name);
  949. if (copied) {
  950. kfree(result);
  951. return NULL;
  952. }
  953. return result;
  954. }
  955. static int
  956. nfsd4_umh_cltrack_upcall(char *cmd, char *arg, char *legacy)
  957. {
  958. char *envp[2];
  959. char *argv[4];
  960. int ret;
  961. if (unlikely(!cltrack_prog[0])) {
  962. dprintk("%s: cltrack_prog is disabled\n", __func__);
  963. return -EACCES;
  964. }
  965. dprintk("%s: cmd: %s\n", __func__, cmd);
  966. dprintk("%s: arg: %s\n", __func__, arg ? arg : "(null)");
  967. dprintk("%s: legacy: %s\n", __func__, legacy ? legacy : "(null)");
  968. envp[0] = legacy;
  969. envp[1] = NULL;
  970. argv[0] = (char *)cltrack_prog;
  971. argv[1] = cmd;
  972. argv[2] = arg;
  973. argv[3] = NULL;
  974. ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
  975. /*
  976. * Disable the upcall mechanism if we're getting an ENOENT or EACCES
  977. * error. The admin can re-enable it on the fly by using sysfs
  978. * once the problem has been fixed.
  979. */
  980. if (ret == -ENOENT || ret == -EACCES) {
  981. dprintk("NFSD: %s was not found or isn't executable (%d). "
  982. "Setting cltrack_prog to blank string!",
  983. cltrack_prog, ret);
  984. cltrack_prog[0] = '\0';
  985. }
  986. dprintk("%s: %s return value: %d\n", __func__, cltrack_prog, ret);
  987. return ret;
  988. }
  989. static char *
  990. bin_to_hex_dup(const unsigned char *src, int srclen)
  991. {
  992. int i;
  993. char *buf, *hex;
  994. /* +1 for terminating NULL */
  995. buf = kmalloc((srclen * 2) + 1, GFP_KERNEL);
  996. if (!buf)
  997. return buf;
  998. hex = buf;
  999. for (i = 0; i < srclen; i++) {
  1000. sprintf(hex, "%2.2x", *src++);
  1001. hex += 2;
  1002. }
  1003. return buf;
  1004. }
  1005. static int
  1006. nfsd4_umh_cltrack_init(struct net __attribute__((unused)) *net)
  1007. {
  1008. /* XXX: The usermode helper s not working in container yet. */
  1009. if (net != &init_net) {
  1010. WARN(1, KERN_ERR "NFSD: attempt to initialize umh client "
  1011. "tracking in a container!\n");
  1012. return -EINVAL;
  1013. }
  1014. return nfsd4_umh_cltrack_upcall("init", NULL, NULL);
  1015. }
  1016. static void
  1017. nfsd4_umh_cltrack_create(struct nfs4_client *clp)
  1018. {
  1019. char *hexid;
  1020. hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
  1021. if (!hexid) {
  1022. dprintk("%s: can't allocate memory for upcall!\n", __func__);
  1023. return;
  1024. }
  1025. nfsd4_umh_cltrack_upcall("create", hexid, NULL);
  1026. kfree(hexid);
  1027. }
  1028. static void
  1029. nfsd4_umh_cltrack_remove(struct nfs4_client *clp)
  1030. {
  1031. char *hexid;
  1032. hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
  1033. if (!hexid) {
  1034. dprintk("%s: can't allocate memory for upcall!\n", __func__);
  1035. return;
  1036. }
  1037. nfsd4_umh_cltrack_upcall("remove", hexid, NULL);
  1038. kfree(hexid);
  1039. }
  1040. static int
  1041. nfsd4_umh_cltrack_check(struct nfs4_client *clp)
  1042. {
  1043. int ret;
  1044. char *hexid, *legacy;
  1045. hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
  1046. if (!hexid) {
  1047. dprintk("%s: can't allocate memory for upcall!\n", __func__);
  1048. return -ENOMEM;
  1049. }
  1050. legacy = nfsd4_cltrack_legacy_recdir(&clp->cl_name);
  1051. ret = nfsd4_umh_cltrack_upcall("check", hexid, legacy);
  1052. kfree(legacy);
  1053. kfree(hexid);
  1054. return ret;
  1055. }
  1056. static void
  1057. nfsd4_umh_cltrack_grace_done(struct nfsd_net __attribute__((unused)) *nn,
  1058. time_t boot_time)
  1059. {
  1060. char *legacy;
  1061. char timestr[22]; /* FIXME: better way to determine max size? */
  1062. sprintf(timestr, "%ld", boot_time);
  1063. legacy = nfsd4_cltrack_legacy_topdir();
  1064. nfsd4_umh_cltrack_upcall("gracedone", timestr, legacy);
  1065. kfree(legacy);
  1066. }
  1067. static struct nfsd4_client_tracking_ops nfsd4_umh_tracking_ops = {
  1068. .init = nfsd4_umh_cltrack_init,
  1069. .exit = NULL,
  1070. .create = nfsd4_umh_cltrack_create,
  1071. .remove = nfsd4_umh_cltrack_remove,
  1072. .check = nfsd4_umh_cltrack_check,
  1073. .grace_done = nfsd4_umh_cltrack_grace_done,
  1074. };
  1075. int
  1076. nfsd4_client_tracking_init(struct net *net)
  1077. {
  1078. int status;
  1079. struct path path;
  1080. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1081. /* just run the init if it the method is already decided */
  1082. if (nn->client_tracking_ops)
  1083. goto do_init;
  1084. /*
  1085. * First, try a UMH upcall. It should succeed or fail quickly, so
  1086. * there's little harm in trying that first.
  1087. */
  1088. nn->client_tracking_ops = &nfsd4_umh_tracking_ops;
  1089. status = nn->client_tracking_ops->init(net);
  1090. if (!status)
  1091. return status;
  1092. /*
  1093. * See if the recoverydir exists and is a directory. If it is,
  1094. * then use the legacy ops.
  1095. */
  1096. nn->client_tracking_ops = &nfsd4_legacy_tracking_ops;
  1097. status = kern_path(nfs4_recoverydir(), LOOKUP_FOLLOW, &path);
  1098. if (!status) {
  1099. status = S_ISDIR(path.dentry->d_inode->i_mode);
  1100. path_put(&path);
  1101. if (status)
  1102. goto do_init;
  1103. }
  1104. /* Finally, try to use nfsdcld */
  1105. nn->client_tracking_ops = &nfsd4_cld_tracking_ops;
  1106. printk(KERN_WARNING "NFSD: the nfsdcld client tracking upcall will be "
  1107. "removed in 3.10. Please transition to using "
  1108. "nfsdcltrack.\n");
  1109. do_init:
  1110. status = nn->client_tracking_ops->init(net);
  1111. if (status) {
  1112. printk(KERN_WARNING "NFSD: Unable to initialize client "
  1113. "recovery tracking! (%d)\n", status);
  1114. nn->client_tracking_ops = NULL;
  1115. }
  1116. return status;
  1117. }
  1118. void
  1119. nfsd4_client_tracking_exit(struct net *net)
  1120. {
  1121. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1122. if (nn->client_tracking_ops) {
  1123. if (nn->client_tracking_ops->exit)
  1124. nn->client_tracking_ops->exit(net);
  1125. nn->client_tracking_ops = NULL;
  1126. }
  1127. }
  1128. void
  1129. nfsd4_client_record_create(struct nfs4_client *clp)
  1130. {
  1131. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1132. if (nn->client_tracking_ops)
  1133. nn->client_tracking_ops->create(clp);
  1134. }
  1135. void
  1136. nfsd4_client_record_remove(struct nfs4_client *clp)
  1137. {
  1138. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1139. if (nn->client_tracking_ops)
  1140. nn->client_tracking_ops->remove(clp);
  1141. }
  1142. int
  1143. nfsd4_client_record_check(struct nfs4_client *clp)
  1144. {
  1145. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1146. if (nn->client_tracking_ops)
  1147. return nn->client_tracking_ops->check(clp);
  1148. return -EOPNOTSUPP;
  1149. }
  1150. void
  1151. nfsd4_record_grace_done(struct nfsd_net *nn, time_t boot_time)
  1152. {
  1153. if (nn->client_tracking_ops)
  1154. nn->client_tracking_ops->grace_done(nn, boot_time);
  1155. }
  1156. static int
  1157. rpc_pipefs_event(struct notifier_block *nb, unsigned long event, void *ptr)
  1158. {
  1159. struct super_block *sb = ptr;
  1160. struct net *net = sb->s_fs_info;
  1161. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1162. struct cld_net *cn = nn->cld_net;
  1163. struct dentry *dentry;
  1164. int ret = 0;
  1165. if (!try_module_get(THIS_MODULE))
  1166. return 0;
  1167. if (!cn) {
  1168. module_put(THIS_MODULE);
  1169. return 0;
  1170. }
  1171. switch (event) {
  1172. case RPC_PIPEFS_MOUNT:
  1173. dentry = nfsd4_cld_register_sb(sb, cn->cn_pipe);
  1174. if (IS_ERR(dentry)) {
  1175. ret = PTR_ERR(dentry);
  1176. break;
  1177. }
  1178. cn->cn_pipe->dentry = dentry;
  1179. break;
  1180. case RPC_PIPEFS_UMOUNT:
  1181. if (cn->cn_pipe->dentry)
  1182. nfsd4_cld_unregister_sb(cn->cn_pipe);
  1183. break;
  1184. default:
  1185. ret = -ENOTSUPP;
  1186. break;
  1187. }
  1188. module_put(THIS_MODULE);
  1189. return ret;
  1190. }
  1191. static struct notifier_block nfsd4_cld_block = {
  1192. .notifier_call = rpc_pipefs_event,
  1193. };
  1194. int
  1195. register_cld_notifier(void)
  1196. {
  1197. return rpc_pipefs_notifier_register(&nfsd4_cld_block);
  1198. }
  1199. void
  1200. unregister_cld_notifier(void)
  1201. {
  1202. rpc_pipefs_notifier_unregister(&nfsd4_cld_block);
  1203. }