nfs4recover.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  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(&d_inode(dir)->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 (d_really_is_positive(dentry))
  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(d_inode(dir), dentry, S_IRWXU);
  185. out_put:
  186. dput(dentry);
  187. out_unlock:
  188. mutex_unlock(&d_inode(dir)->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(&d_inode(dir)->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(&d_inode(dir)->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(&d_inode(dir)->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 (d_really_is_negative(dentry))
  287. goto out;
  288. status = vfs_rmdir(d_inode(dir), dentry);
  289. out:
  290. dput(dentry);
  291. out_unlock:
  292. mutex_unlock(&d_inode(dir)->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(d_inode(parent), 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. }