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_array(CLIENT_HASH_SIZE,
  444. sizeof(struct list_head),
  445. GFP_KERNEL);
  446. if (!nn->reclaim_str_hashtbl)
  447. return -ENOMEM;
  448. for (i = 0; i < CLIENT_HASH_SIZE; i++)
  449. INIT_LIST_HEAD(&nn->reclaim_str_hashtbl[i]);
  450. nn->reclaim_str_hashtbl_size = 0;
  451. return 0;
  452. }
  453. static void
  454. nfs4_legacy_state_shutdown(struct net *net)
  455. {
  456. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  457. kfree(nn->reclaim_str_hashtbl);
  458. }
  459. static int
  460. nfsd4_load_reboot_recovery_data(struct net *net)
  461. {
  462. int status;
  463. status = nfsd4_init_recdir(net);
  464. if (status)
  465. return status;
  466. status = nfsd4_recdir_load(net);
  467. if (status)
  468. nfsd4_shutdown_recdir(net);
  469. return status;
  470. }
  471. static int
  472. nfsd4_legacy_tracking_init(struct net *net)
  473. {
  474. int status;
  475. /* XXX: The legacy code won't work in a container */
  476. if (net != &init_net) {
  477. pr_warn("NFSD: attempt to initialize legacy client tracking in a container ignored.\n");
  478. return -EINVAL;
  479. }
  480. status = nfs4_legacy_state_init(net);
  481. if (status)
  482. return status;
  483. status = nfsd4_load_reboot_recovery_data(net);
  484. if (status)
  485. goto err;
  486. return 0;
  487. err:
  488. nfs4_legacy_state_shutdown(net);
  489. return status;
  490. }
  491. static void
  492. nfsd4_legacy_tracking_exit(struct net *net)
  493. {
  494. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  495. nfs4_release_reclaim(nn);
  496. nfsd4_shutdown_recdir(net);
  497. nfs4_legacy_state_shutdown(net);
  498. }
  499. /*
  500. * Change the NFSv4 recovery directory to recdir.
  501. */
  502. int
  503. nfs4_reset_recoverydir(char *recdir)
  504. {
  505. int status;
  506. struct path path;
  507. status = kern_path(recdir, LOOKUP_FOLLOW, &path);
  508. if (status)
  509. return status;
  510. status = -ENOTDIR;
  511. if (d_is_dir(path.dentry)) {
  512. strcpy(user_recovery_dirname, recdir);
  513. status = 0;
  514. }
  515. path_put(&path);
  516. return status;
  517. }
  518. char *
  519. nfs4_recoverydir(void)
  520. {
  521. return user_recovery_dirname;
  522. }
  523. static int
  524. nfsd4_check_legacy_client(struct nfs4_client *clp)
  525. {
  526. int status;
  527. char dname[HEXDIR_LEN];
  528. struct nfs4_client_reclaim *crp;
  529. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  530. /* did we already find that this client is stable? */
  531. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  532. return 0;
  533. status = nfs4_make_rec_clidname(dname, &clp->cl_name);
  534. if (status) {
  535. legacy_recdir_name_error(clp, status);
  536. return status;
  537. }
  538. /* look for it in the reclaim hashtable otherwise */
  539. crp = nfsd4_find_reclaim_client(dname, nn);
  540. if (crp) {
  541. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  542. crp->cr_clp = clp;
  543. return 0;
  544. }
  545. return -ENOENT;
  546. }
  547. static const struct nfsd4_client_tracking_ops nfsd4_legacy_tracking_ops = {
  548. .init = nfsd4_legacy_tracking_init,
  549. .exit = nfsd4_legacy_tracking_exit,
  550. .create = nfsd4_create_clid_dir,
  551. .remove = nfsd4_remove_clid_dir,
  552. .check = nfsd4_check_legacy_client,
  553. .grace_done = nfsd4_recdir_purge_old,
  554. };
  555. /* Globals */
  556. #define NFSD_PIPE_DIR "nfsd"
  557. #define NFSD_CLD_PIPE "cld"
  558. /* per-net-ns structure for holding cld upcall info */
  559. struct cld_net {
  560. struct rpc_pipe *cn_pipe;
  561. spinlock_t cn_lock;
  562. struct list_head cn_list;
  563. unsigned int cn_xid;
  564. };
  565. struct cld_upcall {
  566. struct list_head cu_list;
  567. struct cld_net *cu_net;
  568. struct task_struct *cu_task;
  569. struct cld_msg cu_msg;
  570. };
  571. static int
  572. __cld_pipe_upcall(struct rpc_pipe *pipe, struct cld_msg *cmsg)
  573. {
  574. int ret;
  575. struct rpc_pipe_msg msg;
  576. memset(&msg, 0, sizeof(msg));
  577. msg.data = cmsg;
  578. msg.len = sizeof(*cmsg);
  579. /*
  580. * Set task state before we queue the upcall. That prevents
  581. * wake_up_process in the downcall from racing with schedule.
  582. */
  583. set_current_state(TASK_UNINTERRUPTIBLE);
  584. ret = rpc_queue_upcall(pipe, &msg);
  585. if (ret < 0) {
  586. set_current_state(TASK_RUNNING);
  587. goto out;
  588. }
  589. schedule();
  590. if (msg.errno < 0)
  591. ret = msg.errno;
  592. out:
  593. return ret;
  594. }
  595. static int
  596. cld_pipe_upcall(struct rpc_pipe *pipe, struct cld_msg *cmsg)
  597. {
  598. int ret;
  599. /*
  600. * -EAGAIN occurs when pipe is closed and reopened while there are
  601. * upcalls queued.
  602. */
  603. do {
  604. ret = __cld_pipe_upcall(pipe, cmsg);
  605. } while (ret == -EAGAIN);
  606. return ret;
  607. }
  608. static ssize_t
  609. cld_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
  610. {
  611. struct cld_upcall *tmp, *cup;
  612. struct cld_msg __user *cmsg = (struct cld_msg __user *)src;
  613. uint32_t xid;
  614. struct nfsd_net *nn = net_generic(file_inode(filp)->i_sb->s_fs_info,
  615. nfsd_net_id);
  616. struct cld_net *cn = nn->cld_net;
  617. if (mlen != sizeof(*cmsg)) {
  618. dprintk("%s: got %zu bytes, expected %zu\n", __func__, mlen,
  619. sizeof(*cmsg));
  620. return -EINVAL;
  621. }
  622. /* copy just the xid so we can try to find that */
  623. if (copy_from_user(&xid, &cmsg->cm_xid, sizeof(xid)) != 0) {
  624. dprintk("%s: error when copying xid from userspace", __func__);
  625. return -EFAULT;
  626. }
  627. /* walk the list and find corresponding xid */
  628. cup = NULL;
  629. spin_lock(&cn->cn_lock);
  630. list_for_each_entry(tmp, &cn->cn_list, cu_list) {
  631. if (get_unaligned(&tmp->cu_msg.cm_xid) == xid) {
  632. cup = tmp;
  633. list_del_init(&cup->cu_list);
  634. break;
  635. }
  636. }
  637. spin_unlock(&cn->cn_lock);
  638. /* couldn't find upcall? */
  639. if (!cup) {
  640. dprintk("%s: couldn't find upcall -- xid=%u\n", __func__, xid);
  641. return -EINVAL;
  642. }
  643. if (copy_from_user(&cup->cu_msg, src, mlen) != 0)
  644. return -EFAULT;
  645. wake_up_process(cup->cu_task);
  646. return mlen;
  647. }
  648. static void
  649. cld_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  650. {
  651. struct cld_msg *cmsg = msg->data;
  652. struct cld_upcall *cup = container_of(cmsg, struct cld_upcall,
  653. cu_msg);
  654. /* errno >= 0 means we got a downcall */
  655. if (msg->errno >= 0)
  656. return;
  657. wake_up_process(cup->cu_task);
  658. }
  659. static const struct rpc_pipe_ops cld_upcall_ops = {
  660. .upcall = rpc_pipe_generic_upcall,
  661. .downcall = cld_pipe_downcall,
  662. .destroy_msg = cld_pipe_destroy_msg,
  663. };
  664. static struct dentry *
  665. nfsd4_cld_register_sb(struct super_block *sb, struct rpc_pipe *pipe)
  666. {
  667. struct dentry *dir, *dentry;
  668. dir = rpc_d_lookup_sb(sb, NFSD_PIPE_DIR);
  669. if (dir == NULL)
  670. return ERR_PTR(-ENOENT);
  671. dentry = rpc_mkpipe_dentry(dir, NFSD_CLD_PIPE, NULL, pipe);
  672. dput(dir);
  673. return dentry;
  674. }
  675. static void
  676. nfsd4_cld_unregister_sb(struct rpc_pipe *pipe)
  677. {
  678. if (pipe->dentry)
  679. rpc_unlink(pipe->dentry);
  680. }
  681. static struct dentry *
  682. nfsd4_cld_register_net(struct net *net, struct rpc_pipe *pipe)
  683. {
  684. struct super_block *sb;
  685. struct dentry *dentry;
  686. sb = rpc_get_sb_net(net);
  687. if (!sb)
  688. return NULL;
  689. dentry = nfsd4_cld_register_sb(sb, pipe);
  690. rpc_put_sb_net(net);
  691. return dentry;
  692. }
  693. static void
  694. nfsd4_cld_unregister_net(struct net *net, struct rpc_pipe *pipe)
  695. {
  696. struct super_block *sb;
  697. sb = rpc_get_sb_net(net);
  698. if (sb) {
  699. nfsd4_cld_unregister_sb(pipe);
  700. rpc_put_sb_net(net);
  701. }
  702. }
  703. /* Initialize rpc_pipefs pipe for communication with client tracking daemon */
  704. static int
  705. nfsd4_init_cld_pipe(struct net *net)
  706. {
  707. int ret;
  708. struct dentry *dentry;
  709. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  710. struct cld_net *cn;
  711. if (nn->cld_net)
  712. return 0;
  713. cn = kzalloc(sizeof(*cn), GFP_KERNEL);
  714. if (!cn) {
  715. ret = -ENOMEM;
  716. goto err;
  717. }
  718. cn->cn_pipe = rpc_mkpipe_data(&cld_upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
  719. if (IS_ERR(cn->cn_pipe)) {
  720. ret = PTR_ERR(cn->cn_pipe);
  721. goto err;
  722. }
  723. spin_lock_init(&cn->cn_lock);
  724. INIT_LIST_HEAD(&cn->cn_list);
  725. dentry = nfsd4_cld_register_net(net, cn->cn_pipe);
  726. if (IS_ERR(dentry)) {
  727. ret = PTR_ERR(dentry);
  728. goto err_destroy_data;
  729. }
  730. cn->cn_pipe->dentry = dentry;
  731. nn->cld_net = cn;
  732. return 0;
  733. err_destroy_data:
  734. rpc_destroy_pipe_data(cn->cn_pipe);
  735. err:
  736. kfree(cn);
  737. printk(KERN_ERR "NFSD: unable to create nfsdcld upcall pipe (%d)\n",
  738. ret);
  739. return ret;
  740. }
  741. static void
  742. nfsd4_remove_cld_pipe(struct net *net)
  743. {
  744. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  745. struct cld_net *cn = nn->cld_net;
  746. nfsd4_cld_unregister_net(net, cn->cn_pipe);
  747. rpc_destroy_pipe_data(cn->cn_pipe);
  748. kfree(nn->cld_net);
  749. nn->cld_net = NULL;
  750. }
  751. static struct cld_upcall *
  752. alloc_cld_upcall(struct cld_net *cn)
  753. {
  754. struct cld_upcall *new, *tmp;
  755. new = kzalloc(sizeof(*new), GFP_KERNEL);
  756. if (!new)
  757. return new;
  758. /* FIXME: hard cap on number in flight? */
  759. restart_search:
  760. spin_lock(&cn->cn_lock);
  761. list_for_each_entry(tmp, &cn->cn_list, cu_list) {
  762. if (tmp->cu_msg.cm_xid == cn->cn_xid) {
  763. cn->cn_xid++;
  764. spin_unlock(&cn->cn_lock);
  765. goto restart_search;
  766. }
  767. }
  768. new->cu_task = current;
  769. new->cu_msg.cm_vers = CLD_UPCALL_VERSION;
  770. put_unaligned(cn->cn_xid++, &new->cu_msg.cm_xid);
  771. new->cu_net = cn;
  772. list_add(&new->cu_list, &cn->cn_list);
  773. spin_unlock(&cn->cn_lock);
  774. dprintk("%s: allocated xid %u\n", __func__, new->cu_msg.cm_xid);
  775. return new;
  776. }
  777. static void
  778. free_cld_upcall(struct cld_upcall *victim)
  779. {
  780. struct cld_net *cn = victim->cu_net;
  781. spin_lock(&cn->cn_lock);
  782. list_del(&victim->cu_list);
  783. spin_unlock(&cn->cn_lock);
  784. kfree(victim);
  785. }
  786. /* Ask daemon to create a new record */
  787. static void
  788. nfsd4_cld_create(struct nfs4_client *clp)
  789. {
  790. int ret;
  791. struct cld_upcall *cup;
  792. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  793. struct cld_net *cn = nn->cld_net;
  794. /* Don't upcall if it's already stored */
  795. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  796. return;
  797. cup = alloc_cld_upcall(cn);
  798. if (!cup) {
  799. ret = -ENOMEM;
  800. goto out_err;
  801. }
  802. cup->cu_msg.cm_cmd = Cld_Create;
  803. cup->cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
  804. memcpy(cup->cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
  805. clp->cl_name.len);
  806. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
  807. if (!ret) {
  808. ret = cup->cu_msg.cm_status;
  809. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  810. }
  811. free_cld_upcall(cup);
  812. out_err:
  813. if (ret)
  814. printk(KERN_ERR "NFSD: Unable to create client "
  815. "record on stable storage: %d\n", ret);
  816. }
  817. /* Ask daemon to create a new record */
  818. static void
  819. nfsd4_cld_remove(struct nfs4_client *clp)
  820. {
  821. int ret;
  822. struct cld_upcall *cup;
  823. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  824. struct cld_net *cn = nn->cld_net;
  825. /* Don't upcall if it's already removed */
  826. if (!test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  827. return;
  828. cup = alloc_cld_upcall(cn);
  829. if (!cup) {
  830. ret = -ENOMEM;
  831. goto out_err;
  832. }
  833. cup->cu_msg.cm_cmd = Cld_Remove;
  834. cup->cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
  835. memcpy(cup->cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
  836. clp->cl_name.len);
  837. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
  838. if (!ret) {
  839. ret = cup->cu_msg.cm_status;
  840. clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  841. }
  842. free_cld_upcall(cup);
  843. out_err:
  844. if (ret)
  845. printk(KERN_ERR "NFSD: Unable to remove client "
  846. "record from stable storage: %d\n", ret);
  847. }
  848. /* Check for presence of a record, and update its timestamp */
  849. static int
  850. nfsd4_cld_check(struct nfs4_client *clp)
  851. {
  852. int ret;
  853. struct cld_upcall *cup;
  854. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  855. struct cld_net *cn = nn->cld_net;
  856. /* Don't upcall if one was already stored during this grace pd */
  857. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  858. return 0;
  859. cup = alloc_cld_upcall(cn);
  860. if (!cup) {
  861. printk(KERN_ERR "NFSD: Unable to check client record on "
  862. "stable storage: %d\n", -ENOMEM);
  863. return -ENOMEM;
  864. }
  865. cup->cu_msg.cm_cmd = Cld_Check;
  866. cup->cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
  867. memcpy(cup->cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
  868. clp->cl_name.len);
  869. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
  870. if (!ret) {
  871. ret = cup->cu_msg.cm_status;
  872. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  873. }
  874. free_cld_upcall(cup);
  875. return ret;
  876. }
  877. static void
  878. nfsd4_cld_grace_done(struct nfsd_net *nn)
  879. {
  880. int ret;
  881. struct cld_upcall *cup;
  882. struct cld_net *cn = nn->cld_net;
  883. cup = alloc_cld_upcall(cn);
  884. if (!cup) {
  885. ret = -ENOMEM;
  886. goto out_err;
  887. }
  888. cup->cu_msg.cm_cmd = Cld_GraceDone;
  889. cup->cu_msg.cm_u.cm_gracetime = (int64_t)nn->boot_time;
  890. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
  891. if (!ret)
  892. ret = cup->cu_msg.cm_status;
  893. free_cld_upcall(cup);
  894. out_err:
  895. if (ret)
  896. printk(KERN_ERR "NFSD: Unable to end grace period: %d\n", ret);
  897. }
  898. static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops = {
  899. .init = nfsd4_init_cld_pipe,
  900. .exit = nfsd4_remove_cld_pipe,
  901. .create = nfsd4_cld_create,
  902. .remove = nfsd4_cld_remove,
  903. .check = nfsd4_cld_check,
  904. .grace_done = nfsd4_cld_grace_done,
  905. };
  906. /* upcall via usermodehelper */
  907. static char cltrack_prog[PATH_MAX] = "/sbin/nfsdcltrack";
  908. module_param_string(cltrack_prog, cltrack_prog, sizeof(cltrack_prog),
  909. S_IRUGO|S_IWUSR);
  910. MODULE_PARM_DESC(cltrack_prog, "Path to the nfsdcltrack upcall program");
  911. static bool cltrack_legacy_disable;
  912. module_param(cltrack_legacy_disable, bool, S_IRUGO|S_IWUSR);
  913. MODULE_PARM_DESC(cltrack_legacy_disable,
  914. "Disable legacy recoverydir conversion. Default: false");
  915. #define LEGACY_TOPDIR_ENV_PREFIX "NFSDCLTRACK_LEGACY_TOPDIR="
  916. #define LEGACY_RECDIR_ENV_PREFIX "NFSDCLTRACK_LEGACY_RECDIR="
  917. #define HAS_SESSION_ENV_PREFIX "NFSDCLTRACK_CLIENT_HAS_SESSION="
  918. #define GRACE_START_ENV_PREFIX "NFSDCLTRACK_GRACE_START="
  919. static char *
  920. nfsd4_cltrack_legacy_topdir(void)
  921. {
  922. int copied;
  923. size_t len;
  924. char *result;
  925. if (cltrack_legacy_disable)
  926. return NULL;
  927. len = strlen(LEGACY_TOPDIR_ENV_PREFIX) +
  928. strlen(nfs4_recoverydir()) + 1;
  929. result = kmalloc(len, GFP_KERNEL);
  930. if (!result)
  931. return result;
  932. copied = snprintf(result, len, LEGACY_TOPDIR_ENV_PREFIX "%s",
  933. nfs4_recoverydir());
  934. if (copied >= len) {
  935. /* just return nothing if output was truncated */
  936. kfree(result);
  937. return NULL;
  938. }
  939. return result;
  940. }
  941. static char *
  942. nfsd4_cltrack_legacy_recdir(const struct xdr_netobj *name)
  943. {
  944. int copied;
  945. size_t len;
  946. char *result;
  947. if (cltrack_legacy_disable)
  948. return NULL;
  949. /* +1 is for '/' between "topdir" and "recdir" */
  950. len = strlen(LEGACY_RECDIR_ENV_PREFIX) +
  951. strlen(nfs4_recoverydir()) + 1 + HEXDIR_LEN;
  952. result = kmalloc(len, GFP_KERNEL);
  953. if (!result)
  954. return result;
  955. copied = snprintf(result, len, LEGACY_RECDIR_ENV_PREFIX "%s/",
  956. nfs4_recoverydir());
  957. if (copied > (len - HEXDIR_LEN)) {
  958. /* just return nothing if output will be truncated */
  959. kfree(result);
  960. return NULL;
  961. }
  962. copied = nfs4_make_rec_clidname(result + copied, name);
  963. if (copied) {
  964. kfree(result);
  965. return NULL;
  966. }
  967. return result;
  968. }
  969. static char *
  970. nfsd4_cltrack_client_has_session(struct nfs4_client *clp)
  971. {
  972. int copied;
  973. size_t len;
  974. char *result;
  975. /* prefix + Y/N character + terminating NULL */
  976. len = strlen(HAS_SESSION_ENV_PREFIX) + 1 + 1;
  977. result = kmalloc(len, GFP_KERNEL);
  978. if (!result)
  979. return result;
  980. copied = snprintf(result, len, HAS_SESSION_ENV_PREFIX "%c",
  981. clp->cl_minorversion ? 'Y' : 'N');
  982. if (copied >= len) {
  983. /* just return nothing if output was truncated */
  984. kfree(result);
  985. return NULL;
  986. }
  987. return result;
  988. }
  989. static char *
  990. nfsd4_cltrack_grace_start(time_t grace_start)
  991. {
  992. int copied;
  993. size_t len;
  994. char *result;
  995. /* prefix + max width of int64_t string + terminating NULL */
  996. len = strlen(GRACE_START_ENV_PREFIX) + 22 + 1;
  997. result = kmalloc(len, GFP_KERNEL);
  998. if (!result)
  999. return result;
  1000. copied = snprintf(result, len, GRACE_START_ENV_PREFIX "%ld",
  1001. grace_start);
  1002. if (copied >= len) {
  1003. /* just return nothing if output was truncated */
  1004. kfree(result);
  1005. return NULL;
  1006. }
  1007. return result;
  1008. }
  1009. static int
  1010. nfsd4_umh_cltrack_upcall(char *cmd, char *arg, char *env0, char *env1)
  1011. {
  1012. char *envp[3];
  1013. char *argv[4];
  1014. int ret;
  1015. if (unlikely(!cltrack_prog[0])) {
  1016. dprintk("%s: cltrack_prog is disabled\n", __func__);
  1017. return -EACCES;
  1018. }
  1019. dprintk("%s: cmd: %s\n", __func__, cmd);
  1020. dprintk("%s: arg: %s\n", __func__, arg ? arg : "(null)");
  1021. dprintk("%s: env0: %s\n", __func__, env0 ? env0 : "(null)");
  1022. dprintk("%s: env1: %s\n", __func__, env1 ? env1 : "(null)");
  1023. envp[0] = env0;
  1024. envp[1] = env1;
  1025. envp[2] = NULL;
  1026. argv[0] = (char *)cltrack_prog;
  1027. argv[1] = cmd;
  1028. argv[2] = arg;
  1029. argv[3] = NULL;
  1030. ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
  1031. /*
  1032. * Disable the upcall mechanism if we're getting an ENOENT or EACCES
  1033. * error. The admin can re-enable it on the fly by using sysfs
  1034. * once the problem has been fixed.
  1035. */
  1036. if (ret == -ENOENT || ret == -EACCES) {
  1037. dprintk("NFSD: %s was not found or isn't executable (%d). "
  1038. "Setting cltrack_prog to blank string!",
  1039. cltrack_prog, ret);
  1040. cltrack_prog[0] = '\0';
  1041. }
  1042. dprintk("%s: %s return value: %d\n", __func__, cltrack_prog, ret);
  1043. return ret;
  1044. }
  1045. static char *
  1046. bin_to_hex_dup(const unsigned char *src, int srclen)
  1047. {
  1048. int i;
  1049. char *buf, *hex;
  1050. /* +1 for terminating NULL */
  1051. buf = kmalloc((srclen * 2) + 1, GFP_KERNEL);
  1052. if (!buf)
  1053. return buf;
  1054. hex = buf;
  1055. for (i = 0; i < srclen; i++) {
  1056. sprintf(hex, "%2.2x", *src++);
  1057. hex += 2;
  1058. }
  1059. return buf;
  1060. }
  1061. static int
  1062. nfsd4_umh_cltrack_init(struct net *net)
  1063. {
  1064. int ret;
  1065. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1066. char *grace_start = nfsd4_cltrack_grace_start(nn->boot_time);
  1067. /* XXX: The usermode helper s not working in container yet. */
  1068. if (net != &init_net) {
  1069. pr_warn("NFSD: attempt to initialize umh client tracking in a container ignored.\n");
  1070. kfree(grace_start);
  1071. return -EINVAL;
  1072. }
  1073. ret = nfsd4_umh_cltrack_upcall("init", NULL, grace_start, NULL);
  1074. kfree(grace_start);
  1075. return ret;
  1076. }
  1077. static void
  1078. nfsd4_cltrack_upcall_lock(struct nfs4_client *clp)
  1079. {
  1080. wait_on_bit_lock(&clp->cl_flags, NFSD4_CLIENT_UPCALL_LOCK,
  1081. TASK_UNINTERRUPTIBLE);
  1082. }
  1083. static void
  1084. nfsd4_cltrack_upcall_unlock(struct nfs4_client *clp)
  1085. {
  1086. smp_mb__before_atomic();
  1087. clear_bit(NFSD4_CLIENT_UPCALL_LOCK, &clp->cl_flags);
  1088. smp_mb__after_atomic();
  1089. wake_up_bit(&clp->cl_flags, NFSD4_CLIENT_UPCALL_LOCK);
  1090. }
  1091. static void
  1092. nfsd4_umh_cltrack_create(struct nfs4_client *clp)
  1093. {
  1094. char *hexid, *has_session, *grace_start;
  1095. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1096. /*
  1097. * With v4.0 clients, there's little difference in outcome between a
  1098. * create and check operation, and we can end up calling into this
  1099. * function multiple times per client (once for each openowner). So,
  1100. * for v4.0 clients skip upcalling once the client has been recorded
  1101. * on stable storage.
  1102. *
  1103. * For v4.1+ clients, the outcome of the two operations is different,
  1104. * so we must ensure that we upcall for the create operation. v4.1+
  1105. * clients call this on RECLAIM_COMPLETE though, so we should only end
  1106. * up doing a single create upcall per client.
  1107. */
  1108. if (clp->cl_minorversion == 0 &&
  1109. test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  1110. return;
  1111. hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
  1112. if (!hexid) {
  1113. dprintk("%s: can't allocate memory for upcall!\n", __func__);
  1114. return;
  1115. }
  1116. has_session = nfsd4_cltrack_client_has_session(clp);
  1117. grace_start = nfsd4_cltrack_grace_start(nn->boot_time);
  1118. nfsd4_cltrack_upcall_lock(clp);
  1119. if (!nfsd4_umh_cltrack_upcall("create", hexid, has_session, grace_start))
  1120. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  1121. nfsd4_cltrack_upcall_unlock(clp);
  1122. kfree(has_session);
  1123. kfree(grace_start);
  1124. kfree(hexid);
  1125. }
  1126. static void
  1127. nfsd4_umh_cltrack_remove(struct nfs4_client *clp)
  1128. {
  1129. char *hexid;
  1130. if (!test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  1131. return;
  1132. hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
  1133. if (!hexid) {
  1134. dprintk("%s: can't allocate memory for upcall!\n", __func__);
  1135. return;
  1136. }
  1137. nfsd4_cltrack_upcall_lock(clp);
  1138. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags) &&
  1139. nfsd4_umh_cltrack_upcall("remove", hexid, NULL, NULL) == 0)
  1140. clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  1141. nfsd4_cltrack_upcall_unlock(clp);
  1142. kfree(hexid);
  1143. }
  1144. static int
  1145. nfsd4_umh_cltrack_check(struct nfs4_client *clp)
  1146. {
  1147. int ret;
  1148. char *hexid, *has_session, *legacy;
  1149. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  1150. return 0;
  1151. hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
  1152. if (!hexid) {
  1153. dprintk("%s: can't allocate memory for upcall!\n", __func__);
  1154. return -ENOMEM;
  1155. }
  1156. has_session = nfsd4_cltrack_client_has_session(clp);
  1157. legacy = nfsd4_cltrack_legacy_recdir(&clp->cl_name);
  1158. nfsd4_cltrack_upcall_lock(clp);
  1159. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) {
  1160. ret = 0;
  1161. } else {
  1162. ret = nfsd4_umh_cltrack_upcall("check", hexid, has_session, legacy);
  1163. if (ret == 0)
  1164. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  1165. }
  1166. nfsd4_cltrack_upcall_unlock(clp);
  1167. kfree(has_session);
  1168. kfree(legacy);
  1169. kfree(hexid);
  1170. return ret;
  1171. }
  1172. static void
  1173. nfsd4_umh_cltrack_grace_done(struct nfsd_net *nn)
  1174. {
  1175. char *legacy;
  1176. char timestr[22]; /* FIXME: better way to determine max size? */
  1177. sprintf(timestr, "%ld", nn->boot_time);
  1178. legacy = nfsd4_cltrack_legacy_topdir();
  1179. nfsd4_umh_cltrack_upcall("gracedone", timestr, legacy, NULL);
  1180. kfree(legacy);
  1181. }
  1182. static const struct nfsd4_client_tracking_ops nfsd4_umh_tracking_ops = {
  1183. .init = nfsd4_umh_cltrack_init,
  1184. .exit = NULL,
  1185. .create = nfsd4_umh_cltrack_create,
  1186. .remove = nfsd4_umh_cltrack_remove,
  1187. .check = nfsd4_umh_cltrack_check,
  1188. .grace_done = nfsd4_umh_cltrack_grace_done,
  1189. };
  1190. int
  1191. nfsd4_client_tracking_init(struct net *net)
  1192. {
  1193. int status;
  1194. struct path path;
  1195. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1196. /* just run the init if it the method is already decided */
  1197. if (nn->client_tracking_ops)
  1198. goto do_init;
  1199. /*
  1200. * First, try a UMH upcall. It should succeed or fail quickly, so
  1201. * there's little harm in trying that first.
  1202. */
  1203. nn->client_tracking_ops = &nfsd4_umh_tracking_ops;
  1204. status = nn->client_tracking_ops->init(net);
  1205. if (!status)
  1206. return status;
  1207. /*
  1208. * See if the recoverydir exists and is a directory. If it is,
  1209. * then use the legacy ops.
  1210. */
  1211. nn->client_tracking_ops = &nfsd4_legacy_tracking_ops;
  1212. status = kern_path(nfs4_recoverydir(), LOOKUP_FOLLOW, &path);
  1213. if (!status) {
  1214. status = d_is_dir(path.dentry);
  1215. path_put(&path);
  1216. if (status)
  1217. goto do_init;
  1218. }
  1219. /* Finally, try to use nfsdcld */
  1220. nn->client_tracking_ops = &nfsd4_cld_tracking_ops;
  1221. printk(KERN_WARNING "NFSD: the nfsdcld client tracking upcall will be "
  1222. "removed in 3.10. Please transition to using "
  1223. "nfsdcltrack.\n");
  1224. do_init:
  1225. status = nn->client_tracking_ops->init(net);
  1226. if (status) {
  1227. printk(KERN_WARNING "NFSD: Unable to initialize client "
  1228. "recovery tracking! (%d)\n", status);
  1229. nn->client_tracking_ops = NULL;
  1230. }
  1231. return status;
  1232. }
  1233. void
  1234. nfsd4_client_tracking_exit(struct net *net)
  1235. {
  1236. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1237. if (nn->client_tracking_ops) {
  1238. if (nn->client_tracking_ops->exit)
  1239. nn->client_tracking_ops->exit(net);
  1240. nn->client_tracking_ops = NULL;
  1241. }
  1242. }
  1243. void
  1244. nfsd4_client_record_create(struct nfs4_client *clp)
  1245. {
  1246. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1247. if (nn->client_tracking_ops)
  1248. nn->client_tracking_ops->create(clp);
  1249. }
  1250. void
  1251. nfsd4_client_record_remove(struct nfs4_client *clp)
  1252. {
  1253. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1254. if (nn->client_tracking_ops)
  1255. nn->client_tracking_ops->remove(clp);
  1256. }
  1257. int
  1258. nfsd4_client_record_check(struct nfs4_client *clp)
  1259. {
  1260. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1261. if (nn->client_tracking_ops)
  1262. return nn->client_tracking_ops->check(clp);
  1263. return -EOPNOTSUPP;
  1264. }
  1265. void
  1266. nfsd4_record_grace_done(struct nfsd_net *nn)
  1267. {
  1268. if (nn->client_tracking_ops)
  1269. nn->client_tracking_ops->grace_done(nn);
  1270. }
  1271. static int
  1272. rpc_pipefs_event(struct notifier_block *nb, unsigned long event, void *ptr)
  1273. {
  1274. struct super_block *sb = ptr;
  1275. struct net *net = sb->s_fs_info;
  1276. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1277. struct cld_net *cn = nn->cld_net;
  1278. struct dentry *dentry;
  1279. int ret = 0;
  1280. if (!try_module_get(THIS_MODULE))
  1281. return 0;
  1282. if (!cn) {
  1283. module_put(THIS_MODULE);
  1284. return 0;
  1285. }
  1286. switch (event) {
  1287. case RPC_PIPEFS_MOUNT:
  1288. dentry = nfsd4_cld_register_sb(sb, cn->cn_pipe);
  1289. if (IS_ERR(dentry)) {
  1290. ret = PTR_ERR(dentry);
  1291. break;
  1292. }
  1293. cn->cn_pipe->dentry = dentry;
  1294. break;
  1295. case RPC_PIPEFS_UMOUNT:
  1296. if (cn->cn_pipe->dentry)
  1297. nfsd4_cld_unregister_sb(cn->cn_pipe);
  1298. break;
  1299. default:
  1300. ret = -ENOTSUPP;
  1301. break;
  1302. }
  1303. module_put(THIS_MODULE);
  1304. return ret;
  1305. }
  1306. static struct notifier_block nfsd4_cld_block = {
  1307. .notifier_call = rpc_pipefs_event,
  1308. };
  1309. int
  1310. register_cld_notifier(void)
  1311. {
  1312. return rpc_pipefs_notifier_register(&nfsd4_cld_block);
  1313. }
  1314. void
  1315. unregister_cld_notifier(void)
  1316. {
  1317. rpc_pipefs_notifier_unregister(&nfsd4_cld_block);
  1318. }