nfs4recover.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 <linux/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;
  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_init_one(&sg, clname->data, clname->len);
  95. if (crypto_hash_digest(&desc, &sg, sg.length, cksum.data))
  96. goto out;
  97. md5_to_hex(dname, cksum.data);
  98. kfree(cksum.data);
  99. status = nfs_ok;
  100. out:
  101. crypto_free_hash(desc.tfm);
  102. out_no_tfm:
  103. return status;
  104. }
  105. static void
  106. nfsd4_sync_rec_dir(void)
  107. {
  108. mutex_lock(&rec_dir.dentry->d_inode->i_mutex);
  109. nfsd_sync_dir(rec_dir.dentry);
  110. mutex_unlock(&rec_dir.dentry->d_inode->i_mutex);
  111. }
  112. int
  113. nfsd4_create_clid_dir(struct nfs4_client *clp)
  114. {
  115. char *dname = clp->cl_recdir;
  116. struct dentry *dentry;
  117. uid_t uid;
  118. gid_t gid;
  119. int status;
  120. dprintk("NFSD: nfsd4_create_clid_dir for \"%s\"\n", dname);
  121. if (!rec_dir_init || clp->cl_firststate)
  122. return 0;
  123. nfs4_save_user(&uid, &gid);
  124. /* lock the parent */
  125. mutex_lock(&rec_dir.dentry->d_inode->i_mutex);
  126. dentry = lookup_one_len(dname, rec_dir.dentry, HEXDIR_LEN-1);
  127. if (IS_ERR(dentry)) {
  128. status = PTR_ERR(dentry);
  129. goto out_unlock;
  130. }
  131. status = -EEXIST;
  132. if (dentry->d_inode) {
  133. dprintk("NFSD: nfsd4_create_clid_dir: DIRECTORY EXISTS\n");
  134. goto out_put;
  135. }
  136. status = vfs_mkdir(rec_dir.dentry->d_inode, dentry, S_IRWXU);
  137. out_put:
  138. dput(dentry);
  139. out_unlock:
  140. mutex_unlock(&rec_dir.dentry->d_inode->i_mutex);
  141. if (status == 0) {
  142. clp->cl_firststate = 1;
  143. nfsd4_sync_rec_dir();
  144. }
  145. nfs4_reset_user(uid, gid);
  146. dprintk("NFSD: nfsd4_create_clid_dir returns %d\n", status);
  147. return status;
  148. }
  149. typedef int (recdir_func)(struct dentry *, struct dentry *);
  150. struct dentry_list {
  151. struct dentry *dentry;
  152. struct list_head list;
  153. };
  154. struct dentry_list_arg {
  155. struct list_head dentries;
  156. struct dentry *parent;
  157. };
  158. static int
  159. nfsd4_build_dentrylist(void *arg, const char *name, int namlen,
  160. loff_t offset, u64 ino, unsigned int d_type)
  161. {
  162. struct dentry_list_arg *dla = arg;
  163. struct list_head *dentries = &dla->dentries;
  164. struct dentry *parent = dla->parent;
  165. struct dentry *dentry;
  166. struct dentry_list *child;
  167. if (name && isdotent(name, namlen))
  168. return 0;
  169. dentry = lookup_one_len(name, parent, namlen);
  170. if (IS_ERR(dentry))
  171. return PTR_ERR(dentry);
  172. child = kmalloc(sizeof(*child), GFP_KERNEL);
  173. if (child == NULL)
  174. return -ENOMEM;
  175. child->dentry = dentry;
  176. list_add(&child->list, dentries);
  177. return 0;
  178. }
  179. static int
  180. nfsd4_list_rec_dir(struct dentry *dir, recdir_func *f)
  181. {
  182. struct file *filp;
  183. struct dentry_list_arg dla = {
  184. .parent = dir,
  185. };
  186. struct list_head *dentries = &dla.dentries;
  187. struct dentry_list *child;
  188. uid_t uid;
  189. gid_t gid;
  190. int status;
  191. if (!rec_dir_init)
  192. return 0;
  193. nfs4_save_user(&uid, &gid);
  194. filp = dentry_open(dget(dir), mntget(rec_dir.mnt), O_RDONLY);
  195. status = PTR_ERR(filp);
  196. if (IS_ERR(filp))
  197. goto out;
  198. INIT_LIST_HEAD(dentries);
  199. status = vfs_readdir(filp, nfsd4_build_dentrylist, &dla);
  200. fput(filp);
  201. while (!list_empty(dentries)) {
  202. child = list_entry(dentries->next, struct dentry_list, list);
  203. status = f(dir, child->dentry);
  204. if (status)
  205. goto out;
  206. list_del(&child->list);
  207. dput(child->dentry);
  208. kfree(child);
  209. }
  210. out:
  211. while (!list_empty(dentries)) {
  212. child = list_entry(dentries->next, struct dentry_list, list);
  213. list_del(&child->list);
  214. dput(child->dentry);
  215. kfree(child);
  216. }
  217. nfs4_reset_user(uid, gid);
  218. return status;
  219. }
  220. static int
  221. nfsd4_remove_clid_file(struct dentry *dir, struct dentry *dentry)
  222. {
  223. int status;
  224. if (!S_ISREG(dir->d_inode->i_mode)) {
  225. printk("nfsd4: non-file found in client recovery directory\n");
  226. return -EINVAL;
  227. }
  228. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
  229. status = vfs_unlink(dir->d_inode, dentry);
  230. mutex_unlock(&dir->d_inode->i_mutex);
  231. return status;
  232. }
  233. static int
  234. nfsd4_clear_clid_dir(struct dentry *dir, struct dentry *dentry)
  235. {
  236. int status;
  237. /* For now this directory should already be empty, but we empty it of
  238. * any regular files anyway, just in case the directory was created by
  239. * a kernel from the future.... */
  240. nfsd4_list_rec_dir(dentry, nfsd4_remove_clid_file);
  241. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
  242. status = vfs_rmdir(dir->d_inode, dentry);
  243. mutex_unlock(&dir->d_inode->i_mutex);
  244. return status;
  245. }
  246. static int
  247. nfsd4_unlink_clid_dir(char *name, int namlen)
  248. {
  249. struct dentry *dentry;
  250. int status;
  251. dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name);
  252. mutex_lock(&rec_dir.dentry->d_inode->i_mutex);
  253. dentry = lookup_one_len(name, rec_dir.dentry, namlen);
  254. mutex_unlock(&rec_dir.dentry->d_inode->i_mutex);
  255. if (IS_ERR(dentry)) {
  256. status = PTR_ERR(dentry);
  257. return status;
  258. }
  259. status = -ENOENT;
  260. if (!dentry->d_inode)
  261. goto out;
  262. status = nfsd4_clear_clid_dir(rec_dir.dentry, dentry);
  263. out:
  264. dput(dentry);
  265. return status;
  266. }
  267. void
  268. nfsd4_remove_clid_dir(struct nfs4_client *clp)
  269. {
  270. uid_t uid;
  271. gid_t gid;
  272. int status;
  273. if (!rec_dir_init || !clp->cl_firststate)
  274. return;
  275. clp->cl_firststate = 0;
  276. nfs4_save_user(&uid, &gid);
  277. status = nfsd4_unlink_clid_dir(clp->cl_recdir, HEXDIR_LEN-1);
  278. nfs4_reset_user(uid, gid);
  279. if (status == 0)
  280. nfsd4_sync_rec_dir();
  281. if (status)
  282. printk("NFSD: Failed to remove expired client state directory"
  283. " %.*s\n", HEXDIR_LEN, clp->cl_recdir);
  284. return;
  285. }
  286. static int
  287. purge_old(struct dentry *parent, struct dentry *child)
  288. {
  289. int status;
  290. if (nfs4_has_reclaimed_state(child->d_name.name))
  291. return 0;
  292. status = nfsd4_clear_clid_dir(parent, child);
  293. if (status)
  294. printk("failed to remove client recovery directory %s\n",
  295. child->d_name.name);
  296. /* Keep trying, success or failure: */
  297. return 0;
  298. }
  299. void
  300. nfsd4_recdir_purge_old(void) {
  301. int status;
  302. if (!rec_dir_init)
  303. return;
  304. status = nfsd4_list_rec_dir(rec_dir.dentry, purge_old);
  305. if (status == 0)
  306. nfsd4_sync_rec_dir();
  307. if (status)
  308. printk("nfsd4: failed to purge old clients from recovery"
  309. " directory %s\n", rec_dir.dentry->d_name.name);
  310. return;
  311. }
  312. static int
  313. load_recdir(struct dentry *parent, struct dentry *child)
  314. {
  315. if (child->d_name.len != HEXDIR_LEN - 1) {
  316. printk("nfsd4: illegal name %s in recovery directory\n",
  317. child->d_name.name);
  318. /* Keep trying; maybe the others are OK: */
  319. return 0;
  320. }
  321. nfs4_client_to_reclaim(child->d_name.name);
  322. return 0;
  323. }
  324. int
  325. nfsd4_recdir_load(void) {
  326. int status;
  327. status = nfsd4_list_rec_dir(rec_dir.dentry, load_recdir);
  328. if (status)
  329. printk("nfsd4: failed loading clients from recovery"
  330. " directory %s\n", rec_dir.dentry->d_name.name);
  331. return status;
  332. }
  333. /*
  334. * Hold reference to the recovery directory.
  335. */
  336. void
  337. nfsd4_init_recdir(char *rec_dirname)
  338. {
  339. uid_t uid = 0;
  340. gid_t gid = 0;
  341. int status;
  342. printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
  343. rec_dirname);
  344. BUG_ON(rec_dir_init);
  345. nfs4_save_user(&uid, &gid);
  346. status = path_lookup(rec_dirname, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
  347. &rec_dir);
  348. if (status)
  349. printk("NFSD: unable to find recovery directory %s\n",
  350. rec_dirname);
  351. if (!status)
  352. rec_dir_init = 1;
  353. nfs4_reset_user(uid, gid);
  354. }
  355. void
  356. nfsd4_shutdown_recdir(void)
  357. {
  358. if (!rec_dir_init)
  359. return;
  360. rec_dir_init = 0;
  361. path_release(&rec_dir);
  362. }