nfs4recover.c 35 KB

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