nfs4recover.c 9.7 KB

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