nfs4recover.c 36 KB

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