nfs4recover.c 51 KB

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