nfs4recover.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Copyright (c) 2004 The Regents of the University of Michigan.
  3. * All rights reserved.
  4. *
  5. * Andy Adamson <andros@citi.umich.edu>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the University nor the names of its
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  21. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  27. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include <linux/file.h>
  34. #include <linux/slab.h>
  35. #include <linux/namei.h>
  36. #include <linux/crypto.h>
  37. #include <linux/sched.h>
  38. #include "nfsd.h"
  39. #include "state.h"
  40. #include "vfs.h"
  41. #define NFSDDBG_FACILITY NFSDDBG_PROC
  42. /* Globals */
  43. static struct file *rec_file;
  44. static int
  45. nfs4_save_creds(const struct cred **original_creds)
  46. {
  47. struct cred *new;
  48. new = prepare_creds();
  49. if (!new)
  50. return -ENOMEM;
  51. new->fsuid = 0;
  52. new->fsgid = 0;
  53. *original_creds = override_creds(new);
  54. put_cred(new);
  55. return 0;
  56. }
  57. static void
  58. nfs4_reset_creds(const struct cred *original)
  59. {
  60. revert_creds(original);
  61. }
  62. static void
  63. md5_to_hex(char *out, char *md5)
  64. {
  65. int i;
  66. for (i=0; i<16; i++) {
  67. unsigned char c = md5[i];
  68. *out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
  69. *out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
  70. }
  71. *out = '\0';
  72. }
  73. __be32
  74. nfs4_make_rec_clidname(char *dname, struct xdr_netobj *clname)
  75. {
  76. struct xdr_netobj cksum;
  77. struct hash_desc desc;
  78. struct scatterlist sg;
  79. __be32 status = nfserr_resource;
  80. dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
  81. clname->len, clname->data);
  82. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  83. desc.tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
  84. if (IS_ERR(desc.tfm))
  85. goto out_no_tfm;
  86. cksum.len = crypto_hash_digestsize(desc.tfm);
  87. cksum.data = kmalloc(cksum.len, GFP_KERNEL);
  88. if (cksum.data == NULL)
  89. goto out;
  90. sg_init_one(&sg, clname->data, clname->len);
  91. if (crypto_hash_digest(&desc, &sg, sg.length, cksum.data))
  92. goto out;
  93. md5_to_hex(dname, cksum.data);
  94. status = nfs_ok;
  95. out:
  96. kfree(cksum.data);
  97. crypto_free_hash(desc.tfm);
  98. out_no_tfm:
  99. return status;
  100. }
  101. int
  102. nfsd4_create_clid_dir(struct nfs4_client *clp)
  103. {
  104. const struct cred *original_cred;
  105. char *dname = clp->cl_recdir;
  106. struct dentry *dir, *dentry;
  107. int status;
  108. dprintk("NFSD: nfsd4_create_clid_dir for \"%s\"\n", dname);
  109. if (!rec_file || clp->cl_firststate)
  110. return 0;
  111. status = nfs4_save_creds(&original_cred);
  112. if (status < 0)
  113. return status;
  114. dir = rec_file->f_path.dentry;
  115. /* lock the parent */
  116. mutex_lock(&dir->d_inode->i_mutex);
  117. dentry = lookup_one_len(dname, dir, HEXDIR_LEN-1);
  118. if (IS_ERR(dentry)) {
  119. status = PTR_ERR(dentry);
  120. goto out_unlock;
  121. }
  122. status = -EEXIST;
  123. if (dentry->d_inode) {
  124. dprintk("NFSD: nfsd4_create_clid_dir: DIRECTORY EXISTS\n");
  125. goto out_put;
  126. }
  127. status = mnt_want_write(rec_file->f_path.mnt);
  128. if (status)
  129. goto out_put;
  130. status = vfs_mkdir(dir->d_inode, dentry, S_IRWXU);
  131. mnt_drop_write(rec_file->f_path.mnt);
  132. out_put:
  133. dput(dentry);
  134. out_unlock:
  135. mutex_unlock(&dir->d_inode->i_mutex);
  136. if (status == 0) {
  137. clp->cl_firststate = 1;
  138. vfs_fsync(rec_file, 0);
  139. }
  140. nfs4_reset_creds(original_cred);
  141. dprintk("NFSD: nfsd4_create_clid_dir returns %d\n", status);
  142. return status;
  143. }
  144. typedef int (recdir_func)(struct dentry *, struct dentry *);
  145. struct name_list {
  146. char name[HEXDIR_LEN];
  147. struct list_head list;
  148. };
  149. static int
  150. nfsd4_build_namelist(void *arg, const char *name, int namlen,
  151. loff_t offset, u64 ino, unsigned int d_type)
  152. {
  153. struct list_head *names = arg;
  154. struct name_list *entry;
  155. if (namlen != HEXDIR_LEN - 1)
  156. return 0;
  157. entry = kmalloc(sizeof(struct name_list), GFP_KERNEL);
  158. if (entry == NULL)
  159. return -ENOMEM;
  160. memcpy(entry->name, name, HEXDIR_LEN - 1);
  161. entry->name[HEXDIR_LEN - 1] = '\0';
  162. list_add(&entry->list, names);
  163. return 0;
  164. }
  165. static int
  166. nfsd4_list_rec_dir(recdir_func *f)
  167. {
  168. const struct cred *original_cred;
  169. struct dentry *dir = rec_file->f_path.dentry;
  170. LIST_HEAD(names);
  171. int status;
  172. status = nfs4_save_creds(&original_cred);
  173. if (status < 0)
  174. return status;
  175. status = vfs_llseek(rec_file, 0, SEEK_SET);
  176. if (status < 0) {
  177. nfs4_reset_creds(original_cred);
  178. return status;
  179. }
  180. status = vfs_readdir(rec_file, nfsd4_build_namelist, &names);
  181. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
  182. while (!list_empty(&names)) {
  183. struct name_list *entry;
  184. entry = list_entry(names.next, struct name_list, list);
  185. if (!status) {
  186. struct dentry *dentry;
  187. dentry = lookup_one_len(entry->name, dir, HEXDIR_LEN-1);
  188. if (IS_ERR(dentry)) {
  189. status = PTR_ERR(dentry);
  190. break;
  191. }
  192. status = f(dir, dentry);
  193. dput(dentry);
  194. }
  195. list_del(&entry->list);
  196. kfree(entry);
  197. }
  198. mutex_unlock(&dir->d_inode->i_mutex);
  199. nfs4_reset_creds(original_cred);
  200. return status;
  201. }
  202. static int
  203. nfsd4_unlink_clid_dir(char *name, int namlen)
  204. {
  205. struct dentry *dir, *dentry;
  206. int status;
  207. dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name);
  208. dir = rec_file->f_path.dentry;
  209. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
  210. dentry = lookup_one_len(name, dir, namlen);
  211. if (IS_ERR(dentry)) {
  212. status = PTR_ERR(dentry);
  213. goto out_unlock;
  214. }
  215. status = -ENOENT;
  216. if (!dentry->d_inode)
  217. goto out;
  218. status = vfs_rmdir(dir->d_inode, dentry);
  219. out:
  220. dput(dentry);
  221. out_unlock:
  222. mutex_unlock(&dir->d_inode->i_mutex);
  223. return status;
  224. }
  225. void
  226. nfsd4_remove_clid_dir(struct nfs4_client *clp)
  227. {
  228. const struct cred *original_cred;
  229. int status;
  230. if (!rec_file || !clp->cl_firststate)
  231. return;
  232. status = mnt_want_write(rec_file->f_path.mnt);
  233. if (status)
  234. goto out;
  235. clp->cl_firststate = 0;
  236. status = nfs4_save_creds(&original_cred);
  237. if (status < 0)
  238. goto out;
  239. status = nfsd4_unlink_clid_dir(clp->cl_recdir, HEXDIR_LEN-1);
  240. nfs4_reset_creds(original_cred);
  241. if (status == 0)
  242. vfs_fsync(rec_file, 0);
  243. mnt_drop_write(rec_file->f_path.mnt);
  244. out:
  245. if (status)
  246. printk("NFSD: Failed to remove expired client state directory"
  247. " %.*s\n", HEXDIR_LEN, clp->cl_recdir);
  248. return;
  249. }
  250. static int
  251. purge_old(struct dentry *parent, struct dentry *child)
  252. {
  253. int status;
  254. if (nfs4_has_reclaimed_state(child->d_name.name, false))
  255. return 0;
  256. status = vfs_rmdir(parent->d_inode, child);
  257. if (status)
  258. printk("failed to remove client recovery directory %s\n",
  259. child->d_name.name);
  260. /* Keep trying, success or failure: */
  261. return 0;
  262. }
  263. void
  264. nfsd4_recdir_purge_old(void) {
  265. int status;
  266. if (!rec_file)
  267. return;
  268. status = mnt_want_write(rec_file->f_path.mnt);
  269. if (status)
  270. goto out;
  271. status = nfsd4_list_rec_dir(purge_old);
  272. if (status == 0)
  273. vfs_fsync(rec_file, 0);
  274. mnt_drop_write(rec_file->f_path.mnt);
  275. out:
  276. if (status)
  277. printk("nfsd4: failed to purge old clients from recovery"
  278. " directory %s\n", rec_file->f_path.dentry->d_name.name);
  279. }
  280. static int
  281. load_recdir(struct dentry *parent, struct dentry *child)
  282. {
  283. if (child->d_name.len != HEXDIR_LEN - 1) {
  284. printk("nfsd4: illegal name %s in recovery directory\n",
  285. child->d_name.name);
  286. /* Keep trying; maybe the others are OK: */
  287. return 0;
  288. }
  289. nfs4_client_to_reclaim(child->d_name.name);
  290. return 0;
  291. }
  292. int
  293. nfsd4_recdir_load(void) {
  294. int status;
  295. if (!rec_file)
  296. return 0;
  297. status = nfsd4_list_rec_dir(load_recdir);
  298. if (status)
  299. printk("nfsd4: failed loading clients from recovery"
  300. " directory %s\n", rec_file->f_path.dentry->d_name.name);
  301. return status;
  302. }
  303. /*
  304. * Hold reference to the recovery directory.
  305. */
  306. void
  307. nfsd4_init_recdir(char *rec_dirname)
  308. {
  309. const struct cred *original_cred;
  310. int status;
  311. printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
  312. rec_dirname);
  313. BUG_ON(rec_file);
  314. status = nfs4_save_creds(&original_cred);
  315. if (status < 0) {
  316. printk("NFSD: Unable to change credentials to find recovery"
  317. " directory: error %d\n",
  318. status);
  319. return;
  320. }
  321. rec_file = filp_open(rec_dirname, O_RDONLY | O_DIRECTORY, 0);
  322. if (IS_ERR(rec_file)) {
  323. printk("NFSD: unable to find recovery directory %s\n",
  324. rec_dirname);
  325. rec_file = NULL;
  326. }
  327. nfs4_reset_creds(original_cred);
  328. }
  329. void
  330. nfsd4_shutdown_recdir(void)
  331. {
  332. if (!rec_file)
  333. return;
  334. fput(rec_file);
  335. rec_file = NULL;
  336. }