nfs4recover.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 path rec_dir;
  44. static int rec_dir_init = 0;
  45. static int
  46. nfs4_save_creds(const struct cred **original_creds)
  47. {
  48. struct cred *new;
  49. new = prepare_creds();
  50. if (!new)
  51. return -ENOMEM;
  52. new->fsuid = 0;
  53. new->fsgid = 0;
  54. *original_creds = override_creds(new);
  55. put_cred(new);
  56. return 0;
  57. }
  58. static void
  59. nfs4_reset_creds(const struct cred *original)
  60. {
  61. revert_creds(original);
  62. }
  63. static void
  64. md5_to_hex(char *out, char *md5)
  65. {
  66. int i;
  67. for (i=0; i<16; i++) {
  68. unsigned char c = md5[i];
  69. *out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
  70. *out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
  71. }
  72. *out = '\0';
  73. }
  74. __be32
  75. nfs4_make_rec_clidname(char *dname, struct xdr_netobj *clname)
  76. {
  77. struct xdr_netobj cksum;
  78. struct hash_desc desc;
  79. struct scatterlist sg;
  80. __be32 status = nfserr_resource;
  81. dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
  82. clname->len, clname->data);
  83. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  84. desc.tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
  85. if (IS_ERR(desc.tfm))
  86. goto out_no_tfm;
  87. cksum.len = crypto_hash_digestsize(desc.tfm);
  88. cksum.data = kmalloc(cksum.len, GFP_KERNEL);
  89. if (cksum.data == NULL)
  90. goto out;
  91. sg_init_one(&sg, clname->data, clname->len);
  92. if (crypto_hash_digest(&desc, &sg, sg.length, cksum.data))
  93. goto out;
  94. md5_to_hex(dname, cksum.data);
  95. status = nfs_ok;
  96. out:
  97. kfree(cksum.data);
  98. crypto_free_hash(desc.tfm);
  99. out_no_tfm:
  100. return status;
  101. }
  102. static void
  103. nfsd4_sync_rec_dir(void)
  104. {
  105. vfs_fsync(NULL, rec_dir.dentry, 0);
  106. }
  107. int
  108. nfsd4_create_clid_dir(struct nfs4_client *clp)
  109. {
  110. const struct cred *original_cred;
  111. char *dname = clp->cl_recdir;
  112. struct dentry *dentry;
  113. int status;
  114. dprintk("NFSD: nfsd4_create_clid_dir for \"%s\"\n", dname);
  115. if (!rec_dir_init || clp->cl_firststate)
  116. return 0;
  117. status = nfs4_save_creds(&original_cred);
  118. if (status < 0)
  119. return status;
  120. /* lock the parent */
  121. mutex_lock(&rec_dir.dentry->d_inode->i_mutex);
  122. dentry = lookup_one_len(dname, rec_dir.dentry, HEXDIR_LEN-1);
  123. if (IS_ERR(dentry)) {
  124. status = PTR_ERR(dentry);
  125. goto out_unlock;
  126. }
  127. status = -EEXIST;
  128. if (dentry->d_inode) {
  129. dprintk("NFSD: nfsd4_create_clid_dir: DIRECTORY EXISTS\n");
  130. goto out_put;
  131. }
  132. status = mnt_want_write(rec_dir.mnt);
  133. if (status)
  134. goto out_put;
  135. status = vfs_mkdir(rec_dir.dentry->d_inode, dentry, S_IRWXU);
  136. mnt_drop_write(rec_dir.mnt);
  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_creds(original_cred);
  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 name_list {
  151. char name[HEXDIR_LEN];
  152. struct list_head list;
  153. };
  154. static int
  155. nfsd4_build_namelist(void *arg, const char *name, int namlen,
  156. loff_t offset, u64 ino, unsigned int d_type)
  157. {
  158. struct list_head *names = arg;
  159. struct name_list *entry;
  160. if (namlen != HEXDIR_LEN - 1)
  161. return 0;
  162. entry = kmalloc(sizeof(struct name_list), GFP_KERNEL);
  163. if (entry == NULL)
  164. return -ENOMEM;
  165. memcpy(entry->name, name, HEXDIR_LEN - 1);
  166. entry->name[HEXDIR_LEN - 1] = '\0';
  167. list_add(&entry->list, names);
  168. return 0;
  169. }
  170. static int
  171. nfsd4_list_rec_dir(struct dentry *dir, recdir_func *f)
  172. {
  173. const struct cred *original_cred;
  174. struct file *filp;
  175. LIST_HEAD(names);
  176. struct name_list *entry;
  177. struct dentry *dentry;
  178. int status;
  179. if (!rec_dir_init)
  180. return 0;
  181. status = nfs4_save_creds(&original_cred);
  182. if (status < 0)
  183. return status;
  184. filp = dentry_open(dget(dir), mntget(rec_dir.mnt), O_RDONLY,
  185. current_cred());
  186. status = PTR_ERR(filp);
  187. if (IS_ERR(filp))
  188. goto out;
  189. status = vfs_readdir(filp, nfsd4_build_namelist, &names);
  190. fput(filp);
  191. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
  192. while (!list_empty(&names)) {
  193. entry = list_entry(names.next, struct name_list, list);
  194. dentry = lookup_one_len(entry->name, dir, HEXDIR_LEN-1);
  195. if (IS_ERR(dentry)) {
  196. status = PTR_ERR(dentry);
  197. break;
  198. }
  199. status = f(dir, dentry);
  200. dput(dentry);
  201. if (status)
  202. break;
  203. list_del(&entry->list);
  204. kfree(entry);
  205. }
  206. mutex_unlock(&dir->d_inode->i_mutex);
  207. out:
  208. while (!list_empty(&names)) {
  209. entry = list_entry(names.next, struct name_list, list);
  210. list_del(&entry->list);
  211. kfree(entry);
  212. }
  213. nfs4_reset_creds(original_cred);
  214. return status;
  215. }
  216. static int
  217. nfsd4_unlink_clid_dir(char *name, int namlen)
  218. {
  219. struct dentry *dentry;
  220. int status;
  221. dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name);
  222. mutex_lock_nested(&rec_dir.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  223. dentry = lookup_one_len(name, rec_dir.dentry, namlen);
  224. if (IS_ERR(dentry)) {
  225. status = PTR_ERR(dentry);
  226. goto out_unlock;
  227. }
  228. status = -ENOENT;
  229. if (!dentry->d_inode)
  230. goto out;
  231. status = vfs_rmdir(rec_dir.dentry->d_inode, dentry);
  232. out:
  233. dput(dentry);
  234. out_unlock:
  235. mutex_unlock(&rec_dir.dentry->d_inode->i_mutex);
  236. return status;
  237. }
  238. void
  239. nfsd4_remove_clid_dir(struct nfs4_client *clp)
  240. {
  241. const struct cred *original_cred;
  242. int status;
  243. if (!rec_dir_init || !clp->cl_firststate)
  244. return;
  245. status = mnt_want_write(rec_dir.mnt);
  246. if (status)
  247. goto out;
  248. clp->cl_firststate = 0;
  249. status = nfs4_save_creds(&original_cred);
  250. if (status < 0)
  251. goto out;
  252. status = nfsd4_unlink_clid_dir(clp->cl_recdir, HEXDIR_LEN-1);
  253. nfs4_reset_creds(original_cred);
  254. if (status == 0)
  255. nfsd4_sync_rec_dir();
  256. mnt_drop_write(rec_dir.mnt);
  257. out:
  258. if (status)
  259. printk("NFSD: Failed to remove expired client state directory"
  260. " %.*s\n", HEXDIR_LEN, clp->cl_recdir);
  261. return;
  262. }
  263. static int
  264. purge_old(struct dentry *parent, struct dentry *child)
  265. {
  266. int status;
  267. /* note: we currently use this path only for minorversion 0 */
  268. if (nfs4_has_reclaimed_state(child->d_name.name, false))
  269. return 0;
  270. status = vfs_rmdir(parent->d_inode, child);
  271. if (status)
  272. printk("failed to remove client recovery directory %s\n",
  273. child->d_name.name);
  274. /* Keep trying, success or failure: */
  275. return 0;
  276. }
  277. void
  278. nfsd4_recdir_purge_old(void) {
  279. int status;
  280. if (!rec_dir_init)
  281. return;
  282. status = mnt_want_write(rec_dir.mnt);
  283. if (status)
  284. goto out;
  285. status = nfsd4_list_rec_dir(rec_dir.dentry, purge_old);
  286. if (status == 0)
  287. nfsd4_sync_rec_dir();
  288. mnt_drop_write(rec_dir.mnt);
  289. out:
  290. if (status)
  291. printk("nfsd4: failed to purge old clients from recovery"
  292. " directory %s\n", rec_dir.dentry->d_name.name);
  293. }
  294. static int
  295. load_recdir(struct dentry *parent, struct dentry *child)
  296. {
  297. if (child->d_name.len != HEXDIR_LEN - 1) {
  298. printk("nfsd4: illegal name %s in recovery directory\n",
  299. child->d_name.name);
  300. /* Keep trying; maybe the others are OK: */
  301. return 0;
  302. }
  303. nfs4_client_to_reclaim(child->d_name.name);
  304. return 0;
  305. }
  306. int
  307. nfsd4_recdir_load(void) {
  308. int status;
  309. status = nfsd4_list_rec_dir(rec_dir.dentry, load_recdir);
  310. if (status)
  311. printk("nfsd4: failed loading clients from recovery"
  312. " directory %s\n", rec_dir.dentry->d_name.name);
  313. return status;
  314. }
  315. /*
  316. * Hold reference to the recovery directory.
  317. */
  318. void
  319. nfsd4_init_recdir(char *rec_dirname)
  320. {
  321. const struct cred *original_cred;
  322. int status;
  323. printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
  324. rec_dirname);
  325. BUG_ON(rec_dir_init);
  326. status = nfs4_save_creds(&original_cred);
  327. if (status < 0) {
  328. printk("NFSD: Unable to change credentials to find recovery"
  329. " directory: error %d\n",
  330. status);
  331. return;
  332. }
  333. status = kern_path(rec_dirname, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
  334. &rec_dir);
  335. if (status)
  336. printk("NFSD: unable to find recovery directory %s\n",
  337. rec_dirname);
  338. if (!status)
  339. rec_dir_init = 1;
  340. nfs4_reset_creds(original_cred);
  341. }
  342. void
  343. nfsd4_shutdown_recdir(void)
  344. {
  345. if (!rec_dir_init)
  346. return;
  347. rec_dir_init = 0;
  348. path_put(&rec_dir);
  349. }