nfs4recover.c 32 KB

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