nfs4recover.c 32 KB

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