nfs4recover.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  1. /*
  2. * Copyright (c) 2004 The Regents of the University of Michigan.
  3. * Copyright (c) 2012 Jeff Layton <jlayton@redhat.com>
  4. * All rights reserved.
  5. *
  6. * Andy Adamson <andros@citi.umich.edu>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  28. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. */
  34. #include <linux/file.h>
  35. #include <linux/slab.h>
  36. #include <linux/namei.h>
  37. #include <linux/crypto.h>
  38. #include <linux/sched.h>
  39. #include <linux/fs.h>
  40. #include <linux/module.h>
  41. #include <net/net_namespace.h>
  42. #include <linux/sunrpc/rpc_pipe_fs.h>
  43. #include <linux/sunrpc/clnt.h>
  44. #include <linux/nfsd/cld.h>
  45. #include "nfsd.h"
  46. #include "state.h"
  47. #include "vfs.h"
  48. #include "netns.h"
  49. #define NFSDDBG_FACILITY NFSDDBG_PROC
  50. /* Declarations */
  51. struct nfsd4_client_tracking_ops {
  52. int (*init)(struct net *);
  53. void (*exit)(struct net *);
  54. void (*create)(struct nfs4_client *);
  55. void (*remove)(struct nfs4_client *);
  56. int (*check)(struct nfs4_client *);
  57. void (*grace_done)(struct nfsd_net *);
  58. };
  59. /* Globals */
  60. static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
  61. static int
  62. nfs4_save_creds(const struct cred **original_creds)
  63. {
  64. struct cred *new;
  65. new = prepare_creds();
  66. if (!new)
  67. return -ENOMEM;
  68. new->fsuid = GLOBAL_ROOT_UID;
  69. new->fsgid = GLOBAL_ROOT_GID;
  70. *original_creds = override_creds(new);
  71. put_cred(new);
  72. return 0;
  73. }
  74. static void
  75. nfs4_reset_creds(const struct cred *original)
  76. {
  77. revert_creds(original);
  78. }
  79. static void
  80. md5_to_hex(char *out, char *md5)
  81. {
  82. int i;
  83. for (i=0; i<16; i++) {
  84. unsigned char c = md5[i];
  85. *out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
  86. *out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
  87. }
  88. *out = '\0';
  89. }
  90. static int
  91. nfs4_make_rec_clidname(char *dname, const struct xdr_netobj *clname)
  92. {
  93. struct xdr_netobj cksum;
  94. struct hash_desc desc;
  95. struct scatterlist sg;
  96. int status;
  97. dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
  98. clname->len, clname->data);
  99. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  100. desc.tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
  101. if (IS_ERR(desc.tfm)) {
  102. status = PTR_ERR(desc.tfm);
  103. goto out_no_tfm;
  104. }
  105. cksum.len = crypto_hash_digestsize(desc.tfm);
  106. cksum.data = kmalloc(cksum.len, GFP_KERNEL);
  107. if (cksum.data == NULL) {
  108. status = -ENOMEM;
  109. goto out;
  110. }
  111. sg_init_one(&sg, clname->data, clname->len);
  112. status = crypto_hash_digest(&desc, &sg, sg.length, cksum.data);
  113. if (status)
  114. goto out;
  115. md5_to_hex(dname, cksum.data);
  116. status = 0;
  117. out:
  118. kfree(cksum.data);
  119. crypto_free_hash(desc.tfm);
  120. out_no_tfm:
  121. return status;
  122. }
  123. /*
  124. * If we had an error generating the recdir name for the legacy tracker
  125. * then warn the admin. If the error doesn't appear to be transient,
  126. * then disable recovery tracking.
  127. */
  128. static void
  129. legacy_recdir_name_error(struct nfs4_client *clp, int error)
  130. {
  131. printk(KERN_ERR "NFSD: unable to generate recoverydir "
  132. "name (%d).\n", error);
  133. /*
  134. * if the algorithm just doesn't exist, then disable the recovery
  135. * tracker altogether. The crypto libs will generally return this if
  136. * FIPS is enabled as well.
  137. */
  138. if (error == -ENOENT) {
  139. printk(KERN_ERR "NFSD: disabling legacy clientid tracking. "
  140. "Reboot recovery will not function correctly!\n");
  141. nfsd4_client_tracking_exit(clp->net);
  142. }
  143. }
  144. static void
  145. nfsd4_create_clid_dir(struct nfs4_client *clp)
  146. {
  147. const struct cred *original_cred;
  148. char dname[HEXDIR_LEN];
  149. struct dentry *dir, *dentry;
  150. struct nfs4_client_reclaim *crp;
  151. int status;
  152. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  153. if (test_and_set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  154. return;
  155. if (!nn->rec_file)
  156. return;
  157. status = nfs4_make_rec_clidname(dname, &clp->cl_name);
  158. if (status)
  159. return legacy_recdir_name_error(clp, status);
  160. status = nfs4_save_creds(&original_cred);
  161. if (status < 0)
  162. return;
  163. status = mnt_want_write_file(nn->rec_file);
  164. if (status)
  165. goto out_creds;
  166. dir = nn->rec_file->f_path.dentry;
  167. /* lock the parent */
  168. mutex_lock(&dir->d_inode->i_mutex);
  169. dentry = lookup_one_len(dname, dir, HEXDIR_LEN-1);
  170. if (IS_ERR(dentry)) {
  171. status = PTR_ERR(dentry);
  172. goto out_unlock;
  173. }
  174. if (dentry->d_inode)
  175. /*
  176. * In the 4.1 case, where we're called from
  177. * reclaim_complete(), records from the previous reboot
  178. * may still be left, so this is OK.
  179. *
  180. * In the 4.0 case, we should never get here; but we may
  181. * as well be forgiving and just succeed silently.
  182. */
  183. goto out_put;
  184. status = vfs_mkdir(dir->d_inode, dentry, S_IRWXU);
  185. out_put:
  186. dput(dentry);
  187. out_unlock:
  188. mutex_unlock(&dir->d_inode->i_mutex);
  189. if (status == 0) {
  190. if (nn->in_grace) {
  191. crp = nfs4_client_to_reclaim(dname, nn);
  192. if (crp)
  193. crp->cr_clp = clp;
  194. }
  195. vfs_fsync(nn->rec_file, 0);
  196. } else {
  197. printk(KERN_ERR "NFSD: failed to write recovery record"
  198. " (err %d); please check that %s exists"
  199. " and is writeable", status,
  200. user_recovery_dirname);
  201. }
  202. mnt_drop_write_file(nn->rec_file);
  203. out_creds:
  204. nfs4_reset_creds(original_cred);
  205. }
  206. typedef int (recdir_func)(struct dentry *, struct dentry *, struct nfsd_net *);
  207. struct name_list {
  208. char name[HEXDIR_LEN];
  209. struct list_head list;
  210. };
  211. struct nfs4_dir_ctx {
  212. struct dir_context ctx;
  213. struct list_head names;
  214. };
  215. static int
  216. nfsd4_build_namelist(void *arg, const char *name, int namlen,
  217. loff_t offset, u64 ino, unsigned int d_type)
  218. {
  219. struct nfs4_dir_ctx *ctx = arg;
  220. struct name_list *entry;
  221. if (namlen != HEXDIR_LEN - 1)
  222. return 0;
  223. entry = kmalloc(sizeof(struct name_list), GFP_KERNEL);
  224. if (entry == NULL)
  225. return -ENOMEM;
  226. memcpy(entry->name, name, HEXDIR_LEN - 1);
  227. entry->name[HEXDIR_LEN - 1] = '\0';
  228. list_add(&entry->list, &ctx->names);
  229. return 0;
  230. }
  231. static int
  232. nfsd4_list_rec_dir(recdir_func *f, struct nfsd_net *nn)
  233. {
  234. const struct cred *original_cred;
  235. struct dentry *dir = nn->rec_file->f_path.dentry;
  236. struct nfs4_dir_ctx ctx = {
  237. .ctx.actor = nfsd4_build_namelist,
  238. .names = LIST_HEAD_INIT(ctx.names)
  239. };
  240. int status;
  241. status = nfs4_save_creds(&original_cred);
  242. if (status < 0)
  243. return status;
  244. status = vfs_llseek(nn->rec_file, 0, SEEK_SET);
  245. if (status < 0) {
  246. nfs4_reset_creds(original_cred);
  247. return status;
  248. }
  249. status = iterate_dir(nn->rec_file, &ctx.ctx);
  250. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
  251. while (!list_empty(&ctx.names)) {
  252. struct name_list *entry;
  253. entry = list_entry(ctx.names.next, struct name_list, list);
  254. if (!status) {
  255. struct dentry *dentry;
  256. dentry = lookup_one_len(entry->name, dir, HEXDIR_LEN-1);
  257. if (IS_ERR(dentry)) {
  258. status = PTR_ERR(dentry);
  259. break;
  260. }
  261. status = f(dir, dentry, nn);
  262. dput(dentry);
  263. }
  264. list_del(&entry->list);
  265. kfree(entry);
  266. }
  267. mutex_unlock(&dir->d_inode->i_mutex);
  268. nfs4_reset_creds(original_cred);
  269. return status;
  270. }
  271. static int
  272. nfsd4_unlink_clid_dir(char *name, int namlen, struct nfsd_net *nn)
  273. {
  274. struct dentry *dir, *dentry;
  275. int status;
  276. dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name);
  277. dir = nn->rec_file->f_path.dentry;
  278. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
  279. dentry = lookup_one_len(name, dir, namlen);
  280. if (IS_ERR(dentry)) {
  281. status = PTR_ERR(dentry);
  282. goto out_unlock;
  283. }
  284. status = -ENOENT;
  285. if (!dentry->d_inode)
  286. goto out;
  287. status = vfs_rmdir(dir->d_inode, dentry);
  288. out:
  289. dput(dentry);
  290. out_unlock:
  291. mutex_unlock(&dir->d_inode->i_mutex);
  292. return status;
  293. }
  294. static void
  295. nfsd4_remove_clid_dir(struct nfs4_client *clp)
  296. {
  297. const struct cred *original_cred;
  298. struct nfs4_client_reclaim *crp;
  299. char dname[HEXDIR_LEN];
  300. int status;
  301. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  302. if (!nn->rec_file || !test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  303. return;
  304. status = nfs4_make_rec_clidname(dname, &clp->cl_name);
  305. if (status)
  306. return legacy_recdir_name_error(clp, status);
  307. status = mnt_want_write_file(nn->rec_file);
  308. if (status)
  309. goto out;
  310. clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  311. status = nfs4_save_creds(&original_cred);
  312. if (status < 0)
  313. goto out_drop_write;
  314. status = nfsd4_unlink_clid_dir(dname, HEXDIR_LEN-1, nn);
  315. nfs4_reset_creds(original_cred);
  316. if (status == 0) {
  317. vfs_fsync(nn->rec_file, 0);
  318. if (nn->in_grace) {
  319. /* remove reclaim record */
  320. crp = nfsd4_find_reclaim_client(dname, nn);
  321. if (crp)
  322. nfs4_remove_reclaim_record(crp, nn);
  323. }
  324. }
  325. out_drop_write:
  326. mnt_drop_write_file(nn->rec_file);
  327. out:
  328. if (status)
  329. printk("NFSD: Failed to remove expired client state directory"
  330. " %.*s\n", HEXDIR_LEN, dname);
  331. }
  332. static int
  333. purge_old(struct dentry *parent, struct dentry *child, struct nfsd_net *nn)
  334. {
  335. int status;
  336. if (nfs4_has_reclaimed_state(child->d_name.name, nn))
  337. return 0;
  338. status = vfs_rmdir(parent->d_inode, child);
  339. if (status)
  340. printk("failed to remove client recovery directory %pd\n",
  341. child);
  342. /* Keep trying, success or failure: */
  343. return 0;
  344. }
  345. static void
  346. nfsd4_recdir_purge_old(struct nfsd_net *nn)
  347. {
  348. int status;
  349. nn->in_grace = false;
  350. if (!nn->rec_file)
  351. return;
  352. status = mnt_want_write_file(nn->rec_file);
  353. if (status)
  354. goto out;
  355. status = nfsd4_list_rec_dir(purge_old, nn);
  356. if (status == 0)
  357. vfs_fsync(nn->rec_file, 0);
  358. mnt_drop_write_file(nn->rec_file);
  359. out:
  360. nfs4_release_reclaim(nn);
  361. if (status)
  362. printk("nfsd4: failed to purge old clients from recovery"
  363. " directory %pD\n", nn->rec_file);
  364. }
  365. static int
  366. load_recdir(struct dentry *parent, struct dentry *child, struct nfsd_net *nn)
  367. {
  368. if (child->d_name.len != HEXDIR_LEN - 1) {
  369. printk("nfsd4: illegal name %pd in recovery directory\n",
  370. child);
  371. /* Keep trying; maybe the others are OK: */
  372. return 0;
  373. }
  374. nfs4_client_to_reclaim(child->d_name.name, nn);
  375. return 0;
  376. }
  377. static int
  378. nfsd4_recdir_load(struct net *net) {
  379. int status;
  380. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  381. if (!nn->rec_file)
  382. return 0;
  383. status = nfsd4_list_rec_dir(load_recdir, nn);
  384. if (status)
  385. printk("nfsd4: failed loading clients from recovery"
  386. " directory %pD\n", nn->rec_file);
  387. return status;
  388. }
  389. /*
  390. * Hold reference to the recovery directory.
  391. */
  392. static int
  393. nfsd4_init_recdir(struct net *net)
  394. {
  395. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  396. const struct cred *original_cred;
  397. int status;
  398. printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
  399. user_recovery_dirname);
  400. BUG_ON(nn->rec_file);
  401. status = nfs4_save_creds(&original_cred);
  402. if (status < 0) {
  403. printk("NFSD: Unable to change credentials to find recovery"
  404. " directory: error %d\n",
  405. status);
  406. return status;
  407. }
  408. nn->rec_file = filp_open(user_recovery_dirname, O_RDONLY | O_DIRECTORY, 0);
  409. if (IS_ERR(nn->rec_file)) {
  410. printk("NFSD: unable to find recovery directory %s\n",
  411. user_recovery_dirname);
  412. status = PTR_ERR(nn->rec_file);
  413. nn->rec_file = NULL;
  414. }
  415. nfs4_reset_creds(original_cred);
  416. if (!status)
  417. nn->in_grace = true;
  418. return status;
  419. }
  420. static void
  421. nfsd4_shutdown_recdir(struct net *net)
  422. {
  423. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  424. if (!nn->rec_file)
  425. return;
  426. fput(nn->rec_file);
  427. nn->rec_file = NULL;
  428. }
  429. static int
  430. nfs4_legacy_state_init(struct net *net)
  431. {
  432. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  433. int i;
  434. nn->reclaim_str_hashtbl = kmalloc(sizeof(struct list_head) *
  435. CLIENT_HASH_SIZE, GFP_KERNEL);
  436. if (!nn->reclaim_str_hashtbl)
  437. return -ENOMEM;
  438. for (i = 0; i < CLIENT_HASH_SIZE; i++)
  439. INIT_LIST_HEAD(&nn->reclaim_str_hashtbl[i]);
  440. nn->reclaim_str_hashtbl_size = 0;
  441. return 0;
  442. }
  443. static void
  444. nfs4_legacy_state_shutdown(struct net *net)
  445. {
  446. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  447. kfree(nn->reclaim_str_hashtbl);
  448. }
  449. static int
  450. nfsd4_load_reboot_recovery_data(struct net *net)
  451. {
  452. int status;
  453. status = nfsd4_init_recdir(net);
  454. if (status)
  455. return status;
  456. status = nfsd4_recdir_load(net);
  457. if (status)
  458. nfsd4_shutdown_recdir(net);
  459. return status;
  460. }
  461. static int
  462. nfsd4_legacy_tracking_init(struct net *net)
  463. {
  464. int status;
  465. /* XXX: The legacy code won't work in a container */
  466. if (net != &init_net) {
  467. WARN(1, KERN_ERR "NFSD: attempt to initialize legacy client "
  468. "tracking in a container!\n");
  469. return -EINVAL;
  470. }
  471. status = nfs4_legacy_state_init(net);
  472. if (status)
  473. return status;
  474. status = nfsd4_load_reboot_recovery_data(net);
  475. if (status)
  476. goto err;
  477. return 0;
  478. err:
  479. nfs4_legacy_state_shutdown(net);
  480. return status;
  481. }
  482. static void
  483. nfsd4_legacy_tracking_exit(struct net *net)
  484. {
  485. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  486. nfs4_release_reclaim(nn);
  487. nfsd4_shutdown_recdir(net);
  488. nfs4_legacy_state_shutdown(net);
  489. }
  490. /*
  491. * Change the NFSv4 recovery directory to recdir.
  492. */
  493. int
  494. nfs4_reset_recoverydir(char *recdir)
  495. {
  496. int status;
  497. struct path path;
  498. status = kern_path(recdir, LOOKUP_FOLLOW, &path);
  499. if (status)
  500. return status;
  501. status = -ENOTDIR;
  502. if (S_ISDIR(path.dentry->d_inode->i_mode)) {
  503. strcpy(user_recovery_dirname, recdir);
  504. status = 0;
  505. }
  506. path_put(&path);
  507. return status;
  508. }
  509. char *
  510. nfs4_recoverydir(void)
  511. {
  512. return user_recovery_dirname;
  513. }
  514. static int
  515. nfsd4_check_legacy_client(struct nfs4_client *clp)
  516. {
  517. int status;
  518. char dname[HEXDIR_LEN];
  519. struct nfs4_client_reclaim *crp;
  520. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  521. /* did we already find that this client is stable? */
  522. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  523. return 0;
  524. status = nfs4_make_rec_clidname(dname, &clp->cl_name);
  525. if (status) {
  526. legacy_recdir_name_error(clp, status);
  527. return status;
  528. }
  529. /* look for it in the reclaim hashtable otherwise */
  530. crp = nfsd4_find_reclaim_client(dname, nn);
  531. if (crp) {
  532. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  533. crp->cr_clp = clp;
  534. return 0;
  535. }
  536. return -ENOENT;
  537. }
  538. static struct nfsd4_client_tracking_ops nfsd4_legacy_tracking_ops = {
  539. .init = nfsd4_legacy_tracking_init,
  540. .exit = nfsd4_legacy_tracking_exit,
  541. .create = nfsd4_create_clid_dir,
  542. .remove = nfsd4_remove_clid_dir,
  543. .check = nfsd4_check_legacy_client,
  544. .grace_done = nfsd4_recdir_purge_old,
  545. };
  546. /* Globals */
  547. #define NFSD_PIPE_DIR "nfsd"
  548. #define NFSD_CLD_PIPE "cld"
  549. /* per-net-ns structure for holding cld upcall info */
  550. struct cld_net {
  551. struct rpc_pipe *cn_pipe;
  552. spinlock_t cn_lock;
  553. struct list_head cn_list;
  554. unsigned int cn_xid;
  555. };
  556. struct cld_upcall {
  557. struct list_head cu_list;
  558. struct cld_net *cu_net;
  559. struct task_struct *cu_task;
  560. struct cld_msg cu_msg;
  561. };
  562. static int
  563. __cld_pipe_upcall(struct rpc_pipe *pipe, struct cld_msg *cmsg)
  564. {
  565. int ret;
  566. struct rpc_pipe_msg msg;
  567. memset(&msg, 0, sizeof(msg));
  568. msg.data = cmsg;
  569. msg.len = sizeof(*cmsg);
  570. /*
  571. * Set task state before we queue the upcall. That prevents
  572. * wake_up_process in the downcall from racing with schedule.
  573. */
  574. set_current_state(TASK_UNINTERRUPTIBLE);
  575. ret = rpc_queue_upcall(pipe, &msg);
  576. if (ret < 0) {
  577. set_current_state(TASK_RUNNING);
  578. goto out;
  579. }
  580. schedule();
  581. if (msg.errno < 0)
  582. ret = msg.errno;
  583. out:
  584. return ret;
  585. }
  586. static int
  587. cld_pipe_upcall(struct rpc_pipe *pipe, struct cld_msg *cmsg)
  588. {
  589. int ret;
  590. /*
  591. * -EAGAIN occurs when pipe is closed and reopened while there are
  592. * upcalls queued.
  593. */
  594. do {
  595. ret = __cld_pipe_upcall(pipe, cmsg);
  596. } while (ret == -EAGAIN);
  597. return ret;
  598. }
  599. static ssize_t
  600. cld_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
  601. {
  602. struct cld_upcall *tmp, *cup;
  603. struct cld_msg __user *cmsg = (struct cld_msg __user *)src;
  604. uint32_t xid;
  605. struct nfsd_net *nn = net_generic(filp->f_dentry->d_sb->s_fs_info,
  606. nfsd_net_id);
  607. struct cld_net *cn = nn->cld_net;
  608. if (mlen != sizeof(*cmsg)) {
  609. dprintk("%s: got %zu bytes, expected %zu\n", __func__, mlen,
  610. sizeof(*cmsg));
  611. return -EINVAL;
  612. }
  613. /* copy just the xid so we can try to find that */
  614. if (copy_from_user(&xid, &cmsg->cm_xid, sizeof(xid)) != 0) {
  615. dprintk("%s: error when copying xid from userspace", __func__);
  616. return -EFAULT;
  617. }
  618. /* walk the list and find corresponding xid */
  619. cup = NULL;
  620. spin_lock(&cn->cn_lock);
  621. list_for_each_entry(tmp, &cn->cn_list, cu_list) {
  622. if (get_unaligned(&tmp->cu_msg.cm_xid) == xid) {
  623. cup = tmp;
  624. list_del_init(&cup->cu_list);
  625. break;
  626. }
  627. }
  628. spin_unlock(&cn->cn_lock);
  629. /* couldn't find upcall? */
  630. if (!cup) {
  631. dprintk("%s: couldn't find upcall -- xid=%u\n", __func__, xid);
  632. return -EINVAL;
  633. }
  634. if (copy_from_user(&cup->cu_msg, src, mlen) != 0)
  635. return -EFAULT;
  636. wake_up_process(cup->cu_task);
  637. return mlen;
  638. }
  639. static void
  640. cld_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  641. {
  642. struct cld_msg *cmsg = msg->data;
  643. struct cld_upcall *cup = container_of(cmsg, struct cld_upcall,
  644. cu_msg);
  645. /* errno >= 0 means we got a downcall */
  646. if (msg->errno >= 0)
  647. return;
  648. wake_up_process(cup->cu_task);
  649. }
  650. static const struct rpc_pipe_ops cld_upcall_ops = {
  651. .upcall = rpc_pipe_generic_upcall,
  652. .downcall = cld_pipe_downcall,
  653. .destroy_msg = cld_pipe_destroy_msg,
  654. };
  655. static struct dentry *
  656. nfsd4_cld_register_sb(struct super_block *sb, struct rpc_pipe *pipe)
  657. {
  658. struct dentry *dir, *dentry;
  659. dir = rpc_d_lookup_sb(sb, NFSD_PIPE_DIR);
  660. if (dir == NULL)
  661. return ERR_PTR(-ENOENT);
  662. dentry = rpc_mkpipe_dentry(dir, NFSD_CLD_PIPE, NULL, pipe);
  663. dput(dir);
  664. return dentry;
  665. }
  666. static void
  667. nfsd4_cld_unregister_sb(struct rpc_pipe *pipe)
  668. {
  669. if (pipe->dentry)
  670. rpc_unlink(pipe->dentry);
  671. }
  672. static struct dentry *
  673. nfsd4_cld_register_net(struct net *net, struct rpc_pipe *pipe)
  674. {
  675. struct super_block *sb;
  676. struct dentry *dentry;
  677. sb = rpc_get_sb_net(net);
  678. if (!sb)
  679. return NULL;
  680. dentry = nfsd4_cld_register_sb(sb, pipe);
  681. rpc_put_sb_net(net);
  682. return dentry;
  683. }
  684. static void
  685. nfsd4_cld_unregister_net(struct net *net, struct rpc_pipe *pipe)
  686. {
  687. struct super_block *sb;
  688. sb = rpc_get_sb_net(net);
  689. if (sb) {
  690. nfsd4_cld_unregister_sb(pipe);
  691. rpc_put_sb_net(net);
  692. }
  693. }
  694. /* Initialize rpc_pipefs pipe for communication with client tracking daemon */
  695. static int
  696. nfsd4_init_cld_pipe(struct net *net)
  697. {
  698. int ret;
  699. struct dentry *dentry;
  700. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  701. struct cld_net *cn;
  702. if (nn->cld_net)
  703. return 0;
  704. cn = kzalloc(sizeof(*cn), GFP_KERNEL);
  705. if (!cn) {
  706. ret = -ENOMEM;
  707. goto err;
  708. }
  709. cn->cn_pipe = rpc_mkpipe_data(&cld_upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
  710. if (IS_ERR(cn->cn_pipe)) {
  711. ret = PTR_ERR(cn->cn_pipe);
  712. goto err;
  713. }
  714. spin_lock_init(&cn->cn_lock);
  715. INIT_LIST_HEAD(&cn->cn_list);
  716. dentry = nfsd4_cld_register_net(net, cn->cn_pipe);
  717. if (IS_ERR(dentry)) {
  718. ret = PTR_ERR(dentry);
  719. goto err_destroy_data;
  720. }
  721. cn->cn_pipe->dentry = dentry;
  722. nn->cld_net = cn;
  723. return 0;
  724. err_destroy_data:
  725. rpc_destroy_pipe_data(cn->cn_pipe);
  726. err:
  727. kfree(cn);
  728. printk(KERN_ERR "NFSD: unable to create nfsdcld upcall pipe (%d)\n",
  729. ret);
  730. return ret;
  731. }
  732. static void
  733. nfsd4_remove_cld_pipe(struct net *net)
  734. {
  735. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  736. struct cld_net *cn = nn->cld_net;
  737. nfsd4_cld_unregister_net(net, cn->cn_pipe);
  738. rpc_destroy_pipe_data(cn->cn_pipe);
  739. kfree(nn->cld_net);
  740. nn->cld_net = NULL;
  741. }
  742. static struct cld_upcall *
  743. alloc_cld_upcall(struct cld_net *cn)
  744. {
  745. struct cld_upcall *new, *tmp;
  746. new = kzalloc(sizeof(*new), GFP_KERNEL);
  747. if (!new)
  748. return new;
  749. /* FIXME: hard cap on number in flight? */
  750. restart_search:
  751. spin_lock(&cn->cn_lock);
  752. list_for_each_entry(tmp, &cn->cn_list, cu_list) {
  753. if (tmp->cu_msg.cm_xid == cn->cn_xid) {
  754. cn->cn_xid++;
  755. spin_unlock(&cn->cn_lock);
  756. goto restart_search;
  757. }
  758. }
  759. new->cu_task = current;
  760. new->cu_msg.cm_vers = CLD_UPCALL_VERSION;
  761. put_unaligned(cn->cn_xid++, &new->cu_msg.cm_xid);
  762. new->cu_net = cn;
  763. list_add(&new->cu_list, &cn->cn_list);
  764. spin_unlock(&cn->cn_lock);
  765. dprintk("%s: allocated xid %u\n", __func__, new->cu_msg.cm_xid);
  766. return new;
  767. }
  768. static void
  769. free_cld_upcall(struct cld_upcall *victim)
  770. {
  771. struct cld_net *cn = victim->cu_net;
  772. spin_lock(&cn->cn_lock);
  773. list_del(&victim->cu_list);
  774. spin_unlock(&cn->cn_lock);
  775. kfree(victim);
  776. }
  777. /* Ask daemon to create a new record */
  778. static void
  779. nfsd4_cld_create(struct nfs4_client *clp)
  780. {
  781. int ret;
  782. struct cld_upcall *cup;
  783. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  784. struct cld_net *cn = nn->cld_net;
  785. /* Don't upcall if it's already stored */
  786. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  787. return;
  788. cup = alloc_cld_upcall(cn);
  789. if (!cup) {
  790. ret = -ENOMEM;
  791. goto out_err;
  792. }
  793. cup->cu_msg.cm_cmd = Cld_Create;
  794. cup->cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
  795. memcpy(cup->cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
  796. clp->cl_name.len);
  797. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
  798. if (!ret) {
  799. ret = cup->cu_msg.cm_status;
  800. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  801. }
  802. free_cld_upcall(cup);
  803. out_err:
  804. if (ret)
  805. printk(KERN_ERR "NFSD: Unable to create client "
  806. "record on stable storage: %d\n", ret);
  807. }
  808. /* Ask daemon to create a new record */
  809. static void
  810. nfsd4_cld_remove(struct nfs4_client *clp)
  811. {
  812. int ret;
  813. struct cld_upcall *cup;
  814. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  815. struct cld_net *cn = nn->cld_net;
  816. /* Don't upcall if it's already removed */
  817. if (!test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  818. return;
  819. cup = alloc_cld_upcall(cn);
  820. if (!cup) {
  821. ret = -ENOMEM;
  822. goto out_err;
  823. }
  824. cup->cu_msg.cm_cmd = Cld_Remove;
  825. cup->cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
  826. memcpy(cup->cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
  827. clp->cl_name.len);
  828. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
  829. if (!ret) {
  830. ret = cup->cu_msg.cm_status;
  831. clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  832. }
  833. free_cld_upcall(cup);
  834. out_err:
  835. if (ret)
  836. printk(KERN_ERR "NFSD: Unable to remove client "
  837. "record from stable storage: %d\n", ret);
  838. }
  839. /* Check for presence of a record, and update its timestamp */
  840. static int
  841. nfsd4_cld_check(struct nfs4_client *clp)
  842. {
  843. int ret;
  844. struct cld_upcall *cup;
  845. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  846. struct cld_net *cn = nn->cld_net;
  847. /* Don't upcall if one was already stored during this grace pd */
  848. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  849. return 0;
  850. cup = alloc_cld_upcall(cn);
  851. if (!cup) {
  852. printk(KERN_ERR "NFSD: Unable to check client record on "
  853. "stable storage: %d\n", -ENOMEM);
  854. return -ENOMEM;
  855. }
  856. cup->cu_msg.cm_cmd = Cld_Check;
  857. cup->cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
  858. memcpy(cup->cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
  859. clp->cl_name.len);
  860. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
  861. if (!ret) {
  862. ret = cup->cu_msg.cm_status;
  863. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  864. }
  865. free_cld_upcall(cup);
  866. return ret;
  867. }
  868. static void
  869. nfsd4_cld_grace_done(struct nfsd_net *nn)
  870. {
  871. int ret;
  872. struct cld_upcall *cup;
  873. struct cld_net *cn = nn->cld_net;
  874. cup = alloc_cld_upcall(cn);
  875. if (!cup) {
  876. ret = -ENOMEM;
  877. goto out_err;
  878. }
  879. cup->cu_msg.cm_cmd = Cld_GraceDone;
  880. cup->cu_msg.cm_u.cm_gracetime = (int64_t)nn->boot_time;
  881. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
  882. if (!ret)
  883. ret = cup->cu_msg.cm_status;
  884. free_cld_upcall(cup);
  885. out_err:
  886. if (ret)
  887. printk(KERN_ERR "NFSD: Unable to end grace period: %d\n", ret);
  888. }
  889. static struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops = {
  890. .init = nfsd4_init_cld_pipe,
  891. .exit = nfsd4_remove_cld_pipe,
  892. .create = nfsd4_cld_create,
  893. .remove = nfsd4_cld_remove,
  894. .check = nfsd4_cld_check,
  895. .grace_done = nfsd4_cld_grace_done,
  896. };
  897. /* upcall via usermodehelper */
  898. static char cltrack_prog[PATH_MAX] = "/sbin/nfsdcltrack";
  899. module_param_string(cltrack_prog, cltrack_prog, sizeof(cltrack_prog),
  900. S_IRUGO|S_IWUSR);
  901. MODULE_PARM_DESC(cltrack_prog, "Path to the nfsdcltrack upcall program");
  902. static bool cltrack_legacy_disable;
  903. module_param(cltrack_legacy_disable, bool, S_IRUGO|S_IWUSR);
  904. MODULE_PARM_DESC(cltrack_legacy_disable,
  905. "Disable legacy recoverydir conversion. Default: false");
  906. #define LEGACY_TOPDIR_ENV_PREFIX "NFSDCLTRACK_LEGACY_TOPDIR="
  907. #define LEGACY_RECDIR_ENV_PREFIX "NFSDCLTRACK_LEGACY_RECDIR="
  908. #define HAS_SESSION_ENV_PREFIX "NFSDCLTRACK_CLIENT_HAS_SESSION="
  909. #define GRACE_START_ENV_PREFIX "NFSDCLTRACK_GRACE_START="
  910. static char *
  911. nfsd4_cltrack_legacy_topdir(void)
  912. {
  913. int copied;
  914. size_t len;
  915. char *result;
  916. if (cltrack_legacy_disable)
  917. return NULL;
  918. len = strlen(LEGACY_TOPDIR_ENV_PREFIX) +
  919. strlen(nfs4_recoverydir()) + 1;
  920. result = kmalloc(len, GFP_KERNEL);
  921. if (!result)
  922. return result;
  923. copied = snprintf(result, len, LEGACY_TOPDIR_ENV_PREFIX "%s",
  924. nfs4_recoverydir());
  925. if (copied >= len) {
  926. /* just return nothing if output was truncated */
  927. kfree(result);
  928. return NULL;
  929. }
  930. return result;
  931. }
  932. static char *
  933. nfsd4_cltrack_legacy_recdir(const struct xdr_netobj *name)
  934. {
  935. int copied;
  936. size_t len;
  937. char *result;
  938. if (cltrack_legacy_disable)
  939. return NULL;
  940. /* +1 is for '/' between "topdir" and "recdir" */
  941. len = strlen(LEGACY_RECDIR_ENV_PREFIX) +
  942. strlen(nfs4_recoverydir()) + 1 + HEXDIR_LEN;
  943. result = kmalloc(len, GFP_KERNEL);
  944. if (!result)
  945. return result;
  946. copied = snprintf(result, len, LEGACY_RECDIR_ENV_PREFIX "%s/",
  947. nfs4_recoverydir());
  948. if (copied > (len - HEXDIR_LEN)) {
  949. /* just return nothing if output will be truncated */
  950. kfree(result);
  951. return NULL;
  952. }
  953. copied = nfs4_make_rec_clidname(result + copied, name);
  954. if (copied) {
  955. kfree(result);
  956. return NULL;
  957. }
  958. return result;
  959. }
  960. static char *
  961. nfsd4_cltrack_client_has_session(struct nfs4_client *clp)
  962. {
  963. int copied;
  964. size_t len;
  965. char *result;
  966. /* prefix + Y/N character + terminating NULL */
  967. len = strlen(HAS_SESSION_ENV_PREFIX) + 1 + 1;
  968. result = kmalloc(len, GFP_KERNEL);
  969. if (!result)
  970. return result;
  971. copied = snprintf(result, len, HAS_SESSION_ENV_PREFIX "%c",
  972. clp->cl_minorversion ? 'Y' : 'N');
  973. if (copied >= len) {
  974. /* just return nothing if output was truncated */
  975. kfree(result);
  976. return NULL;
  977. }
  978. return result;
  979. }
  980. static char *
  981. nfsd4_cltrack_grace_start(time_t grace_start)
  982. {
  983. int copied;
  984. size_t len;
  985. char *result;
  986. /* prefix + max width of int64_t string + terminating NULL */
  987. len = strlen(GRACE_START_ENV_PREFIX) + 22 + 1;
  988. result = kmalloc(len, GFP_KERNEL);
  989. if (!result)
  990. return result;
  991. copied = snprintf(result, len, GRACE_START_ENV_PREFIX "%ld",
  992. grace_start);
  993. if (copied >= len) {
  994. /* just return nothing if output was truncated */
  995. kfree(result);
  996. return NULL;
  997. }
  998. return result;
  999. }
  1000. static int
  1001. nfsd4_umh_cltrack_upcall(char *cmd, char *arg, char *env0, char *env1)
  1002. {
  1003. char *envp[3];
  1004. char *argv[4];
  1005. int ret;
  1006. if (unlikely(!cltrack_prog[0])) {
  1007. dprintk("%s: cltrack_prog is disabled\n", __func__);
  1008. return -EACCES;
  1009. }
  1010. dprintk("%s: cmd: %s\n", __func__, cmd);
  1011. dprintk("%s: arg: %s\n", __func__, arg ? arg : "(null)");
  1012. dprintk("%s: env0: %s\n", __func__, env0 ? env0 : "(null)");
  1013. dprintk("%s: env1: %s\n", __func__, env1 ? env1 : "(null)");
  1014. envp[0] = env0;
  1015. envp[1] = env1;
  1016. envp[2] = NULL;
  1017. argv[0] = (char *)cltrack_prog;
  1018. argv[1] = cmd;
  1019. argv[2] = arg;
  1020. argv[3] = NULL;
  1021. ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
  1022. /*
  1023. * Disable the upcall mechanism if we're getting an ENOENT or EACCES
  1024. * error. The admin can re-enable it on the fly by using sysfs
  1025. * once the problem has been fixed.
  1026. */
  1027. if (ret == -ENOENT || ret == -EACCES) {
  1028. dprintk("NFSD: %s was not found or isn't executable (%d). "
  1029. "Setting cltrack_prog to blank string!",
  1030. cltrack_prog, ret);
  1031. cltrack_prog[0] = '\0';
  1032. }
  1033. dprintk("%s: %s return value: %d\n", __func__, cltrack_prog, ret);
  1034. return ret;
  1035. }
  1036. static char *
  1037. bin_to_hex_dup(const unsigned char *src, int srclen)
  1038. {
  1039. int i;
  1040. char *buf, *hex;
  1041. /* +1 for terminating NULL */
  1042. buf = kmalloc((srclen * 2) + 1, GFP_KERNEL);
  1043. if (!buf)
  1044. return buf;
  1045. hex = buf;
  1046. for (i = 0; i < srclen; i++) {
  1047. sprintf(hex, "%2.2x", *src++);
  1048. hex += 2;
  1049. }
  1050. return buf;
  1051. }
  1052. static int
  1053. nfsd4_umh_cltrack_init(struct net *net)
  1054. {
  1055. int ret;
  1056. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1057. char *grace_start = nfsd4_cltrack_grace_start(nn->boot_time);
  1058. /* XXX: The usermode helper s not working in container yet. */
  1059. if (net != &init_net) {
  1060. WARN(1, KERN_ERR "NFSD: attempt to initialize umh client "
  1061. "tracking in a container!\n");
  1062. return -EINVAL;
  1063. }
  1064. ret = nfsd4_umh_cltrack_upcall("init", NULL, grace_start, NULL);
  1065. kfree(grace_start);
  1066. return ret;
  1067. }
  1068. static void
  1069. nfsd4_cltrack_upcall_lock(struct nfs4_client *clp)
  1070. {
  1071. wait_on_bit_lock(&clp->cl_flags, NFSD4_CLIENT_UPCALL_LOCK,
  1072. TASK_UNINTERRUPTIBLE);
  1073. }
  1074. static void
  1075. nfsd4_cltrack_upcall_unlock(struct nfs4_client *clp)
  1076. {
  1077. smp_mb__before_atomic();
  1078. clear_bit(NFSD4_CLIENT_UPCALL_LOCK, &clp->cl_flags);
  1079. smp_mb__after_atomic();
  1080. wake_up_bit(&clp->cl_flags, NFSD4_CLIENT_UPCALL_LOCK);
  1081. }
  1082. static void
  1083. nfsd4_umh_cltrack_create(struct nfs4_client *clp)
  1084. {
  1085. char *hexid, *has_session, *grace_start;
  1086. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1087. /*
  1088. * With v4.0 clients, there's little difference in outcome between a
  1089. * create and check operation, and we can end up calling into this
  1090. * function multiple times per client (once for each openowner). So,
  1091. * for v4.0 clients skip upcalling once the client has been recorded
  1092. * on stable storage.
  1093. *
  1094. * For v4.1+ clients, the outcome of the two operations is different,
  1095. * so we must ensure that we upcall for the create operation. v4.1+
  1096. * clients call this on RECLAIM_COMPLETE though, so we should only end
  1097. * up doing a single create upcall per client.
  1098. */
  1099. if (clp->cl_minorversion == 0 &&
  1100. test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  1101. return;
  1102. hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
  1103. if (!hexid) {
  1104. dprintk("%s: can't allocate memory for upcall!\n", __func__);
  1105. return;
  1106. }
  1107. has_session = nfsd4_cltrack_client_has_session(clp);
  1108. grace_start = nfsd4_cltrack_grace_start(nn->boot_time);
  1109. nfsd4_cltrack_upcall_lock(clp);
  1110. if (!nfsd4_umh_cltrack_upcall("create", hexid, has_session, grace_start))
  1111. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  1112. nfsd4_cltrack_upcall_unlock(clp);
  1113. kfree(has_session);
  1114. kfree(grace_start);
  1115. kfree(hexid);
  1116. }
  1117. static void
  1118. nfsd4_umh_cltrack_remove(struct nfs4_client *clp)
  1119. {
  1120. char *hexid;
  1121. if (!test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  1122. return;
  1123. hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
  1124. if (!hexid) {
  1125. dprintk("%s: can't allocate memory for upcall!\n", __func__);
  1126. return;
  1127. }
  1128. nfsd4_cltrack_upcall_lock(clp);
  1129. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags) &&
  1130. nfsd4_umh_cltrack_upcall("remove", hexid, NULL, NULL) == 0)
  1131. clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  1132. nfsd4_cltrack_upcall_unlock(clp);
  1133. kfree(hexid);
  1134. }
  1135. static int
  1136. nfsd4_umh_cltrack_check(struct nfs4_client *clp)
  1137. {
  1138. int ret;
  1139. char *hexid, *has_session, *legacy;
  1140. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  1141. return 0;
  1142. hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
  1143. if (!hexid) {
  1144. dprintk("%s: can't allocate memory for upcall!\n", __func__);
  1145. return -ENOMEM;
  1146. }
  1147. has_session = nfsd4_cltrack_client_has_session(clp);
  1148. legacy = nfsd4_cltrack_legacy_recdir(&clp->cl_name);
  1149. nfsd4_cltrack_upcall_lock(clp);
  1150. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) {
  1151. ret = 0;
  1152. } else {
  1153. ret = nfsd4_umh_cltrack_upcall("check", hexid, has_session, legacy);
  1154. if (ret == 0)
  1155. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  1156. }
  1157. nfsd4_cltrack_upcall_unlock(clp);
  1158. kfree(has_session);
  1159. kfree(legacy);
  1160. kfree(hexid);
  1161. return ret;
  1162. }
  1163. static void
  1164. nfsd4_umh_cltrack_grace_done(struct nfsd_net *nn)
  1165. {
  1166. char *legacy;
  1167. char timestr[22]; /* FIXME: better way to determine max size? */
  1168. sprintf(timestr, "%ld", nn->boot_time);
  1169. legacy = nfsd4_cltrack_legacy_topdir();
  1170. nfsd4_umh_cltrack_upcall("gracedone", timestr, legacy, NULL);
  1171. kfree(legacy);
  1172. }
  1173. static struct nfsd4_client_tracking_ops nfsd4_umh_tracking_ops = {
  1174. .init = nfsd4_umh_cltrack_init,
  1175. .exit = NULL,
  1176. .create = nfsd4_umh_cltrack_create,
  1177. .remove = nfsd4_umh_cltrack_remove,
  1178. .check = nfsd4_umh_cltrack_check,
  1179. .grace_done = nfsd4_umh_cltrack_grace_done,
  1180. };
  1181. int
  1182. nfsd4_client_tracking_init(struct net *net)
  1183. {
  1184. int status;
  1185. struct path path;
  1186. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1187. /* just run the init if it the method is already decided */
  1188. if (nn->client_tracking_ops)
  1189. goto do_init;
  1190. /*
  1191. * First, try a UMH upcall. It should succeed or fail quickly, so
  1192. * there's little harm in trying that first.
  1193. */
  1194. nn->client_tracking_ops = &nfsd4_umh_tracking_ops;
  1195. status = nn->client_tracking_ops->init(net);
  1196. if (!status)
  1197. return status;
  1198. /*
  1199. * See if the recoverydir exists and is a directory. If it is,
  1200. * then use the legacy ops.
  1201. */
  1202. nn->client_tracking_ops = &nfsd4_legacy_tracking_ops;
  1203. status = kern_path(nfs4_recoverydir(), LOOKUP_FOLLOW, &path);
  1204. if (!status) {
  1205. status = S_ISDIR(path.dentry->d_inode->i_mode);
  1206. path_put(&path);
  1207. if (status)
  1208. goto do_init;
  1209. }
  1210. /* Finally, try to use nfsdcld */
  1211. nn->client_tracking_ops = &nfsd4_cld_tracking_ops;
  1212. printk(KERN_WARNING "NFSD: the nfsdcld client tracking upcall will be "
  1213. "removed in 3.10. Please transition to using "
  1214. "nfsdcltrack.\n");
  1215. do_init:
  1216. status = nn->client_tracking_ops->init(net);
  1217. if (status) {
  1218. printk(KERN_WARNING "NFSD: Unable to initialize client "
  1219. "recovery tracking! (%d)\n", status);
  1220. nn->client_tracking_ops = NULL;
  1221. }
  1222. return status;
  1223. }
  1224. void
  1225. nfsd4_client_tracking_exit(struct net *net)
  1226. {
  1227. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1228. if (nn->client_tracking_ops) {
  1229. if (nn->client_tracking_ops->exit)
  1230. nn->client_tracking_ops->exit(net);
  1231. nn->client_tracking_ops = NULL;
  1232. }
  1233. }
  1234. void
  1235. nfsd4_client_record_create(struct nfs4_client *clp)
  1236. {
  1237. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1238. if (nn->client_tracking_ops)
  1239. nn->client_tracking_ops->create(clp);
  1240. }
  1241. void
  1242. nfsd4_client_record_remove(struct nfs4_client *clp)
  1243. {
  1244. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1245. if (nn->client_tracking_ops)
  1246. nn->client_tracking_ops->remove(clp);
  1247. }
  1248. int
  1249. nfsd4_client_record_check(struct nfs4_client *clp)
  1250. {
  1251. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1252. if (nn->client_tracking_ops)
  1253. return nn->client_tracking_ops->check(clp);
  1254. return -EOPNOTSUPP;
  1255. }
  1256. void
  1257. nfsd4_record_grace_done(struct nfsd_net *nn)
  1258. {
  1259. if (nn->client_tracking_ops)
  1260. nn->client_tracking_ops->grace_done(nn);
  1261. }
  1262. static int
  1263. rpc_pipefs_event(struct notifier_block *nb, unsigned long event, void *ptr)
  1264. {
  1265. struct super_block *sb = ptr;
  1266. struct net *net = sb->s_fs_info;
  1267. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1268. struct cld_net *cn = nn->cld_net;
  1269. struct dentry *dentry;
  1270. int ret = 0;
  1271. if (!try_module_get(THIS_MODULE))
  1272. return 0;
  1273. if (!cn) {
  1274. module_put(THIS_MODULE);
  1275. return 0;
  1276. }
  1277. switch (event) {
  1278. case RPC_PIPEFS_MOUNT:
  1279. dentry = nfsd4_cld_register_sb(sb, cn->cn_pipe);
  1280. if (IS_ERR(dentry)) {
  1281. ret = PTR_ERR(dentry);
  1282. break;
  1283. }
  1284. cn->cn_pipe->dentry = dentry;
  1285. break;
  1286. case RPC_PIPEFS_UMOUNT:
  1287. if (cn->cn_pipe->dentry)
  1288. nfsd4_cld_unregister_sb(cn->cn_pipe);
  1289. break;
  1290. default:
  1291. ret = -ENOTSUPP;
  1292. break;
  1293. }
  1294. module_put(THIS_MODULE);
  1295. return ret;
  1296. }
  1297. static struct notifier_block nfsd4_cld_block = {
  1298. .notifier_call = rpc_pipefs_event,
  1299. };
  1300. int
  1301. register_cld_notifier(void)
  1302. {
  1303. return rpc_pipefs_notifier_register(&nfsd4_cld_block);
  1304. }
  1305. void
  1306. unregister_cld_notifier(void)
  1307. {
  1308. rpc_pipefs_notifier_unregister(&nfsd4_cld_block);
  1309. }