nfs4recover.c 32 KB

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