nfs4recover.c 31 KB

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