nfs4recover.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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 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 int
  64. nfs4_save_creds(const struct cred **original_creds)
  65. {
  66. struct cred *new;
  67. new = prepare_creds();
  68. if (!new)
  69. return -ENOMEM;
  70. new->fsuid = 0;
  71. new->fsgid = 0;
  72. *original_creds = override_creds(new);
  73. put_cred(new);
  74. return 0;
  75. }
  76. static void
  77. nfs4_reset_creds(const struct cred *original)
  78. {
  79. revert_creds(original);
  80. }
  81. static void
  82. md5_to_hex(char *out, char *md5)
  83. {
  84. int i;
  85. for (i=0; i<16; i++) {
  86. unsigned char c = md5[i];
  87. *out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
  88. *out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
  89. }
  90. *out = '\0';
  91. }
  92. __be32
  93. nfs4_make_rec_clidname(char *dname, struct xdr_netobj *clname)
  94. {
  95. struct xdr_netobj cksum;
  96. struct hash_desc desc;
  97. struct scatterlist sg;
  98. __be32 status = nfserr_jukebox;
  99. dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
  100. clname->len, clname->data);
  101. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  102. desc.tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
  103. if (IS_ERR(desc.tfm))
  104. goto out_no_tfm;
  105. cksum.len = crypto_hash_digestsize(desc.tfm);
  106. cksum.data = kmalloc(cksum.len, GFP_KERNEL);
  107. if (cksum.data == NULL)
  108. goto out;
  109. sg_init_one(&sg, clname->data, clname->len);
  110. if (crypto_hash_digest(&desc, &sg, sg.length, cksum.data))
  111. goto out;
  112. md5_to_hex(dname, cksum.data);
  113. status = nfs_ok;
  114. out:
  115. kfree(cksum.data);
  116. crypto_free_hash(desc.tfm);
  117. out_no_tfm:
  118. return status;
  119. }
  120. static void
  121. nfsd4_create_clid_dir(struct nfs4_client *clp)
  122. {
  123. const struct cred *original_cred;
  124. char *dname = clp->cl_recdir;
  125. struct dentry *dir, *dentry;
  126. int status;
  127. dprintk("NFSD: nfsd4_create_clid_dir for \"%s\"\n", dname);
  128. if (test_and_set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  129. return;
  130. if (!rec_file)
  131. return;
  132. status = nfs4_save_creds(&original_cred);
  133. if (status < 0)
  134. return;
  135. dir = rec_file->f_path.dentry;
  136. /* lock the parent */
  137. mutex_lock(&dir->d_inode->i_mutex);
  138. dentry = lookup_one_len(dname, dir, HEXDIR_LEN-1);
  139. if (IS_ERR(dentry)) {
  140. status = PTR_ERR(dentry);
  141. goto out_unlock;
  142. }
  143. if (dentry->d_inode)
  144. /*
  145. * In the 4.1 case, where we're called from
  146. * reclaim_complete(), records from the previous reboot
  147. * may still be left, so this is OK.
  148. *
  149. * In the 4.0 case, we should never get here; but we may
  150. * as well be forgiving and just succeed silently.
  151. */
  152. goto out_put;
  153. status = mnt_want_write_file(rec_file);
  154. if (status)
  155. goto out_put;
  156. status = vfs_mkdir(dir->d_inode, dentry, S_IRWXU);
  157. mnt_drop_write_file(rec_file);
  158. out_put:
  159. dput(dentry);
  160. out_unlock:
  161. mutex_unlock(&dir->d_inode->i_mutex);
  162. if (status == 0)
  163. vfs_fsync(rec_file, 0);
  164. else
  165. printk(KERN_ERR "NFSD: failed to write recovery record"
  166. " (err %d); please check that %s exists"
  167. " and is writeable", status,
  168. user_recovery_dirname);
  169. nfs4_reset_creds(original_cred);
  170. }
  171. typedef int (recdir_func)(struct dentry *, struct dentry *);
  172. struct name_list {
  173. char name[HEXDIR_LEN];
  174. struct list_head list;
  175. };
  176. struct nfs4_dir_ctx {
  177. struct dir_context ctx;
  178. struct list_head names;
  179. };
  180. static int
  181. nfsd4_build_namelist(void *arg, const char *name, int namlen,
  182. loff_t offset, u64 ino, unsigned int d_type)
  183. {
  184. struct nfs4_dir_ctx *ctx = arg;
  185. struct name_list *entry;
  186. if (namlen != HEXDIR_LEN - 1)
  187. return 0;
  188. entry = kmalloc(sizeof(struct name_list), GFP_KERNEL);
  189. if (entry == NULL)
  190. return -ENOMEM;
  191. memcpy(entry->name, name, HEXDIR_LEN - 1);
  192. entry->name[HEXDIR_LEN - 1] = '\0';
  193. list_add(&entry->list, &ctx->names);
  194. return 0;
  195. }
  196. static int
  197. nfsd4_list_rec_dir(recdir_func *f)
  198. {
  199. const struct cred *original_cred;
  200. struct dentry *dir = rec_file->f_path.dentry;
  201. struct nfs4_dir_ctx ctx = {
  202. .ctx.actor = nfsd4_build_namelist,
  203. .names = LIST_HEAD_INIT(ctx.names)
  204. };
  205. int status;
  206. status = nfs4_save_creds(&original_cred);
  207. if (status < 0)
  208. return status;
  209. status = vfs_llseek(rec_file, 0, SEEK_SET);
  210. if (status < 0) {
  211. nfs4_reset_creds(original_cred);
  212. return status;
  213. }
  214. status = iterate_dir(rec_file, &ctx.ctx);
  215. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
  216. while (!list_empty(&ctx.names)) {
  217. struct name_list *entry;
  218. entry = list_entry(ctx.names.next, struct name_list, list);
  219. if (!status) {
  220. struct dentry *dentry;
  221. dentry = lookup_one_len(entry->name, dir, HEXDIR_LEN-1);
  222. if (IS_ERR(dentry)) {
  223. status = PTR_ERR(dentry);
  224. break;
  225. }
  226. status = f(dir, dentry);
  227. dput(dentry);
  228. }
  229. list_del(&entry->list);
  230. kfree(entry);
  231. }
  232. mutex_unlock(&dir->d_inode->i_mutex);
  233. nfs4_reset_creds(original_cred);
  234. return status;
  235. }
  236. static int
  237. nfsd4_unlink_clid_dir(char *name, int namlen)
  238. {
  239. struct dentry *dir, *dentry;
  240. int status;
  241. dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name);
  242. dir = rec_file->f_path.dentry;
  243. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
  244. dentry = lookup_one_len(name, dir, namlen);
  245. if (IS_ERR(dentry)) {
  246. status = PTR_ERR(dentry);
  247. goto out_unlock;
  248. }
  249. status = -ENOENT;
  250. if (!dentry->d_inode)
  251. goto out;
  252. status = vfs_rmdir(dir->d_inode, dentry);
  253. out:
  254. dput(dentry);
  255. out_unlock:
  256. mutex_unlock(&dir->d_inode->i_mutex);
  257. return status;
  258. }
  259. static void
  260. nfsd4_remove_clid_dir(struct nfs4_client *clp)
  261. {
  262. const struct cred *original_cred;
  263. int status;
  264. if (!rec_file || !test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  265. return;
  266. status = mnt_want_write_file(rec_file);
  267. if (status)
  268. goto out;
  269. clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  270. status = nfs4_save_creds(&original_cred);
  271. if (status < 0)
  272. goto out;
  273. status = nfsd4_unlink_clid_dir(clp->cl_recdir, HEXDIR_LEN-1);
  274. nfs4_reset_creds(original_cred);
  275. if (status == 0)
  276. vfs_fsync(rec_file, 0);
  277. mnt_drop_write_file(rec_file);
  278. out:
  279. if (status)
  280. printk("NFSD: Failed to remove expired client state directory"
  281. " %.*s\n", HEXDIR_LEN, clp->cl_recdir);
  282. }
  283. static int
  284. purge_old(struct dentry *parent, struct dentry *child)
  285. {
  286. int status;
  287. if (nfs4_has_reclaimed_state(child->d_name.name, false))
  288. return 0;
  289. status = vfs_rmdir(parent->d_inode, child);
  290. if (status)
  291. printk("failed to remove client recovery directory %s\n",
  292. child->d_name.name);
  293. /* Keep trying, success or failure: */
  294. return 0;
  295. }
  296. static void
  297. nfsd4_recdir_purge_old(struct net *net, time_t boot_time)
  298. {
  299. int status;
  300. if (!rec_file)
  301. return;
  302. status = mnt_want_write_file(rec_file);
  303. if (status)
  304. goto out;
  305. status = nfsd4_list_rec_dir(purge_old);
  306. if (status == 0)
  307. vfs_fsync(rec_file, 0);
  308. mnt_drop_write_file(rec_file);
  309. out:
  310. if (status)
  311. printk("nfsd4: failed to purge old clients from recovery"
  312. " directory %s\n", rec_file->f_path.dentry->d_name.name);
  313. }
  314. static int
  315. load_recdir(struct dentry *parent, struct dentry *child)
  316. {
  317. if (child->d_name.len != HEXDIR_LEN - 1) {
  318. printk("nfsd4: illegal name %s in recovery directory\n",
  319. child->d_name.name);
  320. /* Keep trying; maybe the others are OK: */
  321. return 0;
  322. }
  323. nfs4_client_to_reclaim(child->d_name.name);
  324. return 0;
  325. }
  326. static int
  327. nfsd4_recdir_load(void) {
  328. int status;
  329. if (!rec_file)
  330. return 0;
  331. status = nfsd4_list_rec_dir(load_recdir);
  332. if (status)
  333. printk("nfsd4: failed loading clients from recovery"
  334. " directory %s\n", rec_file->f_path.dentry->d_name.name);
  335. return status;
  336. }
  337. /*
  338. * Hold reference to the recovery directory.
  339. */
  340. static int
  341. nfsd4_init_recdir(void)
  342. {
  343. const struct cred *original_cred;
  344. int status;
  345. printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
  346. user_recovery_dirname);
  347. BUG_ON(rec_file);
  348. status = nfs4_save_creds(&original_cred);
  349. if (status < 0) {
  350. printk("NFSD: Unable to change credentials to find recovery"
  351. " directory: error %d\n",
  352. status);
  353. return status;
  354. }
  355. rec_file = filp_open(user_recovery_dirname, O_RDONLY | O_DIRECTORY, 0);
  356. if (IS_ERR(rec_file)) {
  357. printk("NFSD: unable to find recovery directory %s\n",
  358. user_recovery_dirname);
  359. status = PTR_ERR(rec_file);
  360. rec_file = NULL;
  361. }
  362. nfs4_reset_creds(original_cred);
  363. return status;
  364. }
  365. static int
  366. nfsd4_load_reboot_recovery_data(struct net *net)
  367. {
  368. int status;
  369. /* XXX: The legacy code won't work in a container */
  370. if (net != &init_net) {
  371. WARN(1, KERN_ERR "NFSD: attempt to initialize legacy client "
  372. "tracking in a container!\n");
  373. return -EINVAL;
  374. }
  375. nfs4_lock_state();
  376. status = nfsd4_init_recdir();
  377. if (!status)
  378. status = nfsd4_recdir_load();
  379. nfs4_unlock_state();
  380. if (status)
  381. printk(KERN_ERR "NFSD: Failure reading reboot recovery data\n");
  382. return status;
  383. }
  384. static void
  385. nfsd4_shutdown_recdir(void)
  386. {
  387. if (!rec_file)
  388. return;
  389. fput(rec_file);
  390. rec_file = NULL;
  391. }
  392. static void
  393. nfsd4_legacy_tracking_exit(struct net *net)
  394. {
  395. nfs4_release_reclaim();
  396. nfsd4_shutdown_recdir();
  397. }
  398. /*
  399. * Change the NFSv4 recovery directory to recdir.
  400. */
  401. int
  402. nfs4_reset_recoverydir(char *recdir)
  403. {
  404. int status;
  405. struct path path;
  406. status = kern_path(recdir, LOOKUP_FOLLOW, &path);
  407. if (status)
  408. return status;
  409. status = -ENOTDIR;
  410. if (S_ISDIR(path.dentry->d_inode->i_mode)) {
  411. strcpy(user_recovery_dirname, recdir);
  412. status = 0;
  413. }
  414. path_put(&path);
  415. return status;
  416. }
  417. char *
  418. nfs4_recoverydir(void)
  419. {
  420. return user_recovery_dirname;
  421. }
  422. static int
  423. nfsd4_check_legacy_client(struct nfs4_client *clp)
  424. {
  425. /* did we already find that this client is stable? */
  426. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  427. return 0;
  428. /* look for it in the reclaim hashtable otherwise */
  429. if (nfsd4_find_reclaim_client(clp)) {
  430. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  431. return 0;
  432. }
  433. return -ENOENT;
  434. }
  435. static struct nfsd4_client_tracking_ops nfsd4_legacy_tracking_ops = {
  436. .init = nfsd4_load_reboot_recovery_data,
  437. .exit = nfsd4_legacy_tracking_exit,
  438. .create = nfsd4_create_clid_dir,
  439. .remove = nfsd4_remove_clid_dir,
  440. .check = nfsd4_check_legacy_client,
  441. .grace_done = nfsd4_recdir_purge_old,
  442. };
  443. /* Globals */
  444. #define NFSD_PIPE_DIR "nfsd"
  445. #define NFSD_CLD_PIPE "cld"
  446. /* per-net-ns structure for holding cld upcall info */
  447. struct cld_net {
  448. struct rpc_pipe *cn_pipe;
  449. spinlock_t cn_lock;
  450. struct list_head cn_list;
  451. unsigned int cn_xid;
  452. };
  453. struct cld_upcall {
  454. struct list_head cu_list;
  455. struct cld_net *cu_net;
  456. struct task_struct *cu_task;
  457. struct cld_msg cu_msg;
  458. };
  459. static int
  460. __cld_pipe_upcall(struct rpc_pipe *pipe, struct cld_msg *cmsg)
  461. {
  462. int ret;
  463. struct rpc_pipe_msg msg;
  464. memset(&msg, 0, sizeof(msg));
  465. msg.data = cmsg;
  466. msg.len = sizeof(*cmsg);
  467. /*
  468. * Set task state before we queue the upcall. That prevents
  469. * wake_up_process in the downcall from racing with schedule.
  470. */
  471. set_current_state(TASK_UNINTERRUPTIBLE);
  472. ret = rpc_queue_upcall(pipe, &msg);
  473. if (ret < 0) {
  474. set_current_state(TASK_RUNNING);
  475. goto out;
  476. }
  477. schedule();
  478. set_current_state(TASK_RUNNING);
  479. if (msg.errno < 0)
  480. ret = msg.errno;
  481. out:
  482. return ret;
  483. }
  484. static int
  485. cld_pipe_upcall(struct rpc_pipe *pipe, struct cld_msg *cmsg)
  486. {
  487. int ret;
  488. /*
  489. * -EAGAIN occurs when pipe is closed and reopened while there are
  490. * upcalls queued.
  491. */
  492. do {
  493. ret = __cld_pipe_upcall(pipe, cmsg);
  494. } while (ret == -EAGAIN);
  495. return ret;
  496. }
  497. static ssize_t
  498. cld_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
  499. {
  500. struct cld_upcall *tmp, *cup;
  501. struct cld_msg *cmsg = (struct cld_msg *)src;
  502. uint32_t xid;
  503. struct nfsd_net *nn = net_generic(filp->f_dentry->d_sb->s_fs_info,
  504. nfsd_net_id);
  505. struct cld_net *cn = nn->cld_net;
  506. if (mlen != sizeof(*cmsg)) {
  507. dprintk("%s: got %zu bytes, expected %zu\n", __func__, mlen,
  508. sizeof(*cmsg));
  509. return -EINVAL;
  510. }
  511. /* copy just the xid so we can try to find that */
  512. if (copy_from_user(&xid, &cmsg->cm_xid, sizeof(xid)) != 0) {
  513. dprintk("%s: error when copying xid from userspace", __func__);
  514. return -EFAULT;
  515. }
  516. /* walk the list and find corresponding xid */
  517. cup = NULL;
  518. spin_lock(&cn->cn_lock);
  519. list_for_each_entry(tmp, &cn->cn_list, cu_list) {
  520. if (get_unaligned(&tmp->cu_msg.cm_xid) == xid) {
  521. cup = tmp;
  522. list_del_init(&cup->cu_list);
  523. break;
  524. }
  525. }
  526. spin_unlock(&cn->cn_lock);
  527. /* couldn't find upcall? */
  528. if (!cup) {
  529. dprintk("%s: couldn't find upcall -- xid=%u\n", __func__, xid);
  530. return -EINVAL;
  531. }
  532. if (copy_from_user(&cup->cu_msg, src, mlen) != 0)
  533. return -EFAULT;
  534. wake_up_process(cup->cu_task);
  535. return mlen;
  536. }
  537. static void
  538. cld_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  539. {
  540. struct cld_msg *cmsg = msg->data;
  541. struct cld_upcall *cup = container_of(cmsg, struct cld_upcall,
  542. cu_msg);
  543. /* errno >= 0 means we got a downcall */
  544. if (msg->errno >= 0)
  545. return;
  546. wake_up_process(cup->cu_task);
  547. }
  548. static const struct rpc_pipe_ops cld_upcall_ops = {
  549. .upcall = rpc_pipe_generic_upcall,
  550. .downcall = cld_pipe_downcall,
  551. .destroy_msg = cld_pipe_destroy_msg,
  552. };
  553. static struct dentry *
  554. nfsd4_cld_register_sb(struct super_block *sb, struct rpc_pipe *pipe)
  555. {
  556. struct dentry *dir, *dentry;
  557. dir = rpc_d_lookup_sb(sb, NFSD_PIPE_DIR);
  558. if (dir == NULL)
  559. return ERR_PTR(-ENOENT);
  560. dentry = rpc_mkpipe_dentry(dir, NFSD_CLD_PIPE, NULL, pipe);
  561. dput(dir);
  562. return dentry;
  563. }
  564. static void
  565. nfsd4_cld_unregister_sb(struct rpc_pipe *pipe)
  566. {
  567. if (pipe->dentry)
  568. rpc_unlink(pipe->dentry);
  569. }
  570. static struct dentry *
  571. nfsd4_cld_register_net(struct net *net, struct rpc_pipe *pipe)
  572. {
  573. struct super_block *sb;
  574. struct dentry *dentry;
  575. sb = rpc_get_sb_net(net);
  576. if (!sb)
  577. return NULL;
  578. dentry = nfsd4_cld_register_sb(sb, pipe);
  579. rpc_put_sb_net(net);
  580. return dentry;
  581. }
  582. static void
  583. nfsd4_cld_unregister_net(struct net *net, struct rpc_pipe *pipe)
  584. {
  585. struct super_block *sb;
  586. sb = rpc_get_sb_net(net);
  587. if (sb) {
  588. nfsd4_cld_unregister_sb(pipe);
  589. rpc_put_sb_net(net);
  590. }
  591. }
  592. /* Initialize rpc_pipefs pipe for communication with client tracking daemon */
  593. static int
  594. nfsd4_init_cld_pipe(struct net *net)
  595. {
  596. int ret;
  597. struct dentry *dentry;
  598. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  599. struct cld_net *cn;
  600. if (nn->cld_net)
  601. return 0;
  602. cn = kzalloc(sizeof(*cn), GFP_KERNEL);
  603. if (!cn) {
  604. ret = -ENOMEM;
  605. goto err;
  606. }
  607. cn->cn_pipe = rpc_mkpipe_data(&cld_upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
  608. if (IS_ERR(cn->cn_pipe)) {
  609. ret = PTR_ERR(cn->cn_pipe);
  610. goto err;
  611. }
  612. spin_lock_init(&cn->cn_lock);
  613. INIT_LIST_HEAD(&cn->cn_list);
  614. dentry = nfsd4_cld_register_net(net, cn->cn_pipe);
  615. if (IS_ERR(dentry)) {
  616. ret = PTR_ERR(dentry);
  617. goto err_destroy_data;
  618. }
  619. cn->cn_pipe->dentry = dentry;
  620. nn->cld_net = cn;
  621. return 0;
  622. err_destroy_data:
  623. rpc_destroy_pipe_data(cn->cn_pipe);
  624. err:
  625. kfree(cn);
  626. printk(KERN_ERR "NFSD: unable to create nfsdcld upcall pipe (%d)\n",
  627. ret);
  628. return ret;
  629. }
  630. static void
  631. nfsd4_remove_cld_pipe(struct net *net)
  632. {
  633. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  634. struct cld_net *cn = nn->cld_net;
  635. nfsd4_cld_unregister_net(net, cn->cn_pipe);
  636. rpc_destroy_pipe_data(cn->cn_pipe);
  637. kfree(nn->cld_net);
  638. nn->cld_net = NULL;
  639. }
  640. static struct cld_upcall *
  641. alloc_cld_upcall(struct cld_net *cn)
  642. {
  643. struct cld_upcall *new, *tmp;
  644. new = kzalloc(sizeof(*new), GFP_KERNEL);
  645. if (!new)
  646. return new;
  647. /* FIXME: hard cap on number in flight? */
  648. restart_search:
  649. spin_lock(&cn->cn_lock);
  650. list_for_each_entry(tmp, &cn->cn_list, cu_list) {
  651. if (tmp->cu_msg.cm_xid == cn->cn_xid) {
  652. cn->cn_xid++;
  653. spin_unlock(&cn->cn_lock);
  654. goto restart_search;
  655. }
  656. }
  657. new->cu_task = current;
  658. new->cu_msg.cm_vers = CLD_UPCALL_VERSION;
  659. put_unaligned(cn->cn_xid++, &new->cu_msg.cm_xid);
  660. new->cu_net = cn;
  661. list_add(&new->cu_list, &cn->cn_list);
  662. spin_unlock(&cn->cn_lock);
  663. dprintk("%s: allocated xid %u\n", __func__, new->cu_msg.cm_xid);
  664. return new;
  665. }
  666. static void
  667. free_cld_upcall(struct cld_upcall *victim)
  668. {
  669. struct cld_net *cn = victim->cu_net;
  670. spin_lock(&cn->cn_lock);
  671. list_del(&victim->cu_list);
  672. spin_unlock(&cn->cn_lock);
  673. kfree(victim);
  674. }
  675. /* Ask daemon to create a new record */
  676. static void
  677. nfsd4_cld_create(struct nfs4_client *clp)
  678. {
  679. int ret;
  680. struct cld_upcall *cup;
  681. /* FIXME: determine net from clp */
  682. struct nfsd_net *nn = net_generic(&init_net, nfsd_net_id);
  683. struct cld_net *cn = nn->cld_net;
  684. /* Don't upcall if it's already stored */
  685. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  686. return;
  687. cup = alloc_cld_upcall(cn);
  688. if (!cup) {
  689. ret = -ENOMEM;
  690. goto out_err;
  691. }
  692. cup->cu_msg.cm_cmd = Cld_Create;
  693. cup->cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
  694. memcpy(cup->cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
  695. clp->cl_name.len);
  696. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
  697. if (!ret) {
  698. ret = cup->cu_msg.cm_status;
  699. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  700. }
  701. free_cld_upcall(cup);
  702. out_err:
  703. if (ret)
  704. printk(KERN_ERR "NFSD: Unable to create client "
  705. "record on stable storage: %d\n", ret);
  706. }
  707. /* Ask daemon to create a new record */
  708. static void
  709. nfsd4_cld_remove(struct nfs4_client *clp)
  710. {
  711. int ret;
  712. struct cld_upcall *cup;
  713. /* FIXME: determine net from clp */
  714. struct nfsd_net *nn = net_generic(&init_net, nfsd_net_id);
  715. struct cld_net *cn = nn->cld_net;
  716. /* Don't upcall if it's already removed */
  717. if (!test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  718. return;
  719. cup = alloc_cld_upcall(cn);
  720. if (!cup) {
  721. ret = -ENOMEM;
  722. goto out_err;
  723. }
  724. cup->cu_msg.cm_cmd = Cld_Remove;
  725. cup->cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
  726. memcpy(cup->cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
  727. clp->cl_name.len);
  728. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
  729. if (!ret) {
  730. ret = cup->cu_msg.cm_status;
  731. clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  732. }
  733. free_cld_upcall(cup);
  734. out_err:
  735. if (ret)
  736. printk(KERN_ERR "NFSD: Unable to remove client "
  737. "record from stable storage: %d\n", ret);
  738. }
  739. /* Check for presence of a record, and update its timestamp */
  740. static int
  741. nfsd4_cld_check(struct nfs4_client *clp)
  742. {
  743. int ret;
  744. struct cld_upcall *cup;
  745. /* FIXME: determine net from clp */
  746. struct nfsd_net *nn = net_generic(&init_net, nfsd_net_id);
  747. struct cld_net *cn = nn->cld_net;
  748. /* Don't upcall if one was already stored during this grace pd */
  749. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  750. return 0;
  751. cup = alloc_cld_upcall(cn);
  752. if (!cup) {
  753. printk(KERN_ERR "NFSD: Unable to check client record on "
  754. "stable storage: %d\n", -ENOMEM);
  755. return -ENOMEM;
  756. }
  757. cup->cu_msg.cm_cmd = Cld_Check;
  758. cup->cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
  759. memcpy(cup->cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
  760. clp->cl_name.len);
  761. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
  762. if (!ret) {
  763. ret = cup->cu_msg.cm_status;
  764. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  765. }
  766. free_cld_upcall(cup);
  767. return ret;
  768. }
  769. static void
  770. nfsd4_cld_grace_done(struct net *net, time_t boot_time)
  771. {
  772. int ret;
  773. struct cld_upcall *cup;
  774. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  775. struct cld_net *cn = nn->cld_net;
  776. cup = alloc_cld_upcall(cn);
  777. if (!cup) {
  778. ret = -ENOMEM;
  779. goto out_err;
  780. }
  781. cup->cu_msg.cm_cmd = Cld_GraceDone;
  782. cup->cu_msg.cm_u.cm_gracetime = (int64_t)boot_time;
  783. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
  784. if (!ret)
  785. ret = cup->cu_msg.cm_status;
  786. free_cld_upcall(cup);
  787. out_err:
  788. if (ret)
  789. printk(KERN_ERR "NFSD: Unable to end grace period: %d\n", ret);
  790. }
  791. static struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops = {
  792. .init = nfsd4_init_cld_pipe,
  793. .exit = nfsd4_remove_cld_pipe,
  794. .create = nfsd4_cld_create,
  795. .remove = nfsd4_cld_remove,
  796. .check = nfsd4_cld_check,
  797. .grace_done = nfsd4_cld_grace_done,
  798. };
  799. int
  800. nfsd4_client_tracking_init(struct net *net)
  801. {
  802. int status;
  803. struct path path;
  804. if (!client_tracking_ops) {
  805. client_tracking_ops = &nfsd4_cld_tracking_ops;
  806. status = kern_path(nfs4_recoverydir(), LOOKUP_FOLLOW, &path);
  807. if (!status) {
  808. if (S_ISDIR(path.dentry->d_inode->i_mode))
  809. client_tracking_ops =
  810. &nfsd4_legacy_tracking_ops;
  811. path_put(&path);
  812. }
  813. }
  814. status = client_tracking_ops->init(net);
  815. if (status) {
  816. printk(KERN_WARNING "NFSD: Unable to initialize client "
  817. "recovery tracking! (%d)\n", status);
  818. client_tracking_ops = NULL;
  819. }
  820. return status;
  821. }
  822. void
  823. nfsd4_client_tracking_exit(struct net *net)
  824. {
  825. if (client_tracking_ops) {
  826. client_tracking_ops->exit(net);
  827. client_tracking_ops = NULL;
  828. }
  829. }
  830. void
  831. nfsd4_client_record_create(struct nfs4_client *clp)
  832. {
  833. if (client_tracking_ops)
  834. client_tracking_ops->create(clp);
  835. }
  836. void
  837. nfsd4_client_record_remove(struct nfs4_client *clp)
  838. {
  839. if (client_tracking_ops)
  840. client_tracking_ops->remove(clp);
  841. }
  842. int
  843. nfsd4_client_record_check(struct nfs4_client *clp)
  844. {
  845. if (client_tracking_ops)
  846. return client_tracking_ops->check(clp);
  847. return -EOPNOTSUPP;
  848. }
  849. void
  850. nfsd4_record_grace_done(struct net *net, time_t boot_time)
  851. {
  852. if (client_tracking_ops)
  853. client_tracking_ops->grace_done(net, boot_time);
  854. }
  855. static int
  856. rpc_pipefs_event(struct notifier_block *nb, unsigned long event, void *ptr)
  857. {
  858. struct super_block *sb = ptr;
  859. struct net *net = sb->s_fs_info;
  860. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  861. struct cld_net *cn = nn->cld_net;
  862. struct dentry *dentry;
  863. int ret = 0;
  864. if (!try_module_get(THIS_MODULE))
  865. return 0;
  866. if (!cn) {
  867. module_put(THIS_MODULE);
  868. return 0;
  869. }
  870. switch (event) {
  871. case RPC_PIPEFS_MOUNT:
  872. dentry = nfsd4_cld_register_sb(sb, cn->cn_pipe);
  873. if (IS_ERR(dentry)) {
  874. ret = PTR_ERR(dentry);
  875. break;
  876. }
  877. cn->cn_pipe->dentry = dentry;
  878. break;
  879. case RPC_PIPEFS_UMOUNT:
  880. if (cn->cn_pipe->dentry)
  881. nfsd4_cld_unregister_sb(cn->cn_pipe);
  882. break;
  883. default:
  884. ret = -ENOTSUPP;
  885. break;
  886. }
  887. module_put(THIS_MODULE);
  888. return ret;
  889. }
  890. struct notifier_block nfsd4_cld_block = {
  891. .notifier_call = rpc_pipefs_event,
  892. };
  893. int
  894. register_cld_notifier(void)
  895. {
  896. return rpc_pipefs_notifier_register(&nfsd4_cld_block);
  897. }
  898. void
  899. unregister_cld_notifier(void)
  900. {
  901. rpc_pipefs_notifier_unregister(&nfsd4_cld_block);
  902. }