nfs4recover.c 32 KB

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