nfs4recover.c 36 KB

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