nfs4recover.c 31 KB

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