nfs4recover.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * linux/fs/nfsd/nfs4recover.c
  3. *
  4. * Copyright (c) 2004 The Regents of the University of Michigan.
  5. * All rights reserved.
  6. *
  7. * Andy Adamson <andros@citi.umich.edu>
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the University nor the names of its
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  23. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  24. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  29. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  30. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  31. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. */
  35. #include <linux/sunrpc/svc.h>
  36. #include <linux/nfsd/nfsd.h>
  37. #include <linux/nfs4.h>
  38. #include <linux/nfsd/state.h>
  39. #include <linux/nfsd/xdr4.h>
  40. #include <linux/param.h>
  41. #include <linux/file.h>
  42. #include <linux/namei.h>
  43. #include <asm/uaccess.h>
  44. #include <asm/scatterlist.h>
  45. #include <linux/crypto.h>
  46. #define NFSDDBG_FACILITY NFSDDBG_PROC
  47. /* Globals */
  48. char recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
  49. static struct nameidata rec_dir;
  50. static int rec_dir_init = 0;
  51. static void
  52. nfs4_save_user(uid_t *saveuid, gid_t *savegid)
  53. {
  54. *saveuid = current->fsuid;
  55. *savegid = current->fsgid;
  56. current->fsuid = 0;
  57. current->fsgid = 0;
  58. }
  59. static void
  60. nfs4_reset_user(uid_t saveuid, gid_t savegid)
  61. {
  62. current->fsuid = saveuid;
  63. current->fsgid = savegid;
  64. }
  65. static void
  66. md5_to_hex(char *out, char *md5)
  67. {
  68. int i;
  69. for (i=0; i<16; i++) {
  70. unsigned char c = md5[i];
  71. *out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
  72. *out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
  73. }
  74. *out = '\0';
  75. }
  76. int
  77. nfs4_make_rec_clidname(char *dname, struct xdr_netobj *clname)
  78. {
  79. struct xdr_netobj cksum;
  80. struct crypto_tfm *tfm;
  81. struct scatterlist sg[1];
  82. int status = nfserr_resource;
  83. dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
  84. clname->len, clname->data);
  85. tfm = crypto_alloc_tfm("md5", 0);
  86. if (tfm == NULL)
  87. goto out;
  88. cksum.len = crypto_tfm_alg_digestsize(tfm);
  89. cksum.data = kmalloc(cksum.len, GFP_KERNEL);
  90. if (cksum.data == NULL)
  91. goto out;
  92. crypto_digest_init(tfm);
  93. sg[0].page = virt_to_page(clname->data);
  94. sg[0].offset = offset_in_page(clname->data);
  95. sg[0].length = clname->len;
  96. crypto_digest_update(tfm, sg, 1);
  97. crypto_digest_final(tfm, cksum.data);
  98. md5_to_hex(dname, cksum.data);
  99. kfree(cksum.data);
  100. status = nfs_ok;
  101. out:
  102. if (tfm)
  103. crypto_free_tfm(tfm);
  104. return status;
  105. }
  106. static int
  107. nfsd4_rec_fsync(struct dentry *dentry)
  108. {
  109. struct file *filp;
  110. int status = nfs_ok;
  111. dprintk("NFSD: nfs4_fsync_rec_dir\n");
  112. filp = dentry_open(dget(dentry), mntget(rec_dir.mnt), O_RDWR);
  113. if (IS_ERR(filp)) {
  114. status = PTR_ERR(filp);
  115. goto out;
  116. }
  117. if (filp->f_op && filp->f_op->fsync)
  118. status = filp->f_op->fsync(filp, filp->f_dentry, 0);
  119. fput(filp);
  120. out:
  121. if (status)
  122. printk("nfsd4: unable to sync recovery directory\n");
  123. return status;
  124. }
  125. int
  126. nfsd4_create_clid_dir(struct nfs4_client *clp)
  127. {
  128. char *dname = clp->cl_recdir;
  129. struct dentry *dentry;
  130. uid_t uid;
  131. gid_t gid;
  132. int status;
  133. dprintk("NFSD: nfsd4_create_clid_dir for \"%s\"\n", dname);
  134. if (!rec_dir_init || clp->cl_firststate)
  135. return 0;
  136. nfs4_save_user(&uid, &gid);
  137. /* lock the parent */
  138. down(&rec_dir.dentry->d_inode->i_sem);
  139. dentry = lookup_one_len(dname, rec_dir.dentry, HEXDIR_LEN-1);
  140. if (IS_ERR(dentry)) {
  141. status = PTR_ERR(dentry);
  142. goto out_unlock;
  143. }
  144. status = -EEXIST;
  145. if (dentry->d_inode) {
  146. dprintk("NFSD: nfsd4_create_clid_dir: DIRECTORY EXISTS\n");
  147. goto out_put;
  148. }
  149. status = vfs_mkdir(rec_dir.dentry->d_inode, dentry, S_IRWXU);
  150. out_put:
  151. dput(dentry);
  152. out_unlock:
  153. up(&rec_dir.dentry->d_inode->i_sem);
  154. if (status == 0) {
  155. clp->cl_firststate = 1;
  156. status = nfsd4_rec_fsync(rec_dir.dentry);
  157. }
  158. nfs4_reset_user(uid, gid);
  159. dprintk("NFSD: nfsd4_create_clid_dir returns %d\n", status);
  160. return status;
  161. }
  162. typedef int (recdir_func)(struct dentry *, struct dentry *);
  163. struct dentry_list {
  164. struct dentry *dentry;
  165. struct list_head list;
  166. };
  167. struct dentry_list_arg {
  168. struct list_head dentries;
  169. struct dentry *parent;
  170. };
  171. static int
  172. nfsd4_build_dentrylist(void *arg, const char *name, int namlen,
  173. loff_t offset, ino_t ino, unsigned int d_type)
  174. {
  175. struct dentry_list_arg *dla = arg;
  176. struct list_head *dentries = &dla->dentries;
  177. struct dentry *parent = dla->parent;
  178. struct dentry *dentry;
  179. struct dentry_list *child;
  180. if (name && isdotent(name, namlen))
  181. return nfs_ok;
  182. dentry = lookup_one_len(name, parent, namlen);
  183. if (IS_ERR(dentry))
  184. return PTR_ERR(dentry);
  185. child = kmalloc(sizeof(*child), GFP_KERNEL);
  186. if (child == NULL)
  187. return -ENOMEM;
  188. child->dentry = dentry;
  189. list_add(&child->list, dentries);
  190. return 0;
  191. }
  192. static int
  193. nfsd4_list_rec_dir(struct dentry *dir, recdir_func *f)
  194. {
  195. struct file *filp;
  196. struct dentry_list_arg dla = {
  197. .parent = dir,
  198. };
  199. struct list_head *dentries = &dla.dentries;
  200. struct dentry_list *child;
  201. uid_t uid;
  202. gid_t gid;
  203. int status;
  204. if (!rec_dir_init)
  205. return 0;
  206. nfs4_save_user(&uid, &gid);
  207. filp = dentry_open(dget(dir), mntget(rec_dir.mnt),
  208. O_RDWR);
  209. status = PTR_ERR(filp);
  210. if (IS_ERR(filp))
  211. goto out;
  212. INIT_LIST_HEAD(dentries);
  213. status = vfs_readdir(filp, nfsd4_build_dentrylist, &dla);
  214. fput(filp);
  215. while (!list_empty(dentries)) {
  216. child = list_entry(dentries->next, struct dentry_list, list);
  217. status = f(dir, child->dentry);
  218. if (status)
  219. goto out;
  220. list_del(&child->list);
  221. dput(child->dentry);
  222. kfree(child);
  223. }
  224. out:
  225. while (!list_empty(dentries)) {
  226. child = list_entry(dentries->next, struct dentry_list, list);
  227. list_del(&child->list);
  228. dput(child->dentry);
  229. kfree(child);
  230. }
  231. nfs4_reset_user(uid, gid);
  232. return status;
  233. }
  234. static int
  235. nfsd4_remove_clid_file(struct dentry *dir, struct dentry *dentry)
  236. {
  237. int status;
  238. if (!S_ISREG(dir->d_inode->i_mode)) {
  239. printk("nfsd4: non-file found in client recovery directory\n");
  240. return -EINVAL;
  241. }
  242. down(&dir->d_inode->i_sem);
  243. status = vfs_unlink(dir->d_inode, dentry);
  244. up(&dir->d_inode->i_sem);
  245. return status;
  246. }
  247. static int
  248. nfsd4_clear_clid_dir(struct dentry *dir, struct dentry *dentry)
  249. {
  250. int status;
  251. /* For now this directory should already be empty, but we empty it of
  252. * any regular files anyway, just in case the directory was created by
  253. * a kernel from the future.... */
  254. nfsd4_list_rec_dir(dentry, nfsd4_remove_clid_file);
  255. down(&dir->d_inode->i_sem);
  256. status = vfs_rmdir(dir->d_inode, dentry);
  257. up(&dir->d_inode->i_sem);
  258. return status;
  259. }
  260. static int
  261. nfsd4_unlink_clid_dir(char *name, int namlen)
  262. {
  263. struct dentry *dentry;
  264. int status;
  265. dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name);
  266. dentry = lookup_one_len(name, rec_dir.dentry, namlen);
  267. if (IS_ERR(dentry)) {
  268. status = PTR_ERR(dentry);
  269. return status;
  270. }
  271. status = -ENOENT;
  272. if (!dentry->d_inode)
  273. goto out;
  274. status = nfsd4_clear_clid_dir(rec_dir.dentry, dentry);
  275. out:
  276. dput(dentry);
  277. return status;
  278. }
  279. void
  280. nfsd4_remove_clid_dir(struct nfs4_client *clp)
  281. {
  282. uid_t uid;
  283. gid_t gid;
  284. int status;
  285. if (!rec_dir_init || !clp->cl_firststate)
  286. return;
  287. nfs4_save_user(&uid, &gid);
  288. status = nfsd4_unlink_clid_dir(clp->cl_recdir, HEXDIR_LEN-1);
  289. nfs4_reset_user(uid, gid);
  290. if (status == 0)
  291. status = nfsd4_rec_fsync(rec_dir.dentry);
  292. if (status)
  293. printk("NFSD: Failed to remove expired client state directory"
  294. " %.*s\n", HEXDIR_LEN, clp->cl_recdir);
  295. return;
  296. }
  297. static int
  298. purge_old(struct dentry *parent, struct dentry *child)
  299. {
  300. int status;
  301. if (nfs4_has_reclaimed_state(child->d_name.name))
  302. return nfs_ok;
  303. status = nfsd4_clear_clid_dir(parent, child);
  304. if (status)
  305. printk("failed to remove client recovery directory %s\n",
  306. child->d_name.name);
  307. /* Keep trying, success or failure: */
  308. return nfs_ok;
  309. }
  310. void
  311. nfsd4_recdir_purge_old(void) {
  312. int status;
  313. if (!rec_dir_init)
  314. return;
  315. status = nfsd4_list_rec_dir(rec_dir.dentry, purge_old);
  316. if (status == 0)
  317. status = nfsd4_rec_fsync(rec_dir.dentry);
  318. if (status)
  319. printk("nfsd4: failed to purge old clients from recovery"
  320. " directory %s\n", rec_dir.dentry->d_name.name);
  321. return;
  322. }
  323. static int
  324. load_recdir(struct dentry *parent, struct dentry *child)
  325. {
  326. if (child->d_name.len != HEXDIR_LEN - 1) {
  327. printk("nfsd4: illegal name %s in recovery directory\n",
  328. child->d_name.name);
  329. /* Keep trying; maybe the others are OK: */
  330. return nfs_ok;
  331. }
  332. nfs4_client_to_reclaim(child->d_name.name);
  333. return nfs_ok;
  334. }
  335. int
  336. nfsd4_recdir_load(void) {
  337. int status;
  338. status = nfsd4_list_rec_dir(rec_dir.dentry, load_recdir);
  339. if (status)
  340. printk("nfsd4: failed loading clients from recovery"
  341. " directory %s\n", rec_dir.dentry->d_name.name);
  342. return status;
  343. }
  344. /*
  345. * Hold reference to the recovery directory.
  346. */
  347. void
  348. nfsd4_init_recdir(char *rec_dirname)
  349. {
  350. uid_t uid = 0;
  351. gid_t gid = 0;
  352. int status;
  353. printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
  354. rec_dirname);
  355. BUG_ON(rec_dir_init);
  356. nfs4_save_user(&uid, &gid);
  357. status = path_lookup(rec_dirname, LOOKUP_FOLLOW, &rec_dir);
  358. if (status == -ENOENT)
  359. printk("NFSD: recovery directory %s doesn't exist\n",
  360. rec_dirname);
  361. if (!status)
  362. rec_dir_init = 1;
  363. nfs4_reset_user(uid, gid);
  364. }
  365. void
  366. nfsd4_shutdown_recdir(void)
  367. {
  368. if (!rec_dir_init)
  369. return;
  370. rec_dir_init = 0;
  371. path_release(&rec_dir);
  372. }