nfs4recover.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169
  1. /*
  2. * Copyright (c) 2004 The Regents of the University of Michigan.
  3. * Copyright (c) 2012 Jeff Layton <[email protected]>
  4. * All rights reserved.
  5. *
  6. * Andy Adamson <[email protected]>
  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(&init_user_ns, 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 bool
  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 true;
  244. entry = kmalloc(sizeof(struct name_list), GFP_KERNEL);
  245. if (entry == NULL)
  246. return false;
  247. memcpy(entry->name, name, HEXDIR_LEN - 1);
  248. entry->name[HEXDIR_LEN - 1] = '\0';
  249. list_add(&entry->list, &ctx->names);
  250. return true;
  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(&init_user_ns, 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(&init_user_ns, 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. pr_info("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(name.data))
  700. return PTR_ERR(name.data);
  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(princhash.data)) {
  708. kfree(name.data);
  709. return PTR_ERR(princhash.data);
  710. }
  711. princhash.len = princhashlen;
  712. } else
  713. princhash.len = 0;
  714. } else {
  715. const struct cld_name __user *cnm;
  716. cnm = &cmsg->cm_u.cm_name;
  717. if (get_user(namelen, &cnm->cn_len))
  718. return -EFAULT;
  719. name.data = memdup_user(&cnm->cn_id, namelen);
  720. if (IS_ERR(name.data))
  721. return PTR_ERR(name.data);
  722. name.len = namelen;
  723. }
  724. if (name.len > 5 && memcmp(name.data, "hash:", 5) == 0) {
  725. name.len = name.len - 5;
  726. memmove(name.data, name.data + 5, name.len);
  727. cn->cn_has_legacy = true;
  728. }
  729. if (!nfs4_client_to_reclaim(name, princhash, nn)) {
  730. kfree(name.data);
  731. kfree(princhash.data);
  732. return -EFAULT;
  733. }
  734. return nn->client_tracking_ops->msglen;
  735. }
  736. return -EFAULT;
  737. }
  738. static ssize_t
  739. cld_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
  740. {
  741. struct cld_upcall *tmp, *cup;
  742. struct cld_msg_hdr __user *hdr = (struct cld_msg_hdr __user *)src;
  743. struct cld_msg_v2 __user *cmsg = (struct cld_msg_v2 __user *)src;
  744. uint32_t xid;
  745. struct nfsd_net *nn = net_generic(file_inode(filp)->i_sb->s_fs_info,
  746. nfsd_net_id);
  747. struct cld_net *cn = nn->cld_net;
  748. int16_t status;
  749. if (mlen != nn->client_tracking_ops->msglen) {
  750. dprintk("%s: got %zu bytes, expected %zu\n", __func__, mlen,
  751. nn->client_tracking_ops->msglen);
  752. return -EINVAL;
  753. }
  754. /* copy just the xid so we can try to find that */
  755. if (copy_from_user(&xid, &hdr->cm_xid, sizeof(xid)) != 0) {
  756. dprintk("%s: error when copying xid from userspace", __func__);
  757. return -EFAULT;
  758. }
  759. /*
  760. * copy the status so we know whether to remove the upcall from the
  761. * list (for -EINPROGRESS, we just want to make sure the xid is
  762. * valid, not remove the upcall from the list)
  763. */
  764. if (get_user(status, &hdr->cm_status)) {
  765. dprintk("%s: error when copying status from userspace", __func__);
  766. return -EFAULT;
  767. }
  768. /* walk the list and find corresponding xid */
  769. cup = NULL;
  770. spin_lock(&cn->cn_lock);
  771. list_for_each_entry(tmp, &cn->cn_list, cu_list) {
  772. if (get_unaligned(&tmp->cu_u.cu_hdr.cm_xid) == xid) {
  773. cup = tmp;
  774. if (status != -EINPROGRESS)
  775. list_del_init(&cup->cu_list);
  776. break;
  777. }
  778. }
  779. spin_unlock(&cn->cn_lock);
  780. /* couldn't find upcall? */
  781. if (!cup) {
  782. dprintk("%s: couldn't find upcall -- xid=%u\n", __func__, xid);
  783. return -EINVAL;
  784. }
  785. if (status == -EINPROGRESS)
  786. return __cld_pipe_inprogress_downcall(cmsg, nn);
  787. if (copy_from_user(&cup->cu_u.cu_msg_v2, src, mlen) != 0)
  788. return -EFAULT;
  789. complete(&cup->cu_done);
  790. return mlen;
  791. }
  792. static void
  793. cld_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  794. {
  795. struct cld_msg *cmsg = msg->data;
  796. struct cld_upcall *cup = container_of(cmsg, struct cld_upcall,
  797. cu_u.cu_msg);
  798. /* errno >= 0 means we got a downcall */
  799. if (msg->errno >= 0)
  800. return;
  801. complete(&cup->cu_done);
  802. }
  803. static const struct rpc_pipe_ops cld_upcall_ops = {
  804. .upcall = rpc_pipe_generic_upcall,
  805. .downcall = cld_pipe_downcall,
  806. .destroy_msg = cld_pipe_destroy_msg,
  807. };
  808. static struct dentry *
  809. nfsd4_cld_register_sb(struct super_block *sb, struct rpc_pipe *pipe)
  810. {
  811. struct dentry *dir, *dentry;
  812. dir = rpc_d_lookup_sb(sb, NFSD_PIPE_DIR);
  813. if (dir == NULL)
  814. return ERR_PTR(-ENOENT);
  815. dentry = rpc_mkpipe_dentry(dir, NFSD_CLD_PIPE, NULL, pipe);
  816. dput(dir);
  817. return dentry;
  818. }
  819. static void
  820. nfsd4_cld_unregister_sb(struct rpc_pipe *pipe)
  821. {
  822. if (pipe->dentry)
  823. rpc_unlink(pipe->dentry);
  824. }
  825. static struct dentry *
  826. nfsd4_cld_register_net(struct net *net, struct rpc_pipe *pipe)
  827. {
  828. struct super_block *sb;
  829. struct dentry *dentry;
  830. sb = rpc_get_sb_net(net);
  831. if (!sb)
  832. return NULL;
  833. dentry = nfsd4_cld_register_sb(sb, pipe);
  834. rpc_put_sb_net(net);
  835. return dentry;
  836. }
  837. static void
  838. nfsd4_cld_unregister_net(struct net *net, struct rpc_pipe *pipe)
  839. {
  840. struct super_block *sb;
  841. sb = rpc_get_sb_net(net);
  842. if (sb) {
  843. nfsd4_cld_unregister_sb(pipe);
  844. rpc_put_sb_net(net);
  845. }
  846. }
  847. /* Initialize rpc_pipefs pipe for communication with client tracking daemon */
  848. static int
  849. __nfsd4_init_cld_pipe(struct net *net)
  850. {
  851. int ret;
  852. struct dentry *dentry;
  853. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  854. struct cld_net *cn;
  855. if (nn->cld_net)
  856. return 0;
  857. cn = kzalloc(sizeof(*cn), GFP_KERNEL);
  858. if (!cn) {
  859. ret = -ENOMEM;
  860. goto err;
  861. }
  862. cn->cn_pipe = rpc_mkpipe_data(&cld_upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
  863. if (IS_ERR(cn->cn_pipe)) {
  864. ret = PTR_ERR(cn->cn_pipe);
  865. goto err;
  866. }
  867. spin_lock_init(&cn->cn_lock);
  868. INIT_LIST_HEAD(&cn->cn_list);
  869. dentry = nfsd4_cld_register_net(net, cn->cn_pipe);
  870. if (IS_ERR(dentry)) {
  871. ret = PTR_ERR(dentry);
  872. goto err_destroy_data;
  873. }
  874. cn->cn_pipe->dentry = dentry;
  875. cn->cn_has_legacy = false;
  876. nn->cld_net = cn;
  877. return 0;
  878. err_destroy_data:
  879. rpc_destroy_pipe_data(cn->cn_pipe);
  880. err:
  881. kfree(cn);
  882. printk(KERN_ERR "NFSD: unable to create nfsdcld upcall pipe (%d)\n",
  883. ret);
  884. return ret;
  885. }
  886. static int
  887. nfsd4_init_cld_pipe(struct net *net)
  888. {
  889. int status;
  890. status = __nfsd4_init_cld_pipe(net);
  891. if (!status)
  892. pr_info("NFSD: Using old nfsdcld client tracking operations.\n");
  893. return status;
  894. }
  895. static void
  896. nfsd4_remove_cld_pipe(struct net *net)
  897. {
  898. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  899. struct cld_net *cn = nn->cld_net;
  900. nfsd4_cld_unregister_net(net, cn->cn_pipe);
  901. rpc_destroy_pipe_data(cn->cn_pipe);
  902. if (cn->cn_tfm)
  903. crypto_free_shash(cn->cn_tfm);
  904. kfree(nn->cld_net);
  905. nn->cld_net = NULL;
  906. }
  907. static struct cld_upcall *
  908. alloc_cld_upcall(struct nfsd_net *nn)
  909. {
  910. struct cld_upcall *new, *tmp;
  911. struct cld_net *cn = nn->cld_net;
  912. new = kzalloc(sizeof(*new), GFP_KERNEL);
  913. if (!new)
  914. return new;
  915. /* FIXME: hard cap on number in flight? */
  916. restart_search:
  917. spin_lock(&cn->cn_lock);
  918. list_for_each_entry(tmp, &cn->cn_list, cu_list) {
  919. if (tmp->cu_u.cu_msg.cm_xid == cn->cn_xid) {
  920. cn->cn_xid++;
  921. spin_unlock(&cn->cn_lock);
  922. goto restart_search;
  923. }
  924. }
  925. init_completion(&new->cu_done);
  926. new->cu_u.cu_msg.cm_vers = nn->client_tracking_ops->version;
  927. put_unaligned(cn->cn_xid++, &new->cu_u.cu_msg.cm_xid);
  928. new->cu_net = cn;
  929. list_add(&new->cu_list, &cn->cn_list);
  930. spin_unlock(&cn->cn_lock);
  931. dprintk("%s: allocated xid %u\n", __func__, new->cu_u.cu_msg.cm_xid);
  932. return new;
  933. }
  934. static void
  935. free_cld_upcall(struct cld_upcall *victim)
  936. {
  937. struct cld_net *cn = victim->cu_net;
  938. spin_lock(&cn->cn_lock);
  939. list_del(&victim->cu_list);
  940. spin_unlock(&cn->cn_lock);
  941. kfree(victim);
  942. }
  943. /* Ask daemon to create a new record */
  944. static void
  945. nfsd4_cld_create(struct nfs4_client *clp)
  946. {
  947. int ret;
  948. struct cld_upcall *cup;
  949. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  950. struct cld_net *cn = nn->cld_net;
  951. /* Don't upcall if it's already stored */
  952. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  953. return;
  954. cup = alloc_cld_upcall(nn);
  955. if (!cup) {
  956. ret = -ENOMEM;
  957. goto out_err;
  958. }
  959. cup->cu_u.cu_msg.cm_cmd = Cld_Create;
  960. cup->cu_u.cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
  961. memcpy(cup->cu_u.cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
  962. clp->cl_name.len);
  963. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
  964. if (!ret) {
  965. ret = cup->cu_u.cu_msg.cm_status;
  966. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  967. }
  968. free_cld_upcall(cup);
  969. out_err:
  970. if (ret)
  971. printk(KERN_ERR "NFSD: Unable to create client "
  972. "record on stable storage: %d\n", ret);
  973. }
  974. /* Ask daemon to create a new record */
  975. static void
  976. nfsd4_cld_create_v2(struct nfs4_client *clp)
  977. {
  978. int ret;
  979. struct cld_upcall *cup;
  980. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  981. struct cld_net *cn = nn->cld_net;
  982. struct cld_msg_v2 *cmsg;
  983. struct crypto_shash *tfm = cn->cn_tfm;
  984. struct xdr_netobj cksum;
  985. char *principal = NULL;
  986. /* Don't upcall if it's already stored */
  987. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  988. return;
  989. cup = alloc_cld_upcall(nn);
  990. if (!cup) {
  991. ret = -ENOMEM;
  992. goto out_err;
  993. }
  994. cmsg = &cup->cu_u.cu_msg_v2;
  995. cmsg->cm_cmd = Cld_Create;
  996. cmsg->cm_u.cm_clntinfo.cc_name.cn_len = clp->cl_name.len;
  997. memcpy(cmsg->cm_u.cm_clntinfo.cc_name.cn_id, clp->cl_name.data,
  998. clp->cl_name.len);
  999. if (clp->cl_cred.cr_raw_principal)
  1000. principal = clp->cl_cred.cr_raw_principal;
  1001. else if (clp->cl_cred.cr_principal)
  1002. principal = clp->cl_cred.cr_principal;
  1003. if (principal) {
  1004. cksum.len = crypto_shash_digestsize(tfm);
  1005. cksum.data = kmalloc(cksum.len, GFP_KERNEL);
  1006. if (cksum.data == NULL) {
  1007. ret = -ENOMEM;
  1008. goto out;
  1009. }
  1010. ret = crypto_shash_tfm_digest(tfm, principal, strlen(principal),
  1011. cksum.data);
  1012. if (ret) {
  1013. kfree(cksum.data);
  1014. goto out;
  1015. }
  1016. cmsg->cm_u.cm_clntinfo.cc_princhash.cp_len = cksum.len;
  1017. memcpy(cmsg->cm_u.cm_clntinfo.cc_princhash.cp_data,
  1018. cksum.data, cksum.len);
  1019. kfree(cksum.data);
  1020. } else
  1021. cmsg->cm_u.cm_clntinfo.cc_princhash.cp_len = 0;
  1022. ret = cld_pipe_upcall(cn->cn_pipe, cmsg, nn);
  1023. if (!ret) {
  1024. ret = cmsg->cm_status;
  1025. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  1026. }
  1027. out:
  1028. free_cld_upcall(cup);
  1029. out_err:
  1030. if (ret)
  1031. pr_err("NFSD: Unable to create client record on stable storage: %d\n",
  1032. ret);
  1033. }
  1034. /* Ask daemon to create a new record */
  1035. static void
  1036. nfsd4_cld_remove(struct nfs4_client *clp)
  1037. {
  1038. int ret;
  1039. struct cld_upcall *cup;
  1040. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1041. struct cld_net *cn = nn->cld_net;
  1042. /* Don't upcall if it's already removed */
  1043. if (!test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  1044. return;
  1045. cup = alloc_cld_upcall(nn);
  1046. if (!cup) {
  1047. ret = -ENOMEM;
  1048. goto out_err;
  1049. }
  1050. cup->cu_u.cu_msg.cm_cmd = Cld_Remove;
  1051. cup->cu_u.cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
  1052. memcpy(cup->cu_u.cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
  1053. clp->cl_name.len);
  1054. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
  1055. if (!ret) {
  1056. ret = cup->cu_u.cu_msg.cm_status;
  1057. clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  1058. }
  1059. free_cld_upcall(cup);
  1060. out_err:
  1061. if (ret)
  1062. printk(KERN_ERR "NFSD: Unable to remove client "
  1063. "record from stable storage: %d\n", ret);
  1064. }
  1065. /*
  1066. * For older nfsdcld's that do not allow us to "slurp" the clients
  1067. * from the tracking database during startup.
  1068. *
  1069. * Check for presence of a record, and update its timestamp
  1070. */
  1071. static int
  1072. nfsd4_cld_check_v0(struct nfs4_client *clp)
  1073. {
  1074. int ret;
  1075. struct cld_upcall *cup;
  1076. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1077. struct cld_net *cn = nn->cld_net;
  1078. /* Don't upcall if one was already stored during this grace pd */
  1079. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  1080. return 0;
  1081. cup = alloc_cld_upcall(nn);
  1082. if (!cup) {
  1083. printk(KERN_ERR "NFSD: Unable to check client record on "
  1084. "stable storage: %d\n", -ENOMEM);
  1085. return -ENOMEM;
  1086. }
  1087. cup->cu_u.cu_msg.cm_cmd = Cld_Check;
  1088. cup->cu_u.cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
  1089. memcpy(cup->cu_u.cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
  1090. clp->cl_name.len);
  1091. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
  1092. if (!ret) {
  1093. ret = cup->cu_u.cu_msg.cm_status;
  1094. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  1095. }
  1096. free_cld_upcall(cup);
  1097. return ret;
  1098. }
  1099. /*
  1100. * For newer nfsdcld's that allow us to "slurp" the clients
  1101. * from the tracking database during startup.
  1102. *
  1103. * Check for presence of a record in the reclaim_str_hashtbl
  1104. */
  1105. static int
  1106. nfsd4_cld_check(struct nfs4_client *clp)
  1107. {
  1108. struct nfs4_client_reclaim *crp;
  1109. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1110. struct cld_net *cn = nn->cld_net;
  1111. int status;
  1112. char dname[HEXDIR_LEN];
  1113. struct xdr_netobj name;
  1114. /* did we already find that this client is stable? */
  1115. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  1116. return 0;
  1117. /* look for it in the reclaim hashtable otherwise */
  1118. crp = nfsd4_find_reclaim_client(clp->cl_name, nn);
  1119. if (crp)
  1120. goto found;
  1121. if (cn->cn_has_legacy) {
  1122. status = nfs4_make_rec_clidname(dname, &clp->cl_name);
  1123. if (status)
  1124. return -ENOENT;
  1125. name.data = kmemdup(dname, HEXDIR_LEN, GFP_KERNEL);
  1126. if (!name.data) {
  1127. dprintk("%s: failed to allocate memory for name.data!\n",
  1128. __func__);
  1129. return -ENOENT;
  1130. }
  1131. name.len = HEXDIR_LEN;
  1132. crp = nfsd4_find_reclaim_client(name, nn);
  1133. kfree(name.data);
  1134. if (crp)
  1135. goto found;
  1136. }
  1137. return -ENOENT;
  1138. found:
  1139. crp->cr_clp = clp;
  1140. return 0;
  1141. }
  1142. static int
  1143. nfsd4_cld_check_v2(struct nfs4_client *clp)
  1144. {
  1145. struct nfs4_client_reclaim *crp;
  1146. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1147. struct cld_net *cn = nn->cld_net;
  1148. int status;
  1149. char dname[HEXDIR_LEN];
  1150. struct xdr_netobj name;
  1151. struct crypto_shash *tfm = cn->cn_tfm;
  1152. struct xdr_netobj cksum;
  1153. char *principal = NULL;
  1154. /* did we already find that this client is stable? */
  1155. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  1156. return 0;
  1157. /* look for it in the reclaim hashtable otherwise */
  1158. crp = nfsd4_find_reclaim_client(clp->cl_name, nn);
  1159. if (crp)
  1160. goto found;
  1161. if (cn->cn_has_legacy) {
  1162. status = nfs4_make_rec_clidname(dname, &clp->cl_name);
  1163. if (status)
  1164. return -ENOENT;
  1165. name.data = kmemdup(dname, HEXDIR_LEN, GFP_KERNEL);
  1166. if (!name.data) {
  1167. dprintk("%s: failed to allocate memory for name.data\n",
  1168. __func__);
  1169. return -ENOENT;
  1170. }
  1171. name.len = HEXDIR_LEN;
  1172. crp = nfsd4_find_reclaim_client(name, nn);
  1173. kfree(name.data);
  1174. if (crp)
  1175. goto found;
  1176. }
  1177. return -ENOENT;
  1178. found:
  1179. if (crp->cr_princhash.len) {
  1180. if (clp->cl_cred.cr_raw_principal)
  1181. principal = clp->cl_cred.cr_raw_principal;
  1182. else if (clp->cl_cred.cr_principal)
  1183. principal = clp->cl_cred.cr_principal;
  1184. if (principal == NULL)
  1185. return -ENOENT;
  1186. cksum.len = crypto_shash_digestsize(tfm);
  1187. cksum.data = kmalloc(cksum.len, GFP_KERNEL);
  1188. if (cksum.data == NULL)
  1189. return -ENOENT;
  1190. status = crypto_shash_tfm_digest(tfm, principal,
  1191. strlen(principal), cksum.data);
  1192. if (status) {
  1193. kfree(cksum.data);
  1194. return -ENOENT;
  1195. }
  1196. if (memcmp(crp->cr_princhash.data, cksum.data,
  1197. crp->cr_princhash.len)) {
  1198. kfree(cksum.data);
  1199. return -ENOENT;
  1200. }
  1201. kfree(cksum.data);
  1202. }
  1203. crp->cr_clp = clp;
  1204. return 0;
  1205. }
  1206. static int
  1207. nfsd4_cld_grace_start(struct nfsd_net *nn)
  1208. {
  1209. int ret;
  1210. struct cld_upcall *cup;
  1211. struct cld_net *cn = nn->cld_net;
  1212. cup = alloc_cld_upcall(nn);
  1213. if (!cup) {
  1214. ret = -ENOMEM;
  1215. goto out_err;
  1216. }
  1217. cup->cu_u.cu_msg.cm_cmd = Cld_GraceStart;
  1218. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
  1219. if (!ret)
  1220. ret = cup->cu_u.cu_msg.cm_status;
  1221. free_cld_upcall(cup);
  1222. out_err:
  1223. if (ret)
  1224. dprintk("%s: Unable to get clients from userspace: %d\n",
  1225. __func__, ret);
  1226. return ret;
  1227. }
  1228. /* For older nfsdcld's that need cm_gracetime */
  1229. static void
  1230. nfsd4_cld_grace_done_v0(struct nfsd_net *nn)
  1231. {
  1232. int ret;
  1233. struct cld_upcall *cup;
  1234. struct cld_net *cn = nn->cld_net;
  1235. cup = alloc_cld_upcall(nn);
  1236. if (!cup) {
  1237. ret = -ENOMEM;
  1238. goto out_err;
  1239. }
  1240. cup->cu_u.cu_msg.cm_cmd = Cld_GraceDone;
  1241. cup->cu_u.cu_msg.cm_u.cm_gracetime = nn->boot_time;
  1242. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
  1243. if (!ret)
  1244. ret = cup->cu_u.cu_msg.cm_status;
  1245. free_cld_upcall(cup);
  1246. out_err:
  1247. if (ret)
  1248. printk(KERN_ERR "NFSD: Unable to end grace period: %d\n", ret);
  1249. }
  1250. /*
  1251. * For newer nfsdcld's that do not need cm_gracetime. We also need to call
  1252. * nfs4_release_reclaim() to clear out the reclaim_str_hashtbl.
  1253. */
  1254. static void
  1255. nfsd4_cld_grace_done(struct nfsd_net *nn)
  1256. {
  1257. int ret;
  1258. struct cld_upcall *cup;
  1259. struct cld_net *cn = nn->cld_net;
  1260. cup = alloc_cld_upcall(nn);
  1261. if (!cup) {
  1262. ret = -ENOMEM;
  1263. goto out_err;
  1264. }
  1265. cup->cu_u.cu_msg.cm_cmd = Cld_GraceDone;
  1266. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
  1267. if (!ret)
  1268. ret = cup->cu_u.cu_msg.cm_status;
  1269. free_cld_upcall(cup);
  1270. out_err:
  1271. nfs4_release_reclaim(nn);
  1272. if (ret)
  1273. printk(KERN_ERR "NFSD: Unable to end grace period: %d\n", ret);
  1274. }
  1275. static int
  1276. nfs4_cld_state_init(struct net *net)
  1277. {
  1278. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1279. int i;
  1280. nn->reclaim_str_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
  1281. sizeof(struct list_head),
  1282. GFP_KERNEL);
  1283. if (!nn->reclaim_str_hashtbl)
  1284. return -ENOMEM;
  1285. for (i = 0; i < CLIENT_HASH_SIZE; i++)
  1286. INIT_LIST_HEAD(&nn->reclaim_str_hashtbl[i]);
  1287. nn->reclaim_str_hashtbl_size = 0;
  1288. nn->track_reclaim_completes = true;
  1289. atomic_set(&nn->nr_reclaim_complete, 0);
  1290. return 0;
  1291. }
  1292. static void
  1293. nfs4_cld_state_shutdown(struct net *net)
  1294. {
  1295. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1296. nn->track_reclaim_completes = false;
  1297. kfree(nn->reclaim_str_hashtbl);
  1298. }
  1299. static bool
  1300. cld_running(struct nfsd_net *nn)
  1301. {
  1302. struct cld_net *cn = nn->cld_net;
  1303. struct rpc_pipe *pipe = cn->cn_pipe;
  1304. return pipe->nreaders || pipe->nwriters;
  1305. }
  1306. static int
  1307. nfsd4_cld_get_version(struct nfsd_net *nn)
  1308. {
  1309. int ret = 0;
  1310. struct cld_upcall *cup;
  1311. struct cld_net *cn = nn->cld_net;
  1312. uint8_t version;
  1313. cup = alloc_cld_upcall(nn);
  1314. if (!cup) {
  1315. ret = -ENOMEM;
  1316. goto out_err;
  1317. }
  1318. cup->cu_u.cu_msg.cm_cmd = Cld_GetVersion;
  1319. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
  1320. if (!ret) {
  1321. ret = cup->cu_u.cu_msg.cm_status;
  1322. if (ret)
  1323. goto out_free;
  1324. version = cup->cu_u.cu_msg.cm_u.cm_version;
  1325. dprintk("%s: userspace returned version %u\n",
  1326. __func__, version);
  1327. if (version < 1)
  1328. version = 1;
  1329. else if (version > CLD_UPCALL_VERSION)
  1330. version = CLD_UPCALL_VERSION;
  1331. switch (version) {
  1332. case 1:
  1333. nn->client_tracking_ops = &nfsd4_cld_tracking_ops;
  1334. break;
  1335. case 2:
  1336. nn->client_tracking_ops = &nfsd4_cld_tracking_ops_v2;
  1337. break;
  1338. default:
  1339. break;
  1340. }
  1341. }
  1342. out_free:
  1343. free_cld_upcall(cup);
  1344. out_err:
  1345. if (ret)
  1346. dprintk("%s: Unable to get version from userspace: %d\n",
  1347. __func__, ret);
  1348. return ret;
  1349. }
  1350. static int
  1351. nfsd4_cld_tracking_init(struct net *net)
  1352. {
  1353. int status;
  1354. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1355. bool running;
  1356. int retries = 10;
  1357. struct crypto_shash *tfm;
  1358. status = nfs4_cld_state_init(net);
  1359. if (status)
  1360. return status;
  1361. status = __nfsd4_init_cld_pipe(net);
  1362. if (status)
  1363. goto err_shutdown;
  1364. /*
  1365. * rpc pipe upcalls take 30 seconds to time out, so we don't want to
  1366. * queue an upcall unless we know that nfsdcld is running (because we
  1367. * want this to fail fast so that nfsd4_client_tracking_init() can try
  1368. * the next client tracking method). nfsdcld should already be running
  1369. * before nfsd is started, so the wait here is for nfsdcld to open the
  1370. * pipefs file we just created.
  1371. */
  1372. while (!(running = cld_running(nn)) && retries--)
  1373. msleep(100);
  1374. if (!running) {
  1375. status = -ETIMEDOUT;
  1376. goto err_remove;
  1377. }
  1378. tfm = crypto_alloc_shash("sha256", 0, 0);
  1379. if (IS_ERR(tfm)) {
  1380. status = PTR_ERR(tfm);
  1381. goto err_remove;
  1382. }
  1383. nn->cld_net->cn_tfm = tfm;
  1384. status = nfsd4_cld_get_version(nn);
  1385. if (status == -EOPNOTSUPP)
  1386. pr_warn("NFSD: nfsdcld GetVersion upcall failed. Please upgrade nfsdcld.\n");
  1387. status = nfsd4_cld_grace_start(nn);
  1388. if (status) {
  1389. if (status == -EOPNOTSUPP)
  1390. pr_warn("NFSD: nfsdcld GraceStart upcall failed. Please upgrade nfsdcld.\n");
  1391. nfs4_release_reclaim(nn);
  1392. goto err_remove;
  1393. } else
  1394. pr_info("NFSD: Using nfsdcld client tracking operations.\n");
  1395. return 0;
  1396. err_remove:
  1397. nfsd4_remove_cld_pipe(net);
  1398. err_shutdown:
  1399. nfs4_cld_state_shutdown(net);
  1400. return status;
  1401. }
  1402. static void
  1403. nfsd4_cld_tracking_exit(struct net *net)
  1404. {
  1405. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1406. nfs4_release_reclaim(nn);
  1407. nfsd4_remove_cld_pipe(net);
  1408. nfs4_cld_state_shutdown(net);
  1409. }
  1410. /* For older nfsdcld's */
  1411. static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops_v0 = {
  1412. .init = nfsd4_init_cld_pipe,
  1413. .exit = nfsd4_remove_cld_pipe,
  1414. .create = nfsd4_cld_create,
  1415. .remove = nfsd4_cld_remove,
  1416. .check = nfsd4_cld_check_v0,
  1417. .grace_done = nfsd4_cld_grace_done_v0,
  1418. .version = 1,
  1419. .msglen = sizeof(struct cld_msg),
  1420. };
  1421. /* For newer nfsdcld's */
  1422. static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops = {
  1423. .init = nfsd4_cld_tracking_init,
  1424. .exit = nfsd4_cld_tracking_exit,
  1425. .create = nfsd4_cld_create,
  1426. .remove = nfsd4_cld_remove,
  1427. .check = nfsd4_cld_check,
  1428. .grace_done = nfsd4_cld_grace_done,
  1429. .version = 1,
  1430. .msglen = sizeof(struct cld_msg),
  1431. };
  1432. /* v2 create/check ops include the principal, if available */
  1433. static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops_v2 = {
  1434. .init = nfsd4_cld_tracking_init,
  1435. .exit = nfsd4_cld_tracking_exit,
  1436. .create = nfsd4_cld_create_v2,
  1437. .remove = nfsd4_cld_remove,
  1438. .check = nfsd4_cld_check_v2,
  1439. .grace_done = nfsd4_cld_grace_done,
  1440. .version = 2,
  1441. .msglen = sizeof(struct cld_msg_v2),
  1442. };
  1443. /* upcall via usermodehelper */
  1444. static char cltrack_prog[PATH_MAX] = "/sbin/nfsdcltrack";
  1445. module_param_string(cltrack_prog, cltrack_prog, sizeof(cltrack_prog),
  1446. S_IRUGO|S_IWUSR);
  1447. MODULE_PARM_DESC(cltrack_prog, "Path to the nfsdcltrack upcall program");
  1448. static bool cltrack_legacy_disable;
  1449. module_param(cltrack_legacy_disable, bool, S_IRUGO|S_IWUSR);
  1450. MODULE_PARM_DESC(cltrack_legacy_disable,
  1451. "Disable legacy recoverydir conversion. Default: false");
  1452. #define LEGACY_TOPDIR_ENV_PREFIX "NFSDCLTRACK_LEGACY_TOPDIR="
  1453. #define LEGACY_RECDIR_ENV_PREFIX "NFSDCLTRACK_LEGACY_RECDIR="
  1454. #define HAS_SESSION_ENV_PREFIX "NFSDCLTRACK_CLIENT_HAS_SESSION="
  1455. #define GRACE_START_ENV_PREFIX "NFSDCLTRACK_GRACE_START="
  1456. static char *
  1457. nfsd4_cltrack_legacy_topdir(void)
  1458. {
  1459. int copied;
  1460. size_t len;
  1461. char *result;
  1462. if (cltrack_legacy_disable)
  1463. return NULL;
  1464. len = strlen(LEGACY_TOPDIR_ENV_PREFIX) +
  1465. strlen(nfs4_recoverydir()) + 1;
  1466. result = kmalloc(len, GFP_KERNEL);
  1467. if (!result)
  1468. return result;
  1469. copied = snprintf(result, len, LEGACY_TOPDIR_ENV_PREFIX "%s",
  1470. nfs4_recoverydir());
  1471. if (copied >= len) {
  1472. /* just return nothing if output was truncated */
  1473. kfree(result);
  1474. return NULL;
  1475. }
  1476. return result;
  1477. }
  1478. static char *
  1479. nfsd4_cltrack_legacy_recdir(const struct xdr_netobj *name)
  1480. {
  1481. int copied;
  1482. size_t len;
  1483. char *result;
  1484. if (cltrack_legacy_disable)
  1485. return NULL;
  1486. /* +1 is for '/' between "topdir" and "recdir" */
  1487. len = strlen(LEGACY_RECDIR_ENV_PREFIX) +
  1488. strlen(nfs4_recoverydir()) + 1 + HEXDIR_LEN;
  1489. result = kmalloc(len, GFP_KERNEL);
  1490. if (!result)
  1491. return result;
  1492. copied = snprintf(result, len, LEGACY_RECDIR_ENV_PREFIX "%s/",
  1493. nfs4_recoverydir());
  1494. if (copied > (len - HEXDIR_LEN)) {
  1495. /* just return nothing if output will be truncated */
  1496. kfree(result);
  1497. return NULL;
  1498. }
  1499. copied = nfs4_make_rec_clidname(result + copied, name);
  1500. if (copied) {
  1501. kfree(result);
  1502. return NULL;
  1503. }
  1504. return result;
  1505. }
  1506. static char *
  1507. nfsd4_cltrack_client_has_session(struct nfs4_client *clp)
  1508. {
  1509. int copied;
  1510. size_t len;
  1511. char *result;
  1512. /* prefix + Y/N character + terminating NULL */
  1513. len = strlen(HAS_SESSION_ENV_PREFIX) + 1 + 1;
  1514. result = kmalloc(len, GFP_KERNEL);
  1515. if (!result)
  1516. return result;
  1517. copied = snprintf(result, len, HAS_SESSION_ENV_PREFIX "%c",
  1518. clp->cl_minorversion ? 'Y' : 'N');
  1519. if (copied >= len) {
  1520. /* just return nothing if output was truncated */
  1521. kfree(result);
  1522. return NULL;
  1523. }
  1524. return result;
  1525. }
  1526. static char *
  1527. nfsd4_cltrack_grace_start(time64_t grace_start)
  1528. {
  1529. int copied;
  1530. size_t len;
  1531. char *result;
  1532. /* prefix + max width of int64_t string + terminating NULL */
  1533. len = strlen(GRACE_START_ENV_PREFIX) + 22 + 1;
  1534. result = kmalloc(len, GFP_KERNEL);
  1535. if (!result)
  1536. return result;
  1537. copied = snprintf(result, len, GRACE_START_ENV_PREFIX "%lld",
  1538. grace_start);
  1539. if (copied >= len) {
  1540. /* just return nothing if output was truncated */
  1541. kfree(result);
  1542. return NULL;
  1543. }
  1544. return result;
  1545. }
  1546. static int
  1547. nfsd4_umh_cltrack_upcall(char *cmd, char *arg, char *env0, char *env1)
  1548. {
  1549. char *envp[3];
  1550. char *argv[4];
  1551. int ret;
  1552. if (unlikely(!cltrack_prog[0])) {
  1553. dprintk("%s: cltrack_prog is disabled\n", __func__);
  1554. return -EACCES;
  1555. }
  1556. dprintk("%s: cmd: %s\n", __func__, cmd);
  1557. dprintk("%s: arg: %s\n", __func__, arg ? arg : "(null)");
  1558. dprintk("%s: env0: %s\n", __func__, env0 ? env0 : "(null)");
  1559. dprintk("%s: env1: %s\n", __func__, env1 ? env1 : "(null)");
  1560. envp[0] = env0;
  1561. envp[1] = env1;
  1562. envp[2] = NULL;
  1563. argv[0] = (char *)cltrack_prog;
  1564. argv[1] = cmd;
  1565. argv[2] = arg;
  1566. argv[3] = NULL;
  1567. ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
  1568. /*
  1569. * Disable the upcall mechanism if we're getting an ENOENT or EACCES
  1570. * error. The admin can re-enable it on the fly by using sysfs
  1571. * once the problem has been fixed.
  1572. */
  1573. if (ret == -ENOENT || ret == -EACCES) {
  1574. dprintk("NFSD: %s was not found or isn't executable (%d). "
  1575. "Setting cltrack_prog to blank string!",
  1576. cltrack_prog, ret);
  1577. cltrack_prog[0] = '\0';
  1578. }
  1579. dprintk("%s: %s return value: %d\n", __func__, cltrack_prog, ret);
  1580. return ret;
  1581. }
  1582. static char *
  1583. bin_to_hex_dup(const unsigned char *src, int srclen)
  1584. {
  1585. char *buf;
  1586. /* +1 for terminating NULL */
  1587. buf = kzalloc((srclen * 2) + 1, GFP_KERNEL);
  1588. if (!buf)
  1589. return buf;
  1590. bin2hex(buf, src, srclen);
  1591. return buf;
  1592. }
  1593. static int
  1594. nfsd4_umh_cltrack_init(struct net *net)
  1595. {
  1596. int ret;
  1597. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1598. char *grace_start = nfsd4_cltrack_grace_start(nn->boot_time);
  1599. /* XXX: The usermode helper s not working in container yet. */
  1600. if (net != &init_net) {
  1601. pr_warn("NFSD: attempt to initialize umh client tracking in a container ignored.\n");
  1602. kfree(grace_start);
  1603. return -EINVAL;
  1604. }
  1605. ret = nfsd4_umh_cltrack_upcall("init", NULL, grace_start, NULL);
  1606. kfree(grace_start);
  1607. if (!ret)
  1608. pr_info("NFSD: Using UMH upcall client tracking operations.\n");
  1609. return ret;
  1610. }
  1611. static void
  1612. nfsd4_cltrack_upcall_lock(struct nfs4_client *clp)
  1613. {
  1614. wait_on_bit_lock(&clp->cl_flags, NFSD4_CLIENT_UPCALL_LOCK,
  1615. TASK_UNINTERRUPTIBLE);
  1616. }
  1617. static void
  1618. nfsd4_cltrack_upcall_unlock(struct nfs4_client *clp)
  1619. {
  1620. smp_mb__before_atomic();
  1621. clear_bit(NFSD4_CLIENT_UPCALL_LOCK, &clp->cl_flags);
  1622. smp_mb__after_atomic();
  1623. wake_up_bit(&clp->cl_flags, NFSD4_CLIENT_UPCALL_LOCK);
  1624. }
  1625. static void
  1626. nfsd4_umh_cltrack_create(struct nfs4_client *clp)
  1627. {
  1628. char *hexid, *has_session, *grace_start;
  1629. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1630. /*
  1631. * With v4.0 clients, there's little difference in outcome between a
  1632. * create and check operation, and we can end up calling into this
  1633. * function multiple times per client (once for each openowner). So,
  1634. * for v4.0 clients skip upcalling once the client has been recorded
  1635. * on stable storage.
  1636. *
  1637. * For v4.1+ clients, the outcome of the two operations is different,
  1638. * so we must ensure that we upcall for the create operation. v4.1+
  1639. * clients call this on RECLAIM_COMPLETE though, so we should only end
  1640. * up doing a single create upcall per client.
  1641. */
  1642. if (clp->cl_minorversion == 0 &&
  1643. test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  1644. return;
  1645. hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
  1646. if (!hexid) {
  1647. dprintk("%s: can't allocate memory for upcall!\n", __func__);
  1648. return;
  1649. }
  1650. has_session = nfsd4_cltrack_client_has_session(clp);
  1651. grace_start = nfsd4_cltrack_grace_start(nn->boot_time);
  1652. nfsd4_cltrack_upcall_lock(clp);
  1653. if (!nfsd4_umh_cltrack_upcall("create", hexid, has_session, grace_start))
  1654. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  1655. nfsd4_cltrack_upcall_unlock(clp);
  1656. kfree(has_session);
  1657. kfree(grace_start);
  1658. kfree(hexid);
  1659. }
  1660. static void
  1661. nfsd4_umh_cltrack_remove(struct nfs4_client *clp)
  1662. {
  1663. char *hexid;
  1664. if (!test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  1665. return;
  1666. hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
  1667. if (!hexid) {
  1668. dprintk("%s: can't allocate memory for upcall!\n", __func__);
  1669. return;
  1670. }
  1671. nfsd4_cltrack_upcall_lock(clp);
  1672. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags) &&
  1673. nfsd4_umh_cltrack_upcall("remove", hexid, NULL, NULL) == 0)
  1674. clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  1675. nfsd4_cltrack_upcall_unlock(clp);
  1676. kfree(hexid);
  1677. }
  1678. static int
  1679. nfsd4_umh_cltrack_check(struct nfs4_client *clp)
  1680. {
  1681. int ret;
  1682. char *hexid, *has_session, *legacy;
  1683. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  1684. return 0;
  1685. hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
  1686. if (!hexid) {
  1687. dprintk("%s: can't allocate memory for upcall!\n", __func__);
  1688. return -ENOMEM;
  1689. }
  1690. has_session = nfsd4_cltrack_client_has_session(clp);
  1691. legacy = nfsd4_cltrack_legacy_recdir(&clp->cl_name);
  1692. nfsd4_cltrack_upcall_lock(clp);
  1693. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) {
  1694. ret = 0;
  1695. } else {
  1696. ret = nfsd4_umh_cltrack_upcall("check", hexid, has_session, legacy);
  1697. if (ret == 0)
  1698. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  1699. }
  1700. nfsd4_cltrack_upcall_unlock(clp);
  1701. kfree(has_session);
  1702. kfree(legacy);
  1703. kfree(hexid);
  1704. return ret;
  1705. }
  1706. static void
  1707. nfsd4_umh_cltrack_grace_done(struct nfsd_net *nn)
  1708. {
  1709. char *legacy;
  1710. char timestr[22]; /* FIXME: better way to determine max size? */
  1711. sprintf(timestr, "%lld", nn->boot_time);
  1712. legacy = nfsd4_cltrack_legacy_topdir();
  1713. nfsd4_umh_cltrack_upcall("gracedone", timestr, legacy, NULL);
  1714. kfree(legacy);
  1715. }
  1716. static const struct nfsd4_client_tracking_ops nfsd4_umh_tracking_ops = {
  1717. .init = nfsd4_umh_cltrack_init,
  1718. .exit = NULL,
  1719. .create = nfsd4_umh_cltrack_create,
  1720. .remove = nfsd4_umh_cltrack_remove,
  1721. .check = nfsd4_umh_cltrack_check,
  1722. .grace_done = nfsd4_umh_cltrack_grace_done,
  1723. .version = 1,
  1724. .msglen = 0,
  1725. };
  1726. int
  1727. nfsd4_client_tracking_init(struct net *net)
  1728. {
  1729. int status;
  1730. struct path path;
  1731. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1732. /* just run the init if it the method is already decided */
  1733. if (nn->client_tracking_ops)
  1734. goto do_init;
  1735. /* First, try to use nfsdcld */
  1736. nn->client_tracking_ops = &nfsd4_cld_tracking_ops;
  1737. status = nn->client_tracking_ops->init(net);
  1738. if (!status)
  1739. return status;
  1740. if (status != -ETIMEDOUT) {
  1741. nn->client_tracking_ops = &nfsd4_cld_tracking_ops_v0;
  1742. status = nn->client_tracking_ops->init(net);
  1743. if (!status)
  1744. return status;
  1745. }
  1746. /*
  1747. * Next, try the UMH upcall.
  1748. */
  1749. nn->client_tracking_ops = &nfsd4_umh_tracking_ops;
  1750. status = nn->client_tracking_ops->init(net);
  1751. if (!status)
  1752. return status;
  1753. /*
  1754. * Finally, See if the recoverydir exists and is a directory.
  1755. * If it is, then use the legacy ops.
  1756. */
  1757. nn->client_tracking_ops = &nfsd4_legacy_tracking_ops;
  1758. status = kern_path(nfs4_recoverydir(), LOOKUP_FOLLOW, &path);
  1759. if (!status) {
  1760. status = d_is_dir(path.dentry);
  1761. path_put(&path);
  1762. if (!status) {
  1763. status = -EINVAL;
  1764. goto out;
  1765. }
  1766. }
  1767. do_init:
  1768. status = nn->client_tracking_ops->init(net);
  1769. out:
  1770. if (status) {
  1771. printk(KERN_WARNING "NFSD: Unable to initialize client "
  1772. "recovery tracking! (%d)\n", status);
  1773. nn->client_tracking_ops = NULL;
  1774. }
  1775. return status;
  1776. }
  1777. void
  1778. nfsd4_client_tracking_exit(struct net *net)
  1779. {
  1780. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1781. if (nn->client_tracking_ops) {
  1782. if (nn->client_tracking_ops->exit)
  1783. nn->client_tracking_ops->exit(net);
  1784. nn->client_tracking_ops = NULL;
  1785. }
  1786. }
  1787. void
  1788. nfsd4_client_record_create(struct nfs4_client *clp)
  1789. {
  1790. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1791. if (nn->client_tracking_ops)
  1792. nn->client_tracking_ops->create(clp);
  1793. }
  1794. void
  1795. nfsd4_client_record_remove(struct nfs4_client *clp)
  1796. {
  1797. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1798. if (nn->client_tracking_ops)
  1799. nn->client_tracking_ops->remove(clp);
  1800. }
  1801. int
  1802. nfsd4_client_record_check(struct nfs4_client *clp)
  1803. {
  1804. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1805. if (nn->client_tracking_ops)
  1806. return nn->client_tracking_ops->check(clp);
  1807. return -EOPNOTSUPP;
  1808. }
  1809. void
  1810. nfsd4_record_grace_done(struct nfsd_net *nn)
  1811. {
  1812. if (nn->client_tracking_ops)
  1813. nn->client_tracking_ops->grace_done(nn);
  1814. }
  1815. static int
  1816. rpc_pipefs_event(struct notifier_block *nb, unsigned long event, void *ptr)
  1817. {
  1818. struct super_block *sb = ptr;
  1819. struct net *net = sb->s_fs_info;
  1820. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1821. struct cld_net *cn = nn->cld_net;
  1822. struct dentry *dentry;
  1823. int ret = 0;
  1824. if (!try_module_get(THIS_MODULE))
  1825. return 0;
  1826. if (!cn) {
  1827. module_put(THIS_MODULE);
  1828. return 0;
  1829. }
  1830. switch (event) {
  1831. case RPC_PIPEFS_MOUNT:
  1832. dentry = nfsd4_cld_register_sb(sb, cn->cn_pipe);
  1833. if (IS_ERR(dentry)) {
  1834. ret = PTR_ERR(dentry);
  1835. break;
  1836. }
  1837. cn->cn_pipe->dentry = dentry;
  1838. break;
  1839. case RPC_PIPEFS_UMOUNT:
  1840. if (cn->cn_pipe->dentry)
  1841. nfsd4_cld_unregister_sb(cn->cn_pipe);
  1842. break;
  1843. default:
  1844. ret = -ENOTSUPP;
  1845. break;
  1846. }
  1847. module_put(THIS_MODULE);
  1848. return ret;
  1849. }
  1850. static struct notifier_block nfsd4_cld_block = {
  1851. .notifier_call = rpc_pipefs_event,
  1852. };
  1853. int
  1854. register_cld_notifier(void)
  1855. {
  1856. WARN_ON(!nfsd_net_id);
  1857. return rpc_pipefs_notifier_register(&nfsd4_cld_block);
  1858. }
  1859. void
  1860. unregister_cld_notifier(void)
  1861. {
  1862. rpc_pipefs_notifier_unregister(&nfsd4_cld_block);
  1863. }