nfs4recover.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167
  1. /*
  2. * Copyright (c) 2004 The Regents of the University of Michigan.
  3. * Copyright (c) 2012 Jeff Layton <jlayton@redhat.com>
  4. * All rights reserved.
  5. *
  6. * Andy Adamson <andros@citi.umich.edu>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  28. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. */
  34. #include <crypto/hash.h>
  35. #include <linux/file.h>
  36. #include <linux/slab.h>
  37. #include <linux/namei.h>
  38. #include <linux/sched.h>
  39. #include <linux/fs.h>
  40. #include <linux/module.h>
  41. #include <net/net_namespace.h>
  42. #include <linux/sunrpc/rpc_pipe_fs.h>
  43. #include <linux/sunrpc/clnt.h>
  44. #include <linux/nfsd/cld.h>
  45. #include "nfsd.h"
  46. #include "state.h"
  47. #include "vfs.h"
  48. #include "netns.h"
  49. #define NFSDDBG_FACILITY NFSDDBG_PROC
  50. /* Declarations */
  51. struct nfsd4_client_tracking_ops {
  52. int (*init)(struct net *);
  53. void (*exit)(struct net *);
  54. void (*create)(struct nfs4_client *);
  55. void (*remove)(struct nfs4_client *);
  56. int (*check)(struct nfs4_client *);
  57. void (*grace_done)(struct nfsd_net *);
  58. uint8_t version;
  59. size_t msglen;
  60. };
  61. static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops;
  62. static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops_v2;
  63. /* Globals */
  64. static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
  65. static int
  66. nfs4_save_creds(const struct cred **original_creds)
  67. {
  68. struct cred *new;
  69. new = prepare_creds();
  70. if (!new)
  71. return -ENOMEM;
  72. new->fsuid = GLOBAL_ROOT_UID;
  73. new->fsgid = GLOBAL_ROOT_GID;
  74. *original_creds = override_creds(new);
  75. put_cred(new);
  76. return 0;
  77. }
  78. static void
  79. nfs4_reset_creds(const struct cred *original)
  80. {
  81. revert_creds(original);
  82. }
  83. static void
  84. md5_to_hex(char *out, char *md5)
  85. {
  86. int i;
  87. for (i=0; i<16; i++) {
  88. unsigned char c = md5[i];
  89. *out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
  90. *out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
  91. }
  92. *out = '\0';
  93. }
  94. static int
  95. nfs4_make_rec_clidname(char *dname, const struct xdr_netobj *clname)
  96. {
  97. struct xdr_netobj cksum;
  98. struct crypto_shash *tfm;
  99. int status;
  100. dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
  101. clname->len, clname->data);
  102. tfm = crypto_alloc_shash("md5", 0, 0);
  103. if (IS_ERR(tfm)) {
  104. status = PTR_ERR(tfm);
  105. goto out_no_tfm;
  106. }
  107. cksum.len = crypto_shash_digestsize(tfm);
  108. cksum.data = kmalloc(cksum.len, GFP_KERNEL);
  109. if (cksum.data == NULL) {
  110. status = -ENOMEM;
  111. goto out;
  112. }
  113. status = crypto_shash_tfm_digest(tfm, clname->data, clname->len,
  114. cksum.data);
  115. if (status)
  116. goto out;
  117. md5_to_hex(dname, cksum.data);
  118. status = 0;
  119. out:
  120. kfree(cksum.data);
  121. crypto_free_shash(tfm);
  122. out_no_tfm:
  123. return status;
  124. }
  125. /*
  126. * If we had an error generating the recdir name for the legacy tracker
  127. * then warn the admin. If the error doesn't appear to be transient,
  128. * then disable recovery tracking.
  129. */
  130. static void
  131. legacy_recdir_name_error(struct nfs4_client *clp, int error)
  132. {
  133. printk(KERN_ERR "NFSD: unable to generate recoverydir "
  134. "name (%d).\n", error);
  135. /*
  136. * if the algorithm just doesn't exist, then disable the recovery
  137. * tracker altogether. The crypto libs will generally return this if
  138. * FIPS is enabled as well.
  139. */
  140. if (error == -ENOENT) {
  141. printk(KERN_ERR "NFSD: disabling legacy clientid tracking. "
  142. "Reboot recovery will not function correctly!\n");
  143. nfsd4_client_tracking_exit(clp->net);
  144. }
  145. }
  146. static void
  147. __nfsd4_create_reclaim_record_grace(struct nfs4_client *clp,
  148. const char *dname, int len, struct nfsd_net *nn)
  149. {
  150. struct xdr_netobj name;
  151. struct xdr_netobj princhash = { .len = 0, .data = NULL };
  152. struct nfs4_client_reclaim *crp;
  153. name.data = kmemdup(dname, len, GFP_KERNEL);
  154. if (!name.data) {
  155. dprintk("%s: failed to allocate memory for name.data!\n",
  156. __func__);
  157. return;
  158. }
  159. name.len = len;
  160. crp = nfs4_client_to_reclaim(name, princhash, nn);
  161. if (!crp) {
  162. kfree(name.data);
  163. return;
  164. }
  165. crp->cr_clp = clp;
  166. }
  167. static void
  168. nfsd4_create_clid_dir(struct nfs4_client *clp)
  169. {
  170. const struct cred *original_cred;
  171. char dname[HEXDIR_LEN];
  172. struct dentry *dir, *dentry;
  173. int status;
  174. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  175. if (test_and_set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  176. return;
  177. if (!nn->rec_file)
  178. return;
  179. status = nfs4_make_rec_clidname(dname, &clp->cl_name);
  180. if (status)
  181. return legacy_recdir_name_error(clp, status);
  182. status = nfs4_save_creds(&original_cred);
  183. if (status < 0)
  184. return;
  185. status = mnt_want_write_file(nn->rec_file);
  186. if (status)
  187. goto out_creds;
  188. dir = nn->rec_file->f_path.dentry;
  189. /* lock the parent */
  190. inode_lock(d_inode(dir));
  191. dentry = lookup_one_len(dname, dir, HEXDIR_LEN-1);
  192. if (IS_ERR(dentry)) {
  193. status = PTR_ERR(dentry);
  194. goto out_unlock;
  195. }
  196. if (d_really_is_positive(dentry))
  197. /*
  198. * In the 4.1 case, where we're called from
  199. * reclaim_complete(), records from the previous reboot
  200. * may still be left, so this is OK.
  201. *
  202. * In the 4.0 case, we should never get here; but we may
  203. * as well be forgiving and just succeed silently.
  204. */
  205. goto out_put;
  206. status = vfs_mkdir(d_inode(dir), dentry, S_IRWXU);
  207. out_put:
  208. dput(dentry);
  209. out_unlock:
  210. inode_unlock(d_inode(dir));
  211. if (status == 0) {
  212. if (nn->in_grace)
  213. __nfsd4_create_reclaim_record_grace(clp, dname,
  214. HEXDIR_LEN, nn);
  215. vfs_fsync(nn->rec_file, 0);
  216. } else {
  217. printk(KERN_ERR "NFSD: failed to write recovery record"
  218. " (err %d); please check that %s exists"
  219. " and is writeable", status,
  220. user_recovery_dirname);
  221. }
  222. mnt_drop_write_file(nn->rec_file);
  223. out_creds:
  224. nfs4_reset_creds(original_cred);
  225. }
  226. typedef int (recdir_func)(struct dentry *, struct dentry *, struct nfsd_net *);
  227. struct name_list {
  228. char name[HEXDIR_LEN];
  229. struct list_head list;
  230. };
  231. struct nfs4_dir_ctx {
  232. struct dir_context ctx;
  233. struct list_head names;
  234. };
  235. static int
  236. nfsd4_build_namelist(struct dir_context *__ctx, const char *name, int namlen,
  237. loff_t offset, u64 ino, unsigned int d_type)
  238. {
  239. struct nfs4_dir_ctx *ctx =
  240. container_of(__ctx, struct nfs4_dir_ctx, ctx);
  241. struct name_list *entry;
  242. if (namlen != HEXDIR_LEN - 1)
  243. return 0;
  244. entry = kmalloc(sizeof(struct name_list), GFP_KERNEL);
  245. if (entry == NULL)
  246. return -ENOMEM;
  247. memcpy(entry->name, name, HEXDIR_LEN - 1);
  248. entry->name[HEXDIR_LEN - 1] = '\0';
  249. list_add(&entry->list, &ctx->names);
  250. return 0;
  251. }
  252. static int
  253. nfsd4_list_rec_dir(recdir_func *f, struct nfsd_net *nn)
  254. {
  255. const struct cred *original_cred;
  256. struct dentry *dir = nn->rec_file->f_path.dentry;
  257. struct nfs4_dir_ctx ctx = {
  258. .ctx.actor = nfsd4_build_namelist,
  259. .names = LIST_HEAD_INIT(ctx.names)
  260. };
  261. struct name_list *entry, *tmp;
  262. int status;
  263. status = nfs4_save_creds(&original_cred);
  264. if (status < 0)
  265. return status;
  266. status = vfs_llseek(nn->rec_file, 0, SEEK_SET);
  267. if (status < 0) {
  268. nfs4_reset_creds(original_cred);
  269. return status;
  270. }
  271. status = iterate_dir(nn->rec_file, &ctx.ctx);
  272. inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
  273. list_for_each_entry_safe(entry, tmp, &ctx.names, list) {
  274. if (!status) {
  275. struct dentry *dentry;
  276. dentry = lookup_one_len(entry->name, dir, HEXDIR_LEN-1);
  277. if (IS_ERR(dentry)) {
  278. status = PTR_ERR(dentry);
  279. break;
  280. }
  281. status = f(dir, dentry, nn);
  282. dput(dentry);
  283. }
  284. list_del(&entry->list);
  285. kfree(entry);
  286. }
  287. inode_unlock(d_inode(dir));
  288. nfs4_reset_creds(original_cred);
  289. list_for_each_entry_safe(entry, tmp, &ctx.names, list) {
  290. dprintk("NFSD: %s. Left entry %s\n", __func__, entry->name);
  291. list_del(&entry->list);
  292. kfree(entry);
  293. }
  294. return status;
  295. }
  296. static int
  297. nfsd4_unlink_clid_dir(char *name, int namlen, struct nfsd_net *nn)
  298. {
  299. struct dentry *dir, *dentry;
  300. int status;
  301. dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name);
  302. dir = nn->rec_file->f_path.dentry;
  303. inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
  304. dentry = lookup_one_len(name, dir, namlen);
  305. if (IS_ERR(dentry)) {
  306. status = PTR_ERR(dentry);
  307. goto out_unlock;
  308. }
  309. status = -ENOENT;
  310. if (d_really_is_negative(dentry))
  311. goto out;
  312. status = vfs_rmdir(d_inode(dir), dentry);
  313. out:
  314. dput(dentry);
  315. out_unlock:
  316. inode_unlock(d_inode(dir));
  317. return status;
  318. }
  319. static void
  320. __nfsd4_remove_reclaim_record_grace(const char *dname, int len,
  321. struct nfsd_net *nn)
  322. {
  323. struct xdr_netobj name;
  324. struct nfs4_client_reclaim *crp;
  325. name.data = kmemdup(dname, len, GFP_KERNEL);
  326. if (!name.data) {
  327. dprintk("%s: failed to allocate memory for name.data!\n",
  328. __func__);
  329. return;
  330. }
  331. name.len = len;
  332. crp = nfsd4_find_reclaim_client(name, nn);
  333. kfree(name.data);
  334. if (crp)
  335. nfs4_remove_reclaim_record(crp, nn);
  336. }
  337. static void
  338. nfsd4_remove_clid_dir(struct nfs4_client *clp)
  339. {
  340. const struct cred *original_cred;
  341. char dname[HEXDIR_LEN];
  342. int status;
  343. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  344. if (!nn->rec_file || !test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  345. return;
  346. status = nfs4_make_rec_clidname(dname, &clp->cl_name);
  347. if (status)
  348. return legacy_recdir_name_error(clp, status);
  349. status = mnt_want_write_file(nn->rec_file);
  350. if (status)
  351. goto out;
  352. clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  353. status = nfs4_save_creds(&original_cred);
  354. if (status < 0)
  355. goto out_drop_write;
  356. status = nfsd4_unlink_clid_dir(dname, HEXDIR_LEN-1, nn);
  357. nfs4_reset_creds(original_cred);
  358. if (status == 0) {
  359. vfs_fsync(nn->rec_file, 0);
  360. if (nn->in_grace)
  361. __nfsd4_remove_reclaim_record_grace(dname,
  362. HEXDIR_LEN, nn);
  363. }
  364. out_drop_write:
  365. mnt_drop_write_file(nn->rec_file);
  366. out:
  367. if (status)
  368. printk("NFSD: Failed to remove expired client state directory"
  369. " %.*s\n", HEXDIR_LEN, dname);
  370. }
  371. static int
  372. purge_old(struct dentry *parent, struct dentry *child, struct nfsd_net *nn)
  373. {
  374. int status;
  375. struct xdr_netobj name;
  376. if (child->d_name.len != HEXDIR_LEN - 1) {
  377. printk("%s: illegal name %pd in recovery directory\n",
  378. __func__, child);
  379. /* Keep trying; maybe the others are OK: */
  380. return 0;
  381. }
  382. name.data = kmemdup_nul(child->d_name.name, child->d_name.len, GFP_KERNEL);
  383. if (!name.data) {
  384. dprintk("%s: failed to allocate memory for name.data!\n",
  385. __func__);
  386. goto out;
  387. }
  388. name.len = HEXDIR_LEN;
  389. if (nfs4_has_reclaimed_state(name, nn))
  390. goto out_free;
  391. status = vfs_rmdir(d_inode(parent), child);
  392. if (status)
  393. printk("failed to remove client recovery directory %pd\n",
  394. child);
  395. out_free:
  396. kfree(name.data);
  397. out:
  398. /* Keep trying, success or failure: */
  399. return 0;
  400. }
  401. static void
  402. nfsd4_recdir_purge_old(struct nfsd_net *nn)
  403. {
  404. int status;
  405. nn->in_grace = false;
  406. if (!nn->rec_file)
  407. return;
  408. status = mnt_want_write_file(nn->rec_file);
  409. if (status)
  410. goto out;
  411. status = nfsd4_list_rec_dir(purge_old, nn);
  412. if (status == 0)
  413. vfs_fsync(nn->rec_file, 0);
  414. mnt_drop_write_file(nn->rec_file);
  415. out:
  416. nfs4_release_reclaim(nn);
  417. if (status)
  418. printk("nfsd4: failed to purge old clients from recovery"
  419. " directory %pD\n", nn->rec_file);
  420. }
  421. static int
  422. load_recdir(struct dentry *parent, struct dentry *child, struct nfsd_net *nn)
  423. {
  424. struct xdr_netobj name;
  425. struct xdr_netobj princhash = { .len = 0, .data = NULL };
  426. if (child->d_name.len != HEXDIR_LEN - 1) {
  427. printk("%s: illegal name %pd in recovery directory\n",
  428. __func__, child);
  429. /* Keep trying; maybe the others are OK: */
  430. return 0;
  431. }
  432. name.data = kmemdup_nul(child->d_name.name, child->d_name.len, GFP_KERNEL);
  433. if (!name.data) {
  434. dprintk("%s: failed to allocate memory for name.data!\n",
  435. __func__);
  436. goto out;
  437. }
  438. name.len = HEXDIR_LEN;
  439. if (!nfs4_client_to_reclaim(name, princhash, nn))
  440. kfree(name.data);
  441. out:
  442. return 0;
  443. }
  444. static int
  445. nfsd4_recdir_load(struct net *net) {
  446. int status;
  447. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  448. if (!nn->rec_file)
  449. return 0;
  450. status = nfsd4_list_rec_dir(load_recdir, nn);
  451. if (status)
  452. printk("nfsd4: failed loading clients from recovery"
  453. " directory %pD\n", nn->rec_file);
  454. return status;
  455. }
  456. /*
  457. * Hold reference to the recovery directory.
  458. */
  459. static int
  460. nfsd4_init_recdir(struct net *net)
  461. {
  462. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  463. const struct cred *original_cred;
  464. int status;
  465. printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
  466. user_recovery_dirname);
  467. BUG_ON(nn->rec_file);
  468. status = nfs4_save_creds(&original_cred);
  469. if (status < 0) {
  470. printk("NFSD: Unable to change credentials to find recovery"
  471. " directory: error %d\n",
  472. status);
  473. return status;
  474. }
  475. nn->rec_file = filp_open(user_recovery_dirname, O_RDONLY | O_DIRECTORY, 0);
  476. if (IS_ERR(nn->rec_file)) {
  477. printk("NFSD: unable to find recovery directory %s\n",
  478. user_recovery_dirname);
  479. status = PTR_ERR(nn->rec_file);
  480. nn->rec_file = NULL;
  481. }
  482. nfs4_reset_creds(original_cred);
  483. if (!status)
  484. nn->in_grace = true;
  485. return status;
  486. }
  487. static void
  488. nfsd4_shutdown_recdir(struct net *net)
  489. {
  490. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  491. if (!nn->rec_file)
  492. return;
  493. fput(nn->rec_file);
  494. nn->rec_file = NULL;
  495. }
  496. static int
  497. nfs4_legacy_state_init(struct net *net)
  498. {
  499. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  500. int i;
  501. nn->reclaim_str_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
  502. sizeof(struct list_head),
  503. GFP_KERNEL);
  504. if (!nn->reclaim_str_hashtbl)
  505. return -ENOMEM;
  506. for (i = 0; i < CLIENT_HASH_SIZE; i++)
  507. INIT_LIST_HEAD(&nn->reclaim_str_hashtbl[i]);
  508. nn->reclaim_str_hashtbl_size = 0;
  509. return 0;
  510. }
  511. static void
  512. nfs4_legacy_state_shutdown(struct net *net)
  513. {
  514. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  515. kfree(nn->reclaim_str_hashtbl);
  516. }
  517. static int
  518. nfsd4_load_reboot_recovery_data(struct net *net)
  519. {
  520. int status;
  521. status = nfsd4_init_recdir(net);
  522. if (status)
  523. return status;
  524. status = nfsd4_recdir_load(net);
  525. if (status)
  526. nfsd4_shutdown_recdir(net);
  527. return status;
  528. }
  529. static int
  530. nfsd4_legacy_tracking_init(struct net *net)
  531. {
  532. int status;
  533. /* XXX: The legacy code won't work in a container */
  534. if (net != &init_net) {
  535. pr_warn("NFSD: attempt to initialize legacy client tracking in a container ignored.\n");
  536. return -EINVAL;
  537. }
  538. status = nfs4_legacy_state_init(net);
  539. if (status)
  540. return status;
  541. status = nfsd4_load_reboot_recovery_data(net);
  542. if (status)
  543. goto err;
  544. printk("NFSD: Using legacy client tracking operations.\n");
  545. return 0;
  546. err:
  547. nfs4_legacy_state_shutdown(net);
  548. return status;
  549. }
  550. static void
  551. nfsd4_legacy_tracking_exit(struct net *net)
  552. {
  553. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  554. nfs4_release_reclaim(nn);
  555. nfsd4_shutdown_recdir(net);
  556. nfs4_legacy_state_shutdown(net);
  557. }
  558. /*
  559. * Change the NFSv4 recovery directory to recdir.
  560. */
  561. int
  562. nfs4_reset_recoverydir(char *recdir)
  563. {
  564. int status;
  565. struct path path;
  566. status = kern_path(recdir, LOOKUP_FOLLOW, &path);
  567. if (status)
  568. return status;
  569. status = -ENOTDIR;
  570. if (d_is_dir(path.dentry)) {
  571. strcpy(user_recovery_dirname, recdir);
  572. status = 0;
  573. }
  574. path_put(&path);
  575. return status;
  576. }
  577. char *
  578. nfs4_recoverydir(void)
  579. {
  580. return user_recovery_dirname;
  581. }
  582. static int
  583. nfsd4_check_legacy_client(struct nfs4_client *clp)
  584. {
  585. int status;
  586. char dname[HEXDIR_LEN];
  587. struct nfs4_client_reclaim *crp;
  588. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  589. struct xdr_netobj name;
  590. /* did we already find that this client is stable? */
  591. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  592. return 0;
  593. status = nfs4_make_rec_clidname(dname, &clp->cl_name);
  594. if (status) {
  595. legacy_recdir_name_error(clp, status);
  596. return status;
  597. }
  598. /* look for it in the reclaim hashtable otherwise */
  599. name.data = kmemdup(dname, HEXDIR_LEN, GFP_KERNEL);
  600. if (!name.data) {
  601. dprintk("%s: failed to allocate memory for name.data!\n",
  602. __func__);
  603. goto out_enoent;
  604. }
  605. name.len = HEXDIR_LEN;
  606. crp = nfsd4_find_reclaim_client(name, nn);
  607. kfree(name.data);
  608. if (crp) {
  609. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  610. crp->cr_clp = clp;
  611. return 0;
  612. }
  613. out_enoent:
  614. return -ENOENT;
  615. }
  616. static const struct nfsd4_client_tracking_ops nfsd4_legacy_tracking_ops = {
  617. .init = nfsd4_legacy_tracking_init,
  618. .exit = nfsd4_legacy_tracking_exit,
  619. .create = nfsd4_create_clid_dir,
  620. .remove = nfsd4_remove_clid_dir,
  621. .check = nfsd4_check_legacy_client,
  622. .grace_done = nfsd4_recdir_purge_old,
  623. .version = 1,
  624. .msglen = 0,
  625. };
  626. /* Globals */
  627. #define NFSD_PIPE_DIR "nfsd"
  628. #define NFSD_CLD_PIPE "cld"
  629. /* per-net-ns structure for holding cld upcall info */
  630. struct cld_net {
  631. struct rpc_pipe *cn_pipe;
  632. spinlock_t cn_lock;
  633. struct list_head cn_list;
  634. unsigned int cn_xid;
  635. bool cn_has_legacy;
  636. struct crypto_shash *cn_tfm;
  637. };
  638. struct cld_upcall {
  639. struct list_head cu_list;
  640. struct cld_net *cu_net;
  641. struct completion cu_done;
  642. union {
  643. struct cld_msg_hdr cu_hdr;
  644. struct cld_msg cu_msg;
  645. struct cld_msg_v2 cu_msg_v2;
  646. } cu_u;
  647. };
  648. static int
  649. __cld_pipe_upcall(struct rpc_pipe *pipe, void *cmsg, struct nfsd_net *nn)
  650. {
  651. int ret;
  652. struct rpc_pipe_msg msg;
  653. struct cld_upcall *cup = container_of(cmsg, struct cld_upcall, cu_u);
  654. memset(&msg, 0, sizeof(msg));
  655. msg.data = cmsg;
  656. msg.len = nn->client_tracking_ops->msglen;
  657. ret = rpc_queue_upcall(pipe, &msg);
  658. if (ret < 0) {
  659. goto out;
  660. }
  661. wait_for_completion(&cup->cu_done);
  662. if (msg.errno < 0)
  663. ret = msg.errno;
  664. out:
  665. return ret;
  666. }
  667. static int
  668. cld_pipe_upcall(struct rpc_pipe *pipe, void *cmsg, struct nfsd_net *nn)
  669. {
  670. int ret;
  671. /*
  672. * -EAGAIN occurs when pipe is closed and reopened while there are
  673. * upcalls queued.
  674. */
  675. do {
  676. ret = __cld_pipe_upcall(pipe, cmsg, nn);
  677. } while (ret == -EAGAIN);
  678. return ret;
  679. }
  680. static ssize_t
  681. __cld_pipe_inprogress_downcall(const struct cld_msg_v2 __user *cmsg,
  682. struct nfsd_net *nn)
  683. {
  684. uint8_t cmd, princhashlen;
  685. struct xdr_netobj name, princhash = { .len = 0, .data = NULL };
  686. uint16_t namelen;
  687. struct cld_net *cn = nn->cld_net;
  688. if (get_user(cmd, &cmsg->cm_cmd)) {
  689. dprintk("%s: error when copying cmd from userspace", __func__);
  690. return -EFAULT;
  691. }
  692. if (cmd == Cld_GraceStart) {
  693. if (nn->client_tracking_ops->version >= 2) {
  694. const struct cld_clntinfo __user *ci;
  695. ci = &cmsg->cm_u.cm_clntinfo;
  696. if (get_user(namelen, &ci->cc_name.cn_len))
  697. return -EFAULT;
  698. name.data = memdup_user(&ci->cc_name.cn_id, namelen);
  699. if (IS_ERR_OR_NULL(name.data))
  700. return -EFAULT;
  701. name.len = namelen;
  702. get_user(princhashlen, &ci->cc_princhash.cp_len);
  703. if (princhashlen > 0) {
  704. princhash.data = memdup_user(
  705. &ci->cc_princhash.cp_data,
  706. princhashlen);
  707. if (IS_ERR_OR_NULL(princhash.data))
  708. return -EFAULT;
  709. princhash.len = princhashlen;
  710. } else
  711. princhash.len = 0;
  712. } else {
  713. const struct cld_name __user *cnm;
  714. cnm = &cmsg->cm_u.cm_name;
  715. if (get_user(namelen, &cnm->cn_len))
  716. return -EFAULT;
  717. name.data = memdup_user(&cnm->cn_id, namelen);
  718. if (IS_ERR_OR_NULL(name.data))
  719. return -EFAULT;
  720. name.len = namelen;
  721. }
  722. if (name.len > 5 && memcmp(name.data, "hash:", 5) == 0) {
  723. name.len = name.len - 5;
  724. memmove(name.data, name.data + 5, name.len);
  725. cn->cn_has_legacy = true;
  726. }
  727. if (!nfs4_client_to_reclaim(name, princhash, nn)) {
  728. kfree(name.data);
  729. kfree(princhash.data);
  730. return -EFAULT;
  731. }
  732. return nn->client_tracking_ops->msglen;
  733. }
  734. return -EFAULT;
  735. }
  736. static ssize_t
  737. cld_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
  738. {
  739. struct cld_upcall *tmp, *cup;
  740. struct cld_msg_hdr __user *hdr = (struct cld_msg_hdr __user *)src;
  741. struct cld_msg_v2 __user *cmsg = (struct cld_msg_v2 __user *)src;
  742. uint32_t xid;
  743. struct nfsd_net *nn = net_generic(file_inode(filp)->i_sb->s_fs_info,
  744. nfsd_net_id);
  745. struct cld_net *cn = nn->cld_net;
  746. int16_t status;
  747. if (mlen != nn->client_tracking_ops->msglen) {
  748. dprintk("%s: got %zu bytes, expected %zu\n", __func__, mlen,
  749. nn->client_tracking_ops->msglen);
  750. return -EINVAL;
  751. }
  752. /* copy just the xid so we can try to find that */
  753. if (copy_from_user(&xid, &hdr->cm_xid, sizeof(xid)) != 0) {
  754. dprintk("%s: error when copying xid from userspace", __func__);
  755. return -EFAULT;
  756. }
  757. /*
  758. * copy the status so we know whether to remove the upcall from the
  759. * list (for -EINPROGRESS, we just want to make sure the xid is
  760. * valid, not remove the upcall from the list)
  761. */
  762. if (get_user(status, &hdr->cm_status)) {
  763. dprintk("%s: error when copying status from userspace", __func__);
  764. return -EFAULT;
  765. }
  766. /* walk the list and find corresponding xid */
  767. cup = NULL;
  768. spin_lock(&cn->cn_lock);
  769. list_for_each_entry(tmp, &cn->cn_list, cu_list) {
  770. if (get_unaligned(&tmp->cu_u.cu_hdr.cm_xid) == xid) {
  771. cup = tmp;
  772. if (status != -EINPROGRESS)
  773. list_del_init(&cup->cu_list);
  774. break;
  775. }
  776. }
  777. spin_unlock(&cn->cn_lock);
  778. /* couldn't find upcall? */
  779. if (!cup) {
  780. dprintk("%s: couldn't find upcall -- xid=%u\n", __func__, xid);
  781. return -EINVAL;
  782. }
  783. if (status == -EINPROGRESS)
  784. return __cld_pipe_inprogress_downcall(cmsg, nn);
  785. if (copy_from_user(&cup->cu_u.cu_msg_v2, src, mlen) != 0)
  786. return -EFAULT;
  787. complete(&cup->cu_done);
  788. return mlen;
  789. }
  790. static void
  791. cld_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  792. {
  793. struct cld_msg *cmsg = msg->data;
  794. struct cld_upcall *cup = container_of(cmsg, struct cld_upcall,
  795. cu_u.cu_msg);
  796. /* errno >= 0 means we got a downcall */
  797. if (msg->errno >= 0)
  798. return;
  799. complete(&cup->cu_done);
  800. }
  801. static const struct rpc_pipe_ops cld_upcall_ops = {
  802. .upcall = rpc_pipe_generic_upcall,
  803. .downcall = cld_pipe_downcall,
  804. .destroy_msg = cld_pipe_destroy_msg,
  805. };
  806. static struct dentry *
  807. nfsd4_cld_register_sb(struct super_block *sb, struct rpc_pipe *pipe)
  808. {
  809. struct dentry *dir, *dentry;
  810. dir = rpc_d_lookup_sb(sb, NFSD_PIPE_DIR);
  811. if (dir == NULL)
  812. return ERR_PTR(-ENOENT);
  813. dentry = rpc_mkpipe_dentry(dir, NFSD_CLD_PIPE, NULL, pipe);
  814. dput(dir);
  815. return dentry;
  816. }
  817. static void
  818. nfsd4_cld_unregister_sb(struct rpc_pipe *pipe)
  819. {
  820. if (pipe->dentry)
  821. rpc_unlink(pipe->dentry);
  822. }
  823. static struct dentry *
  824. nfsd4_cld_register_net(struct net *net, struct rpc_pipe *pipe)
  825. {
  826. struct super_block *sb;
  827. struct dentry *dentry;
  828. sb = rpc_get_sb_net(net);
  829. if (!sb)
  830. return NULL;
  831. dentry = nfsd4_cld_register_sb(sb, pipe);
  832. rpc_put_sb_net(net);
  833. return dentry;
  834. }
  835. static void
  836. nfsd4_cld_unregister_net(struct net *net, struct rpc_pipe *pipe)
  837. {
  838. struct super_block *sb;
  839. sb = rpc_get_sb_net(net);
  840. if (sb) {
  841. nfsd4_cld_unregister_sb(pipe);
  842. rpc_put_sb_net(net);
  843. }
  844. }
  845. /* Initialize rpc_pipefs pipe for communication with client tracking daemon */
  846. static int
  847. __nfsd4_init_cld_pipe(struct net *net)
  848. {
  849. int ret;
  850. struct dentry *dentry;
  851. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  852. struct cld_net *cn;
  853. if (nn->cld_net)
  854. return 0;
  855. cn = kzalloc(sizeof(*cn), GFP_KERNEL);
  856. if (!cn) {
  857. ret = -ENOMEM;
  858. goto err;
  859. }
  860. cn->cn_pipe = rpc_mkpipe_data(&cld_upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
  861. if (IS_ERR(cn->cn_pipe)) {
  862. ret = PTR_ERR(cn->cn_pipe);
  863. goto err;
  864. }
  865. spin_lock_init(&cn->cn_lock);
  866. INIT_LIST_HEAD(&cn->cn_list);
  867. dentry = nfsd4_cld_register_net(net, cn->cn_pipe);
  868. if (IS_ERR(dentry)) {
  869. ret = PTR_ERR(dentry);
  870. goto err_destroy_data;
  871. }
  872. cn->cn_pipe->dentry = dentry;
  873. cn->cn_has_legacy = false;
  874. nn->cld_net = cn;
  875. return 0;
  876. err_destroy_data:
  877. rpc_destroy_pipe_data(cn->cn_pipe);
  878. err:
  879. kfree(cn);
  880. printk(KERN_ERR "NFSD: unable to create nfsdcld upcall pipe (%d)\n",
  881. ret);
  882. return ret;
  883. }
  884. static int
  885. nfsd4_init_cld_pipe(struct net *net)
  886. {
  887. int status;
  888. status = __nfsd4_init_cld_pipe(net);
  889. if (!status)
  890. printk("NFSD: Using old nfsdcld client tracking operations.\n");
  891. return status;
  892. }
  893. static void
  894. nfsd4_remove_cld_pipe(struct net *net)
  895. {
  896. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  897. struct cld_net *cn = nn->cld_net;
  898. nfsd4_cld_unregister_net(net, cn->cn_pipe);
  899. rpc_destroy_pipe_data(cn->cn_pipe);
  900. if (cn->cn_tfm)
  901. crypto_free_shash(cn->cn_tfm);
  902. kfree(nn->cld_net);
  903. nn->cld_net = NULL;
  904. }
  905. static struct cld_upcall *
  906. alloc_cld_upcall(struct nfsd_net *nn)
  907. {
  908. struct cld_upcall *new, *tmp;
  909. struct cld_net *cn = nn->cld_net;
  910. new = kzalloc(sizeof(*new), GFP_KERNEL);
  911. if (!new)
  912. return new;
  913. /* FIXME: hard cap on number in flight? */
  914. restart_search:
  915. spin_lock(&cn->cn_lock);
  916. list_for_each_entry(tmp, &cn->cn_list, cu_list) {
  917. if (tmp->cu_u.cu_msg.cm_xid == cn->cn_xid) {
  918. cn->cn_xid++;
  919. spin_unlock(&cn->cn_lock);
  920. goto restart_search;
  921. }
  922. }
  923. init_completion(&new->cu_done);
  924. new->cu_u.cu_msg.cm_vers = nn->client_tracking_ops->version;
  925. put_unaligned(cn->cn_xid++, &new->cu_u.cu_msg.cm_xid);
  926. new->cu_net = cn;
  927. list_add(&new->cu_list, &cn->cn_list);
  928. spin_unlock(&cn->cn_lock);
  929. dprintk("%s: allocated xid %u\n", __func__, new->cu_u.cu_msg.cm_xid);
  930. return new;
  931. }
  932. static void
  933. free_cld_upcall(struct cld_upcall *victim)
  934. {
  935. struct cld_net *cn = victim->cu_net;
  936. spin_lock(&cn->cn_lock);
  937. list_del(&victim->cu_list);
  938. spin_unlock(&cn->cn_lock);
  939. kfree(victim);
  940. }
  941. /* Ask daemon to create a new record */
  942. static void
  943. nfsd4_cld_create(struct nfs4_client *clp)
  944. {
  945. int ret;
  946. struct cld_upcall *cup;
  947. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  948. struct cld_net *cn = nn->cld_net;
  949. /* Don't upcall if it's already stored */
  950. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  951. return;
  952. cup = alloc_cld_upcall(nn);
  953. if (!cup) {
  954. ret = -ENOMEM;
  955. goto out_err;
  956. }
  957. cup->cu_u.cu_msg.cm_cmd = Cld_Create;
  958. cup->cu_u.cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
  959. memcpy(cup->cu_u.cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
  960. clp->cl_name.len);
  961. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
  962. if (!ret) {
  963. ret = cup->cu_u.cu_msg.cm_status;
  964. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  965. }
  966. free_cld_upcall(cup);
  967. out_err:
  968. if (ret)
  969. printk(KERN_ERR "NFSD: Unable to create client "
  970. "record on stable storage: %d\n", ret);
  971. }
  972. /* Ask daemon to create a new record */
  973. static void
  974. nfsd4_cld_create_v2(struct nfs4_client *clp)
  975. {
  976. int ret;
  977. struct cld_upcall *cup;
  978. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  979. struct cld_net *cn = nn->cld_net;
  980. struct cld_msg_v2 *cmsg;
  981. struct crypto_shash *tfm = cn->cn_tfm;
  982. struct xdr_netobj cksum;
  983. char *principal = NULL;
  984. /* Don't upcall if it's already stored */
  985. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  986. return;
  987. cup = alloc_cld_upcall(nn);
  988. if (!cup) {
  989. ret = -ENOMEM;
  990. goto out_err;
  991. }
  992. cmsg = &cup->cu_u.cu_msg_v2;
  993. cmsg->cm_cmd = Cld_Create;
  994. cmsg->cm_u.cm_clntinfo.cc_name.cn_len = clp->cl_name.len;
  995. memcpy(cmsg->cm_u.cm_clntinfo.cc_name.cn_id, clp->cl_name.data,
  996. clp->cl_name.len);
  997. if (clp->cl_cred.cr_raw_principal)
  998. principal = clp->cl_cred.cr_raw_principal;
  999. else if (clp->cl_cred.cr_principal)
  1000. principal = clp->cl_cred.cr_principal;
  1001. if (principal) {
  1002. cksum.len = crypto_shash_digestsize(tfm);
  1003. cksum.data = kmalloc(cksum.len, GFP_KERNEL);
  1004. if (cksum.data == NULL) {
  1005. ret = -ENOMEM;
  1006. goto out;
  1007. }
  1008. ret = crypto_shash_tfm_digest(tfm, principal, strlen(principal),
  1009. cksum.data);
  1010. if (ret) {
  1011. kfree(cksum.data);
  1012. goto out;
  1013. }
  1014. cmsg->cm_u.cm_clntinfo.cc_princhash.cp_len = cksum.len;
  1015. memcpy(cmsg->cm_u.cm_clntinfo.cc_princhash.cp_data,
  1016. cksum.data, cksum.len);
  1017. kfree(cksum.data);
  1018. } else
  1019. cmsg->cm_u.cm_clntinfo.cc_princhash.cp_len = 0;
  1020. ret = cld_pipe_upcall(cn->cn_pipe, cmsg, nn);
  1021. if (!ret) {
  1022. ret = cmsg->cm_status;
  1023. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  1024. }
  1025. out:
  1026. free_cld_upcall(cup);
  1027. out_err:
  1028. if (ret)
  1029. pr_err("NFSD: Unable to create client record on stable storage: %d\n",
  1030. ret);
  1031. }
  1032. /* Ask daemon to create a new record */
  1033. static void
  1034. nfsd4_cld_remove(struct nfs4_client *clp)
  1035. {
  1036. int ret;
  1037. struct cld_upcall *cup;
  1038. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1039. struct cld_net *cn = nn->cld_net;
  1040. /* Don't upcall if it's already removed */
  1041. if (!test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  1042. return;
  1043. cup = alloc_cld_upcall(nn);
  1044. if (!cup) {
  1045. ret = -ENOMEM;
  1046. goto out_err;
  1047. }
  1048. cup->cu_u.cu_msg.cm_cmd = Cld_Remove;
  1049. cup->cu_u.cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
  1050. memcpy(cup->cu_u.cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
  1051. clp->cl_name.len);
  1052. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
  1053. if (!ret) {
  1054. ret = cup->cu_u.cu_msg.cm_status;
  1055. clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  1056. }
  1057. free_cld_upcall(cup);
  1058. out_err:
  1059. if (ret)
  1060. printk(KERN_ERR "NFSD: Unable to remove client "
  1061. "record from stable storage: %d\n", ret);
  1062. }
  1063. /*
  1064. * For older nfsdcld's that do not allow us to "slurp" the clients
  1065. * from the tracking database during startup.
  1066. *
  1067. * Check for presence of a record, and update its timestamp
  1068. */
  1069. static int
  1070. nfsd4_cld_check_v0(struct nfs4_client *clp)
  1071. {
  1072. int ret;
  1073. struct cld_upcall *cup;
  1074. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1075. struct cld_net *cn = nn->cld_net;
  1076. /* Don't upcall if one was already stored during this grace pd */
  1077. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  1078. return 0;
  1079. cup = alloc_cld_upcall(nn);
  1080. if (!cup) {
  1081. printk(KERN_ERR "NFSD: Unable to check client record on "
  1082. "stable storage: %d\n", -ENOMEM);
  1083. return -ENOMEM;
  1084. }
  1085. cup->cu_u.cu_msg.cm_cmd = Cld_Check;
  1086. cup->cu_u.cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
  1087. memcpy(cup->cu_u.cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
  1088. clp->cl_name.len);
  1089. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
  1090. if (!ret) {
  1091. ret = cup->cu_u.cu_msg.cm_status;
  1092. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  1093. }
  1094. free_cld_upcall(cup);
  1095. return ret;
  1096. }
  1097. /*
  1098. * For newer nfsdcld's that allow us to "slurp" the clients
  1099. * from the tracking database during startup.
  1100. *
  1101. * Check for presence of a record in the reclaim_str_hashtbl
  1102. */
  1103. static int
  1104. nfsd4_cld_check(struct nfs4_client *clp)
  1105. {
  1106. struct nfs4_client_reclaim *crp;
  1107. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1108. struct cld_net *cn = nn->cld_net;
  1109. int status;
  1110. char dname[HEXDIR_LEN];
  1111. struct xdr_netobj name;
  1112. /* did we already find that this client is stable? */
  1113. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  1114. return 0;
  1115. /* look for it in the reclaim hashtable otherwise */
  1116. crp = nfsd4_find_reclaim_client(clp->cl_name, nn);
  1117. if (crp)
  1118. goto found;
  1119. if (cn->cn_has_legacy) {
  1120. status = nfs4_make_rec_clidname(dname, &clp->cl_name);
  1121. if (status)
  1122. return -ENOENT;
  1123. name.data = kmemdup(dname, HEXDIR_LEN, GFP_KERNEL);
  1124. if (!name.data) {
  1125. dprintk("%s: failed to allocate memory for name.data!\n",
  1126. __func__);
  1127. return -ENOENT;
  1128. }
  1129. name.len = HEXDIR_LEN;
  1130. crp = nfsd4_find_reclaim_client(name, nn);
  1131. kfree(name.data);
  1132. if (crp)
  1133. goto found;
  1134. }
  1135. return -ENOENT;
  1136. found:
  1137. crp->cr_clp = clp;
  1138. return 0;
  1139. }
  1140. static int
  1141. nfsd4_cld_check_v2(struct nfs4_client *clp)
  1142. {
  1143. struct nfs4_client_reclaim *crp;
  1144. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1145. struct cld_net *cn = nn->cld_net;
  1146. int status;
  1147. char dname[HEXDIR_LEN];
  1148. struct xdr_netobj name;
  1149. struct crypto_shash *tfm = cn->cn_tfm;
  1150. struct xdr_netobj cksum;
  1151. char *principal = NULL;
  1152. /* did we already find that this client is stable? */
  1153. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  1154. return 0;
  1155. /* look for it in the reclaim hashtable otherwise */
  1156. crp = nfsd4_find_reclaim_client(clp->cl_name, nn);
  1157. if (crp)
  1158. goto found;
  1159. if (cn->cn_has_legacy) {
  1160. status = nfs4_make_rec_clidname(dname, &clp->cl_name);
  1161. if (status)
  1162. return -ENOENT;
  1163. name.data = kmemdup(dname, HEXDIR_LEN, GFP_KERNEL);
  1164. if (!name.data) {
  1165. dprintk("%s: failed to allocate memory for name.data\n",
  1166. __func__);
  1167. return -ENOENT;
  1168. }
  1169. name.len = HEXDIR_LEN;
  1170. crp = nfsd4_find_reclaim_client(name, nn);
  1171. kfree(name.data);
  1172. if (crp)
  1173. goto found;
  1174. }
  1175. return -ENOENT;
  1176. found:
  1177. if (crp->cr_princhash.len) {
  1178. if (clp->cl_cred.cr_raw_principal)
  1179. principal = clp->cl_cred.cr_raw_principal;
  1180. else if (clp->cl_cred.cr_principal)
  1181. principal = clp->cl_cred.cr_principal;
  1182. if (principal == NULL)
  1183. return -ENOENT;
  1184. cksum.len = crypto_shash_digestsize(tfm);
  1185. cksum.data = kmalloc(cksum.len, GFP_KERNEL);
  1186. if (cksum.data == NULL)
  1187. return -ENOENT;
  1188. status = crypto_shash_tfm_digest(tfm, principal,
  1189. strlen(principal), cksum.data);
  1190. if (status) {
  1191. kfree(cksum.data);
  1192. return -ENOENT;
  1193. }
  1194. if (memcmp(crp->cr_princhash.data, cksum.data,
  1195. crp->cr_princhash.len)) {
  1196. kfree(cksum.data);
  1197. return -ENOENT;
  1198. }
  1199. kfree(cksum.data);
  1200. }
  1201. crp->cr_clp = clp;
  1202. return 0;
  1203. }
  1204. static int
  1205. nfsd4_cld_grace_start(struct nfsd_net *nn)
  1206. {
  1207. int ret;
  1208. struct cld_upcall *cup;
  1209. struct cld_net *cn = nn->cld_net;
  1210. cup = alloc_cld_upcall(nn);
  1211. if (!cup) {
  1212. ret = -ENOMEM;
  1213. goto out_err;
  1214. }
  1215. cup->cu_u.cu_msg.cm_cmd = Cld_GraceStart;
  1216. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
  1217. if (!ret)
  1218. ret = cup->cu_u.cu_msg.cm_status;
  1219. free_cld_upcall(cup);
  1220. out_err:
  1221. if (ret)
  1222. dprintk("%s: Unable to get clients from userspace: %d\n",
  1223. __func__, ret);
  1224. return ret;
  1225. }
  1226. /* For older nfsdcld's that need cm_gracetime */
  1227. static void
  1228. nfsd4_cld_grace_done_v0(struct nfsd_net *nn)
  1229. {
  1230. int ret;
  1231. struct cld_upcall *cup;
  1232. struct cld_net *cn = nn->cld_net;
  1233. cup = alloc_cld_upcall(nn);
  1234. if (!cup) {
  1235. ret = -ENOMEM;
  1236. goto out_err;
  1237. }
  1238. cup->cu_u.cu_msg.cm_cmd = Cld_GraceDone;
  1239. cup->cu_u.cu_msg.cm_u.cm_gracetime = nn->boot_time;
  1240. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
  1241. if (!ret)
  1242. ret = cup->cu_u.cu_msg.cm_status;
  1243. free_cld_upcall(cup);
  1244. out_err:
  1245. if (ret)
  1246. printk(KERN_ERR "NFSD: Unable to end grace period: %d\n", ret);
  1247. }
  1248. /*
  1249. * For newer nfsdcld's that do not need cm_gracetime. We also need to call
  1250. * nfs4_release_reclaim() to clear out the reclaim_str_hashtbl.
  1251. */
  1252. static void
  1253. nfsd4_cld_grace_done(struct nfsd_net *nn)
  1254. {
  1255. int ret;
  1256. struct cld_upcall *cup;
  1257. struct cld_net *cn = nn->cld_net;
  1258. cup = alloc_cld_upcall(nn);
  1259. if (!cup) {
  1260. ret = -ENOMEM;
  1261. goto out_err;
  1262. }
  1263. cup->cu_u.cu_msg.cm_cmd = Cld_GraceDone;
  1264. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
  1265. if (!ret)
  1266. ret = cup->cu_u.cu_msg.cm_status;
  1267. free_cld_upcall(cup);
  1268. out_err:
  1269. nfs4_release_reclaim(nn);
  1270. if (ret)
  1271. printk(KERN_ERR "NFSD: Unable to end grace period: %d\n", ret);
  1272. }
  1273. static int
  1274. nfs4_cld_state_init(struct net *net)
  1275. {
  1276. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1277. int i;
  1278. nn->reclaim_str_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
  1279. sizeof(struct list_head),
  1280. GFP_KERNEL);
  1281. if (!nn->reclaim_str_hashtbl)
  1282. return -ENOMEM;
  1283. for (i = 0; i < CLIENT_HASH_SIZE; i++)
  1284. INIT_LIST_HEAD(&nn->reclaim_str_hashtbl[i]);
  1285. nn->reclaim_str_hashtbl_size = 0;
  1286. nn->track_reclaim_completes = true;
  1287. atomic_set(&nn->nr_reclaim_complete, 0);
  1288. return 0;
  1289. }
  1290. static void
  1291. nfs4_cld_state_shutdown(struct net *net)
  1292. {
  1293. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1294. nn->track_reclaim_completes = false;
  1295. kfree(nn->reclaim_str_hashtbl);
  1296. }
  1297. static bool
  1298. cld_running(struct nfsd_net *nn)
  1299. {
  1300. struct cld_net *cn = nn->cld_net;
  1301. struct rpc_pipe *pipe = cn->cn_pipe;
  1302. return pipe->nreaders || pipe->nwriters;
  1303. }
  1304. static int
  1305. nfsd4_cld_get_version(struct nfsd_net *nn)
  1306. {
  1307. int ret = 0;
  1308. struct cld_upcall *cup;
  1309. struct cld_net *cn = nn->cld_net;
  1310. uint8_t version;
  1311. cup = alloc_cld_upcall(nn);
  1312. if (!cup) {
  1313. ret = -ENOMEM;
  1314. goto out_err;
  1315. }
  1316. cup->cu_u.cu_msg.cm_cmd = Cld_GetVersion;
  1317. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
  1318. if (!ret) {
  1319. ret = cup->cu_u.cu_msg.cm_status;
  1320. if (ret)
  1321. goto out_free;
  1322. version = cup->cu_u.cu_msg.cm_u.cm_version;
  1323. dprintk("%s: userspace returned version %u\n",
  1324. __func__, version);
  1325. if (version < 1)
  1326. version = 1;
  1327. else if (version > CLD_UPCALL_VERSION)
  1328. version = CLD_UPCALL_VERSION;
  1329. switch (version) {
  1330. case 1:
  1331. nn->client_tracking_ops = &nfsd4_cld_tracking_ops;
  1332. break;
  1333. case 2:
  1334. nn->client_tracking_ops = &nfsd4_cld_tracking_ops_v2;
  1335. break;
  1336. default:
  1337. break;
  1338. }
  1339. }
  1340. out_free:
  1341. free_cld_upcall(cup);
  1342. out_err:
  1343. if (ret)
  1344. dprintk("%s: Unable to get version from userspace: %d\n",
  1345. __func__, ret);
  1346. return ret;
  1347. }
  1348. static int
  1349. nfsd4_cld_tracking_init(struct net *net)
  1350. {
  1351. int status;
  1352. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1353. bool running;
  1354. int retries = 10;
  1355. struct crypto_shash *tfm;
  1356. status = nfs4_cld_state_init(net);
  1357. if (status)
  1358. return status;
  1359. status = __nfsd4_init_cld_pipe(net);
  1360. if (status)
  1361. goto err_shutdown;
  1362. /*
  1363. * rpc pipe upcalls take 30 seconds to time out, so we don't want to
  1364. * queue an upcall unless we know that nfsdcld is running (because we
  1365. * want this to fail fast so that nfsd4_client_tracking_init() can try
  1366. * the next client tracking method). nfsdcld should already be running
  1367. * before nfsd is started, so the wait here is for nfsdcld to open the
  1368. * pipefs file we just created.
  1369. */
  1370. while (!(running = cld_running(nn)) && retries--)
  1371. msleep(100);
  1372. if (!running) {
  1373. status = -ETIMEDOUT;
  1374. goto err_remove;
  1375. }
  1376. tfm = crypto_alloc_shash("sha256", 0, 0);
  1377. if (IS_ERR(tfm)) {
  1378. status = PTR_ERR(tfm);
  1379. goto err_remove;
  1380. }
  1381. nn->cld_net->cn_tfm = tfm;
  1382. status = nfsd4_cld_get_version(nn);
  1383. if (status == -EOPNOTSUPP)
  1384. pr_warn("NFSD: nfsdcld GetVersion upcall failed. Please upgrade nfsdcld.\n");
  1385. status = nfsd4_cld_grace_start(nn);
  1386. if (status) {
  1387. if (status == -EOPNOTSUPP)
  1388. pr_warn("NFSD: nfsdcld GraceStart upcall failed. Please upgrade nfsdcld.\n");
  1389. nfs4_release_reclaim(nn);
  1390. goto err_remove;
  1391. } else
  1392. printk("NFSD: Using nfsdcld client tracking operations.\n");
  1393. return 0;
  1394. err_remove:
  1395. nfsd4_remove_cld_pipe(net);
  1396. err_shutdown:
  1397. nfs4_cld_state_shutdown(net);
  1398. return status;
  1399. }
  1400. static void
  1401. nfsd4_cld_tracking_exit(struct net *net)
  1402. {
  1403. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1404. nfs4_release_reclaim(nn);
  1405. nfsd4_remove_cld_pipe(net);
  1406. nfs4_cld_state_shutdown(net);
  1407. }
  1408. /* For older nfsdcld's */
  1409. static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops_v0 = {
  1410. .init = nfsd4_init_cld_pipe,
  1411. .exit = nfsd4_remove_cld_pipe,
  1412. .create = nfsd4_cld_create,
  1413. .remove = nfsd4_cld_remove,
  1414. .check = nfsd4_cld_check_v0,
  1415. .grace_done = nfsd4_cld_grace_done_v0,
  1416. .version = 1,
  1417. .msglen = sizeof(struct cld_msg),
  1418. };
  1419. /* For newer nfsdcld's */
  1420. static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops = {
  1421. .init = nfsd4_cld_tracking_init,
  1422. .exit = nfsd4_cld_tracking_exit,
  1423. .create = nfsd4_cld_create,
  1424. .remove = nfsd4_cld_remove,
  1425. .check = nfsd4_cld_check,
  1426. .grace_done = nfsd4_cld_grace_done,
  1427. .version = 1,
  1428. .msglen = sizeof(struct cld_msg),
  1429. };
  1430. /* v2 create/check ops include the principal, if available */
  1431. static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops_v2 = {
  1432. .init = nfsd4_cld_tracking_init,
  1433. .exit = nfsd4_cld_tracking_exit,
  1434. .create = nfsd4_cld_create_v2,
  1435. .remove = nfsd4_cld_remove,
  1436. .check = nfsd4_cld_check_v2,
  1437. .grace_done = nfsd4_cld_grace_done,
  1438. .version = 2,
  1439. .msglen = sizeof(struct cld_msg_v2),
  1440. };
  1441. /* upcall via usermodehelper */
  1442. static char cltrack_prog[PATH_MAX] = "/sbin/nfsdcltrack";
  1443. module_param_string(cltrack_prog, cltrack_prog, sizeof(cltrack_prog),
  1444. S_IRUGO|S_IWUSR);
  1445. MODULE_PARM_DESC(cltrack_prog, "Path to the nfsdcltrack upcall program");
  1446. static bool cltrack_legacy_disable;
  1447. module_param(cltrack_legacy_disable, bool, S_IRUGO|S_IWUSR);
  1448. MODULE_PARM_DESC(cltrack_legacy_disable,
  1449. "Disable legacy recoverydir conversion. Default: false");
  1450. #define LEGACY_TOPDIR_ENV_PREFIX "NFSDCLTRACK_LEGACY_TOPDIR="
  1451. #define LEGACY_RECDIR_ENV_PREFIX "NFSDCLTRACK_LEGACY_RECDIR="
  1452. #define HAS_SESSION_ENV_PREFIX "NFSDCLTRACK_CLIENT_HAS_SESSION="
  1453. #define GRACE_START_ENV_PREFIX "NFSDCLTRACK_GRACE_START="
  1454. static char *
  1455. nfsd4_cltrack_legacy_topdir(void)
  1456. {
  1457. int copied;
  1458. size_t len;
  1459. char *result;
  1460. if (cltrack_legacy_disable)
  1461. return NULL;
  1462. len = strlen(LEGACY_TOPDIR_ENV_PREFIX) +
  1463. strlen(nfs4_recoverydir()) + 1;
  1464. result = kmalloc(len, GFP_KERNEL);
  1465. if (!result)
  1466. return result;
  1467. copied = snprintf(result, len, LEGACY_TOPDIR_ENV_PREFIX "%s",
  1468. nfs4_recoverydir());
  1469. if (copied >= len) {
  1470. /* just return nothing if output was truncated */
  1471. kfree(result);
  1472. return NULL;
  1473. }
  1474. return result;
  1475. }
  1476. static char *
  1477. nfsd4_cltrack_legacy_recdir(const struct xdr_netobj *name)
  1478. {
  1479. int copied;
  1480. size_t len;
  1481. char *result;
  1482. if (cltrack_legacy_disable)
  1483. return NULL;
  1484. /* +1 is for '/' between "topdir" and "recdir" */
  1485. len = strlen(LEGACY_RECDIR_ENV_PREFIX) +
  1486. strlen(nfs4_recoverydir()) + 1 + HEXDIR_LEN;
  1487. result = kmalloc(len, GFP_KERNEL);
  1488. if (!result)
  1489. return result;
  1490. copied = snprintf(result, len, LEGACY_RECDIR_ENV_PREFIX "%s/",
  1491. nfs4_recoverydir());
  1492. if (copied > (len - HEXDIR_LEN)) {
  1493. /* just return nothing if output will be truncated */
  1494. kfree(result);
  1495. return NULL;
  1496. }
  1497. copied = nfs4_make_rec_clidname(result + copied, name);
  1498. if (copied) {
  1499. kfree(result);
  1500. return NULL;
  1501. }
  1502. return result;
  1503. }
  1504. static char *
  1505. nfsd4_cltrack_client_has_session(struct nfs4_client *clp)
  1506. {
  1507. int copied;
  1508. size_t len;
  1509. char *result;
  1510. /* prefix + Y/N character + terminating NULL */
  1511. len = strlen(HAS_SESSION_ENV_PREFIX) + 1 + 1;
  1512. result = kmalloc(len, GFP_KERNEL);
  1513. if (!result)
  1514. return result;
  1515. copied = snprintf(result, len, HAS_SESSION_ENV_PREFIX "%c",
  1516. clp->cl_minorversion ? 'Y' : 'N');
  1517. if (copied >= len) {
  1518. /* just return nothing if output was truncated */
  1519. kfree(result);
  1520. return NULL;
  1521. }
  1522. return result;
  1523. }
  1524. static char *
  1525. nfsd4_cltrack_grace_start(time64_t grace_start)
  1526. {
  1527. int copied;
  1528. size_t len;
  1529. char *result;
  1530. /* prefix + max width of int64_t string + terminating NULL */
  1531. len = strlen(GRACE_START_ENV_PREFIX) + 22 + 1;
  1532. result = kmalloc(len, GFP_KERNEL);
  1533. if (!result)
  1534. return result;
  1535. copied = snprintf(result, len, GRACE_START_ENV_PREFIX "%lld",
  1536. grace_start);
  1537. if (copied >= len) {
  1538. /* just return nothing if output was truncated */
  1539. kfree(result);
  1540. return NULL;
  1541. }
  1542. return result;
  1543. }
  1544. static int
  1545. nfsd4_umh_cltrack_upcall(char *cmd, char *arg, char *env0, char *env1)
  1546. {
  1547. char *envp[3];
  1548. char *argv[4];
  1549. int ret;
  1550. if (unlikely(!cltrack_prog[0])) {
  1551. dprintk("%s: cltrack_prog is disabled\n", __func__);
  1552. return -EACCES;
  1553. }
  1554. dprintk("%s: cmd: %s\n", __func__, cmd);
  1555. dprintk("%s: arg: %s\n", __func__, arg ? arg : "(null)");
  1556. dprintk("%s: env0: %s\n", __func__, env0 ? env0 : "(null)");
  1557. dprintk("%s: env1: %s\n", __func__, env1 ? env1 : "(null)");
  1558. envp[0] = env0;
  1559. envp[1] = env1;
  1560. envp[2] = NULL;
  1561. argv[0] = (char *)cltrack_prog;
  1562. argv[1] = cmd;
  1563. argv[2] = arg;
  1564. argv[3] = NULL;
  1565. ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
  1566. /*
  1567. * Disable the upcall mechanism if we're getting an ENOENT or EACCES
  1568. * error. The admin can re-enable it on the fly by using sysfs
  1569. * once the problem has been fixed.
  1570. */
  1571. if (ret == -ENOENT || ret == -EACCES) {
  1572. dprintk("NFSD: %s was not found or isn't executable (%d). "
  1573. "Setting cltrack_prog to blank string!",
  1574. cltrack_prog, ret);
  1575. cltrack_prog[0] = '\0';
  1576. }
  1577. dprintk("%s: %s return value: %d\n", __func__, cltrack_prog, ret);
  1578. return ret;
  1579. }
  1580. static char *
  1581. bin_to_hex_dup(const unsigned char *src, int srclen)
  1582. {
  1583. char *buf;
  1584. /* +1 for terminating NULL */
  1585. buf = kzalloc((srclen * 2) + 1, GFP_KERNEL);
  1586. if (!buf)
  1587. return buf;
  1588. bin2hex(buf, src, srclen);
  1589. return buf;
  1590. }
  1591. static int
  1592. nfsd4_umh_cltrack_init(struct net *net)
  1593. {
  1594. int ret;
  1595. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1596. char *grace_start = nfsd4_cltrack_grace_start(nn->boot_time);
  1597. /* XXX: The usermode helper s not working in container yet. */
  1598. if (net != &init_net) {
  1599. pr_warn("NFSD: attempt to initialize umh client tracking in a container ignored.\n");
  1600. kfree(grace_start);
  1601. return -EINVAL;
  1602. }
  1603. ret = nfsd4_umh_cltrack_upcall("init", NULL, grace_start, NULL);
  1604. kfree(grace_start);
  1605. if (!ret)
  1606. printk("NFSD: Using UMH upcall client tracking operations.\n");
  1607. return ret;
  1608. }
  1609. static void
  1610. nfsd4_cltrack_upcall_lock(struct nfs4_client *clp)
  1611. {
  1612. wait_on_bit_lock(&clp->cl_flags, NFSD4_CLIENT_UPCALL_LOCK,
  1613. TASK_UNINTERRUPTIBLE);
  1614. }
  1615. static void
  1616. nfsd4_cltrack_upcall_unlock(struct nfs4_client *clp)
  1617. {
  1618. smp_mb__before_atomic();
  1619. clear_bit(NFSD4_CLIENT_UPCALL_LOCK, &clp->cl_flags);
  1620. smp_mb__after_atomic();
  1621. wake_up_bit(&clp->cl_flags, NFSD4_CLIENT_UPCALL_LOCK);
  1622. }
  1623. static void
  1624. nfsd4_umh_cltrack_create(struct nfs4_client *clp)
  1625. {
  1626. char *hexid, *has_session, *grace_start;
  1627. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1628. /*
  1629. * With v4.0 clients, there's little difference in outcome between a
  1630. * create and check operation, and we can end up calling into this
  1631. * function multiple times per client (once for each openowner). So,
  1632. * for v4.0 clients skip upcalling once the client has been recorded
  1633. * on stable storage.
  1634. *
  1635. * For v4.1+ clients, the outcome of the two operations is different,
  1636. * so we must ensure that we upcall for the create operation. v4.1+
  1637. * clients call this on RECLAIM_COMPLETE though, so we should only end
  1638. * up doing a single create upcall per client.
  1639. */
  1640. if (clp->cl_minorversion == 0 &&
  1641. test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  1642. return;
  1643. hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
  1644. if (!hexid) {
  1645. dprintk("%s: can't allocate memory for upcall!\n", __func__);
  1646. return;
  1647. }
  1648. has_session = nfsd4_cltrack_client_has_session(clp);
  1649. grace_start = nfsd4_cltrack_grace_start(nn->boot_time);
  1650. nfsd4_cltrack_upcall_lock(clp);
  1651. if (!nfsd4_umh_cltrack_upcall("create", hexid, has_session, grace_start))
  1652. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  1653. nfsd4_cltrack_upcall_unlock(clp);
  1654. kfree(has_session);
  1655. kfree(grace_start);
  1656. kfree(hexid);
  1657. }
  1658. static void
  1659. nfsd4_umh_cltrack_remove(struct nfs4_client *clp)
  1660. {
  1661. char *hexid;
  1662. if (!test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  1663. return;
  1664. hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
  1665. if (!hexid) {
  1666. dprintk("%s: can't allocate memory for upcall!\n", __func__);
  1667. return;
  1668. }
  1669. nfsd4_cltrack_upcall_lock(clp);
  1670. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags) &&
  1671. nfsd4_umh_cltrack_upcall("remove", hexid, NULL, NULL) == 0)
  1672. clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  1673. nfsd4_cltrack_upcall_unlock(clp);
  1674. kfree(hexid);
  1675. }
  1676. static int
  1677. nfsd4_umh_cltrack_check(struct nfs4_client *clp)
  1678. {
  1679. int ret;
  1680. char *hexid, *has_session, *legacy;
  1681. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  1682. return 0;
  1683. hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
  1684. if (!hexid) {
  1685. dprintk("%s: can't allocate memory for upcall!\n", __func__);
  1686. return -ENOMEM;
  1687. }
  1688. has_session = nfsd4_cltrack_client_has_session(clp);
  1689. legacy = nfsd4_cltrack_legacy_recdir(&clp->cl_name);
  1690. nfsd4_cltrack_upcall_lock(clp);
  1691. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) {
  1692. ret = 0;
  1693. } else {
  1694. ret = nfsd4_umh_cltrack_upcall("check", hexid, has_session, legacy);
  1695. if (ret == 0)
  1696. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  1697. }
  1698. nfsd4_cltrack_upcall_unlock(clp);
  1699. kfree(has_session);
  1700. kfree(legacy);
  1701. kfree(hexid);
  1702. return ret;
  1703. }
  1704. static void
  1705. nfsd4_umh_cltrack_grace_done(struct nfsd_net *nn)
  1706. {
  1707. char *legacy;
  1708. char timestr[22]; /* FIXME: better way to determine max size? */
  1709. sprintf(timestr, "%lld", nn->boot_time);
  1710. legacy = nfsd4_cltrack_legacy_topdir();
  1711. nfsd4_umh_cltrack_upcall("gracedone", timestr, legacy, NULL);
  1712. kfree(legacy);
  1713. }
  1714. static const struct nfsd4_client_tracking_ops nfsd4_umh_tracking_ops = {
  1715. .init = nfsd4_umh_cltrack_init,
  1716. .exit = NULL,
  1717. .create = nfsd4_umh_cltrack_create,
  1718. .remove = nfsd4_umh_cltrack_remove,
  1719. .check = nfsd4_umh_cltrack_check,
  1720. .grace_done = nfsd4_umh_cltrack_grace_done,
  1721. .version = 1,
  1722. .msglen = 0,
  1723. };
  1724. int
  1725. nfsd4_client_tracking_init(struct net *net)
  1726. {
  1727. int status;
  1728. struct path path;
  1729. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1730. /* just run the init if it the method is already decided */
  1731. if (nn->client_tracking_ops)
  1732. goto do_init;
  1733. /* First, try to use nfsdcld */
  1734. nn->client_tracking_ops = &nfsd4_cld_tracking_ops;
  1735. status = nn->client_tracking_ops->init(net);
  1736. if (!status)
  1737. return status;
  1738. if (status != -ETIMEDOUT) {
  1739. nn->client_tracking_ops = &nfsd4_cld_tracking_ops_v0;
  1740. status = nn->client_tracking_ops->init(net);
  1741. if (!status)
  1742. return status;
  1743. }
  1744. /*
  1745. * Next, try the UMH upcall.
  1746. */
  1747. nn->client_tracking_ops = &nfsd4_umh_tracking_ops;
  1748. status = nn->client_tracking_ops->init(net);
  1749. if (!status)
  1750. return status;
  1751. /*
  1752. * Finally, See if the recoverydir exists and is a directory.
  1753. * If it is, then use the legacy ops.
  1754. */
  1755. nn->client_tracking_ops = &nfsd4_legacy_tracking_ops;
  1756. status = kern_path(nfs4_recoverydir(), LOOKUP_FOLLOW, &path);
  1757. if (!status) {
  1758. status = d_is_dir(path.dentry);
  1759. path_put(&path);
  1760. if (!status) {
  1761. status = -EINVAL;
  1762. goto out;
  1763. }
  1764. }
  1765. do_init:
  1766. status = nn->client_tracking_ops->init(net);
  1767. out:
  1768. if (status) {
  1769. printk(KERN_WARNING "NFSD: Unable to initialize client "
  1770. "recovery tracking! (%d)\n", status);
  1771. nn->client_tracking_ops = NULL;
  1772. }
  1773. return status;
  1774. }
  1775. void
  1776. nfsd4_client_tracking_exit(struct net *net)
  1777. {
  1778. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1779. if (nn->client_tracking_ops) {
  1780. if (nn->client_tracking_ops->exit)
  1781. nn->client_tracking_ops->exit(net);
  1782. nn->client_tracking_ops = NULL;
  1783. }
  1784. }
  1785. void
  1786. nfsd4_client_record_create(struct nfs4_client *clp)
  1787. {
  1788. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1789. if (nn->client_tracking_ops)
  1790. nn->client_tracking_ops->create(clp);
  1791. }
  1792. void
  1793. nfsd4_client_record_remove(struct nfs4_client *clp)
  1794. {
  1795. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1796. if (nn->client_tracking_ops)
  1797. nn->client_tracking_ops->remove(clp);
  1798. }
  1799. int
  1800. nfsd4_client_record_check(struct nfs4_client *clp)
  1801. {
  1802. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1803. if (nn->client_tracking_ops)
  1804. return nn->client_tracking_ops->check(clp);
  1805. return -EOPNOTSUPP;
  1806. }
  1807. void
  1808. nfsd4_record_grace_done(struct nfsd_net *nn)
  1809. {
  1810. if (nn->client_tracking_ops)
  1811. nn->client_tracking_ops->grace_done(nn);
  1812. }
  1813. static int
  1814. rpc_pipefs_event(struct notifier_block *nb, unsigned long event, void *ptr)
  1815. {
  1816. struct super_block *sb = ptr;
  1817. struct net *net = sb->s_fs_info;
  1818. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1819. struct cld_net *cn = nn->cld_net;
  1820. struct dentry *dentry;
  1821. int ret = 0;
  1822. if (!try_module_get(THIS_MODULE))
  1823. return 0;
  1824. if (!cn) {
  1825. module_put(THIS_MODULE);
  1826. return 0;
  1827. }
  1828. switch (event) {
  1829. case RPC_PIPEFS_MOUNT:
  1830. dentry = nfsd4_cld_register_sb(sb, cn->cn_pipe);
  1831. if (IS_ERR(dentry)) {
  1832. ret = PTR_ERR(dentry);
  1833. break;
  1834. }
  1835. cn->cn_pipe->dentry = dentry;
  1836. break;
  1837. case RPC_PIPEFS_UMOUNT:
  1838. if (cn->cn_pipe->dentry)
  1839. nfsd4_cld_unregister_sb(cn->cn_pipe);
  1840. break;
  1841. default:
  1842. ret = -ENOTSUPP;
  1843. break;
  1844. }
  1845. module_put(THIS_MODULE);
  1846. return ret;
  1847. }
  1848. static struct notifier_block nfsd4_cld_block = {
  1849. .notifier_call = rpc_pipefs_event,
  1850. };
  1851. int
  1852. register_cld_notifier(void)
  1853. {
  1854. WARN_ON(!nfsd_net_id);
  1855. return rpc_pipefs_notifier_register(&nfsd4_cld_block);
  1856. }
  1857. void
  1858. unregister_cld_notifier(void)
  1859. {
  1860. rpc_pipefs_notifier_unregister(&nfsd4_cld_block);
  1861. }