nfs4recover.c 32 KB

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