nfs4recover.c 9.9 KB

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