nfs4recover.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. static struct nameidata rec_dir;
  49. static int rec_dir_init = 0;
  50. static void
  51. nfs4_save_user(uid_t *saveuid, gid_t *savegid)
  52. {
  53. *saveuid = current->fsuid;
  54. *savegid = current->fsgid;
  55. current->fsuid = 0;
  56. current->fsgid = 0;
  57. }
  58. static void
  59. nfs4_reset_user(uid_t saveuid, gid_t savegid)
  60. {
  61. current->fsuid = saveuid;
  62. current->fsgid = savegid;
  63. }
  64. static void
  65. md5_to_hex(char *out, char *md5)
  66. {
  67. int i;
  68. for (i=0; i<16; i++) {
  69. unsigned char c = md5[i];
  70. *out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
  71. *out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
  72. }
  73. *out = '\0';
  74. }
  75. int
  76. nfs4_make_rec_clidname(char *dname, struct xdr_netobj *clname)
  77. {
  78. struct xdr_netobj cksum;
  79. struct crypto_tfm *tfm;
  80. struct scatterlist sg[1];
  81. int status = nfserr_resource;
  82. dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
  83. clname->len, clname->data);
  84. tfm = crypto_alloc_tfm("md5", 0);
  85. if (tfm == NULL)
  86. goto out;
  87. cksum.len = crypto_tfm_alg_digestsize(tfm);
  88. cksum.data = kmalloc(cksum.len, GFP_KERNEL);
  89. if (cksum.data == NULL)
  90. goto out;
  91. crypto_digest_init(tfm);
  92. sg[0].page = virt_to_page(clname->data);
  93. sg[0].offset = offset_in_page(clname->data);
  94. sg[0].length = clname->len;
  95. crypto_digest_update(tfm, sg, 1);
  96. crypto_digest_final(tfm, cksum.data);
  97. md5_to_hex(dname, cksum.data);
  98. kfree(cksum.data);
  99. status = nfs_ok;
  100. out:
  101. if (tfm)
  102. crypto_free_tfm(tfm);
  103. return status;
  104. }
  105. static int
  106. nfsd4_rec_fsync(struct dentry *dentry)
  107. {
  108. struct file *filp;
  109. int status = nfs_ok;
  110. dprintk("NFSD: nfs4_fsync_rec_dir\n");
  111. filp = dentry_open(dget(dentry), mntget(rec_dir.mnt), O_RDWR);
  112. if (IS_ERR(filp)) {
  113. status = PTR_ERR(filp);
  114. goto out;
  115. }
  116. if (filp->f_op && filp->f_op->fsync)
  117. status = filp->f_op->fsync(filp, filp->f_dentry, 0);
  118. fput(filp);
  119. out:
  120. if (status)
  121. printk("nfsd4: unable to sync recovery directory\n");
  122. return status;
  123. }
  124. int
  125. nfsd4_create_clid_dir(struct nfs4_client *clp)
  126. {
  127. char *dname = clp->cl_recdir;
  128. struct dentry *dentry;
  129. uid_t uid;
  130. gid_t gid;
  131. int status;
  132. dprintk("NFSD: nfsd4_create_clid_dir for \"%s\"\n", dname);
  133. if (!rec_dir_init || clp->cl_firststate)
  134. return 0;
  135. nfs4_save_user(&uid, &gid);
  136. /* lock the parent */
  137. down(&rec_dir.dentry->d_inode->i_sem);
  138. dentry = lookup_one_len(dname, rec_dir.dentry, HEXDIR_LEN-1);
  139. if (IS_ERR(dentry)) {
  140. status = PTR_ERR(dentry);
  141. goto out_unlock;
  142. }
  143. status = -EEXIST;
  144. if (dentry->d_inode) {
  145. dprintk("NFSD: nfsd4_create_clid_dir: DIRECTORY EXISTS\n");
  146. goto out_put;
  147. }
  148. status = vfs_mkdir(rec_dir.dentry->d_inode, dentry, S_IRWXU);
  149. out_put:
  150. dput(dentry);
  151. out_unlock:
  152. up(&rec_dir.dentry->d_inode->i_sem);
  153. if (status == 0) {
  154. clp->cl_firststate = 1;
  155. status = nfsd4_rec_fsync(rec_dir.dentry);
  156. }
  157. nfs4_reset_user(uid, gid);
  158. dprintk("NFSD: nfsd4_create_clid_dir returns %d\n", status);
  159. return status;
  160. }
  161. typedef int (recdir_func)(struct dentry *, struct dentry *);
  162. struct dentry_list {
  163. struct dentry *dentry;
  164. struct list_head list;
  165. };
  166. struct dentry_list_arg {
  167. struct list_head dentries;
  168. struct dentry *parent;
  169. };
  170. static int
  171. nfsd4_build_dentrylist(void *arg, const char *name, int namlen,
  172. loff_t offset, ino_t ino, unsigned int d_type)
  173. {
  174. struct dentry_list_arg *dla = arg;
  175. struct list_head *dentries = &dla->dentries;
  176. struct dentry *parent = dla->parent;
  177. struct dentry *dentry;
  178. struct dentry_list *child;
  179. if (name && isdotent(name, namlen))
  180. return nfs_ok;
  181. dentry = lookup_one_len(name, parent, namlen);
  182. if (IS_ERR(dentry))
  183. return PTR_ERR(dentry);
  184. child = kmalloc(sizeof(*child), GFP_KERNEL);
  185. if (child == NULL)
  186. return -ENOMEM;
  187. child->dentry = dentry;
  188. list_add(&child->list, dentries);
  189. return 0;
  190. }
  191. static int
  192. nfsd4_list_rec_dir(struct dentry *dir, recdir_func *f)
  193. {
  194. struct file *filp;
  195. struct dentry_list_arg dla = {
  196. .parent = dir,
  197. };
  198. struct list_head *dentries = &dla.dentries;
  199. struct dentry_list *child;
  200. uid_t uid;
  201. gid_t gid;
  202. int status;
  203. if (!rec_dir_init)
  204. return 0;
  205. nfs4_save_user(&uid, &gid);
  206. filp = dentry_open(dget(dir), mntget(rec_dir.mnt),
  207. O_RDWR);
  208. status = PTR_ERR(filp);
  209. if (IS_ERR(filp))
  210. goto out;
  211. INIT_LIST_HEAD(dentries);
  212. status = vfs_readdir(filp, nfsd4_build_dentrylist, &dla);
  213. fput(filp);
  214. while (!list_empty(dentries)) {
  215. child = list_entry(dentries->next, struct dentry_list, list);
  216. status = f(dir, child->dentry);
  217. if (status)
  218. goto out;
  219. list_del(&child->list);
  220. dput(child->dentry);
  221. kfree(child);
  222. }
  223. out:
  224. while (!list_empty(dentries)) {
  225. child = list_entry(dentries->next, struct dentry_list, list);
  226. list_del(&child->list);
  227. dput(child->dentry);
  228. kfree(child);
  229. }
  230. nfs4_reset_user(uid, gid);
  231. return status;
  232. }
  233. static int
  234. nfsd4_remove_clid_file(struct dentry *dir, struct dentry *dentry)
  235. {
  236. int status;
  237. if (!S_ISREG(dir->d_inode->i_mode)) {
  238. printk("nfsd4: non-file found in client recovery directory\n");
  239. return -EINVAL;
  240. }
  241. down(&dir->d_inode->i_sem);
  242. status = vfs_unlink(dir->d_inode, dentry);
  243. up(&dir->d_inode->i_sem);
  244. return status;
  245. }
  246. static int
  247. nfsd4_clear_clid_dir(struct dentry *dir, struct dentry *dentry)
  248. {
  249. int status;
  250. /* For now this directory should already be empty, but we empty it of
  251. * any regular files anyway, just in case the directory was created by
  252. * a kernel from the future.... */
  253. nfsd4_list_rec_dir(dentry, nfsd4_remove_clid_file);
  254. down(&dir->d_inode->i_sem);
  255. status = vfs_rmdir(dir->d_inode, dentry);
  256. up(&dir->d_inode->i_sem);
  257. return status;
  258. }
  259. static int
  260. nfsd4_unlink_clid_dir(char *name, int namlen)
  261. {
  262. struct dentry *dentry;
  263. int status;
  264. dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name);
  265. dentry = lookup_one_len(name, rec_dir.dentry, namlen);
  266. if (IS_ERR(dentry)) {
  267. status = PTR_ERR(dentry);
  268. return status;
  269. }
  270. status = -ENOENT;
  271. if (!dentry->d_inode)
  272. goto out;
  273. status = nfsd4_clear_clid_dir(rec_dir.dentry, dentry);
  274. out:
  275. dput(dentry);
  276. return status;
  277. }
  278. void
  279. nfsd4_remove_clid_dir(struct nfs4_client *clp)
  280. {
  281. uid_t uid;
  282. gid_t gid;
  283. int status;
  284. if (!rec_dir_init || !clp->cl_firststate)
  285. return;
  286. nfs4_save_user(&uid, &gid);
  287. status = nfsd4_unlink_clid_dir(clp->cl_recdir, HEXDIR_LEN-1);
  288. nfs4_reset_user(uid, gid);
  289. if (status == 0)
  290. status = nfsd4_rec_fsync(rec_dir.dentry);
  291. if (status)
  292. printk("NFSD: Failed to remove expired client state directory"
  293. " %.*s\n", HEXDIR_LEN, clp->cl_recdir);
  294. return;
  295. }
  296. static int
  297. purge_old(struct dentry *parent, struct dentry *child)
  298. {
  299. int status;
  300. if (nfs4_has_reclaimed_state(child->d_name.name))
  301. return nfs_ok;
  302. status = nfsd4_clear_clid_dir(parent, child);
  303. if (status)
  304. printk("failed to remove client recovery directory %s\n",
  305. child->d_name.name);
  306. /* Keep trying, success or failure: */
  307. return nfs_ok;
  308. }
  309. void
  310. nfsd4_recdir_purge_old(void) {
  311. int status;
  312. if (!rec_dir_init)
  313. return;
  314. status = nfsd4_list_rec_dir(rec_dir.dentry, purge_old);
  315. if (status == 0)
  316. status = nfsd4_rec_fsync(rec_dir.dentry);
  317. if (status)
  318. printk("nfsd4: failed to purge old clients from recovery"
  319. " directory %s\n", rec_dir.dentry->d_name.name);
  320. return;
  321. }
  322. static int
  323. load_recdir(struct dentry *parent, struct dentry *child)
  324. {
  325. if (child->d_name.len != HEXDIR_LEN - 1) {
  326. printk("nfsd4: illegal name %s in recovery directory\n",
  327. child->d_name.name);
  328. /* Keep trying; maybe the others are OK: */
  329. return nfs_ok;
  330. }
  331. nfs4_client_to_reclaim(child->d_name.name);
  332. return nfs_ok;
  333. }
  334. int
  335. nfsd4_recdir_load(void) {
  336. int status;
  337. status = nfsd4_list_rec_dir(rec_dir.dentry, load_recdir);
  338. if (status)
  339. printk("nfsd4: failed loading clients from recovery"
  340. " directory %s\n", rec_dir.dentry->d_name.name);
  341. return status;
  342. }
  343. /*
  344. * Hold reference to the recovery directory.
  345. */
  346. void
  347. nfsd4_init_recdir(char *rec_dirname)
  348. {
  349. uid_t uid = 0;
  350. gid_t gid = 0;
  351. int status;
  352. printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
  353. rec_dirname);
  354. BUG_ON(rec_dir_init);
  355. nfs4_save_user(&uid, &gid);
  356. status = path_lookup(rec_dirname, LOOKUP_FOLLOW, &rec_dir);
  357. if (status == -ENOENT)
  358. printk("NFSD: recovery directory %s doesn't exist\n",
  359. rec_dirname);
  360. if (!status)
  361. rec_dir_init = 1;
  362. nfs4_reset_user(uid, gid);
  363. }
  364. void
  365. nfsd4_shutdown_recdir(void)
  366. {
  367. if (!rec_dir_init)
  368. return;
  369. rec_dir_init = 0;
  370. path_release(&rec_dir);
  371. }