nfs4recover.c 23 KB

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