nfs4recover.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. kfree(cksum.data);
  104. status = nfs_ok;
  105. out:
  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 dentry_list {
  161. struct dentry *dentry;
  162. struct list_head list;
  163. };
  164. struct dentry_list_arg {
  165. struct list_head dentries;
  166. struct dentry *parent;
  167. };
  168. static int
  169. nfsd4_build_dentrylist(void *arg, const char *name, int namlen,
  170. loff_t offset, u64 ino, unsigned int d_type)
  171. {
  172. struct dentry_list_arg *dla = arg;
  173. struct list_head *dentries = &dla->dentries;
  174. struct dentry *parent = dla->parent;
  175. struct dentry *dentry;
  176. struct dentry_list *child;
  177. if (name && isdotent(name, namlen))
  178. return 0;
  179. dentry = lookup_one_len(name, parent, namlen);
  180. if (IS_ERR(dentry))
  181. return PTR_ERR(dentry);
  182. child = kmalloc(sizeof(*child), GFP_KERNEL);
  183. if (child == NULL)
  184. return -ENOMEM;
  185. child->dentry = dentry;
  186. list_add(&child->list, dentries);
  187. return 0;
  188. }
  189. static int
  190. nfsd4_list_rec_dir(struct dentry *dir, recdir_func *f)
  191. {
  192. const struct cred *original_cred;
  193. struct file *filp;
  194. struct dentry_list_arg dla = {
  195. .parent = dir,
  196. };
  197. struct list_head *dentries = &dla.dentries;
  198. struct dentry_list *child;
  199. int status;
  200. if (!rec_dir_init)
  201. return 0;
  202. status = nfs4_save_creds(&original_cred);
  203. if (status < 0)
  204. return status;
  205. INIT_LIST_HEAD(dentries);
  206. filp = dentry_open(dget(dir), mntget(rec_dir.mnt), O_RDONLY,
  207. current_cred());
  208. status = PTR_ERR(filp);
  209. if (IS_ERR(filp))
  210. goto out;
  211. INIT_LIST_HEAD(dentries);
  212. status = vfs_readdir(filp, nfsd4_build_dentrylist, &dla);
  213. fput(filp);
  214. while (!list_empty(dentries)) {
  215. child = list_entry(dentries->next, struct dentry_list, list);
  216. status = f(dir, child->dentry);
  217. if (status)
  218. goto out;
  219. list_del(&child->list);
  220. dput(child->dentry);
  221. kfree(child);
  222. }
  223. out:
  224. while (!list_empty(dentries)) {
  225. child = list_entry(dentries->next, struct dentry_list, list);
  226. list_del(&child->list);
  227. dput(child->dentry);
  228. kfree(child);
  229. }
  230. nfs4_reset_creds(original_cred);
  231. return status;
  232. }
  233. static int
  234. nfsd4_remove_clid_file(struct dentry *dir, struct dentry *dentry)
  235. {
  236. int status;
  237. if (!S_ISREG(dir->d_inode->i_mode)) {
  238. printk("nfsd4: non-file found in client recovery directory\n");
  239. return -EINVAL;
  240. }
  241. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
  242. status = vfs_unlink(dir->d_inode, dentry);
  243. mutex_unlock(&dir->d_inode->i_mutex);
  244. return status;
  245. }
  246. static int
  247. nfsd4_clear_clid_dir(struct dentry *dir, struct dentry *dentry)
  248. {
  249. int status;
  250. /* For now this directory should already be empty, but we empty it of
  251. * any regular files anyway, just in case the directory was created by
  252. * a kernel from the future.... */
  253. nfsd4_list_rec_dir(dentry, nfsd4_remove_clid_file);
  254. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
  255. status = vfs_rmdir(dir->d_inode, dentry);
  256. mutex_unlock(&dir->d_inode->i_mutex);
  257. return status;
  258. }
  259. static int
  260. nfsd4_unlink_clid_dir(char *name, int namlen)
  261. {
  262. struct dentry *dentry;
  263. int status;
  264. dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name);
  265. mutex_lock(&rec_dir.dentry->d_inode->i_mutex);
  266. dentry = lookup_one_len(name, rec_dir.dentry, namlen);
  267. mutex_unlock(&rec_dir.dentry->d_inode->i_mutex);
  268. if (IS_ERR(dentry)) {
  269. status = PTR_ERR(dentry);
  270. return status;
  271. }
  272. status = -ENOENT;
  273. if (!dentry->d_inode)
  274. goto out;
  275. status = nfsd4_clear_clid_dir(rec_dir.dentry, dentry);
  276. out:
  277. dput(dentry);
  278. return status;
  279. }
  280. void
  281. nfsd4_remove_clid_dir(struct nfs4_client *clp)
  282. {
  283. const struct cred *original_cred;
  284. int status;
  285. if (!rec_dir_init || !clp->cl_firststate)
  286. return;
  287. status = mnt_want_write(rec_dir.mnt);
  288. if (status)
  289. goto out;
  290. clp->cl_firststate = 0;
  291. status = nfs4_save_creds(&original_cred);
  292. if (status < 0)
  293. goto out;
  294. status = nfsd4_unlink_clid_dir(clp->cl_recdir, HEXDIR_LEN-1);
  295. nfs4_reset_creds(original_cred);
  296. if (status == 0)
  297. nfsd4_sync_rec_dir();
  298. mnt_drop_write(rec_dir.mnt);
  299. out:
  300. if (status)
  301. printk("NFSD: Failed to remove expired client state directory"
  302. " %.*s\n", HEXDIR_LEN, clp->cl_recdir);
  303. return;
  304. }
  305. static int
  306. purge_old(struct dentry *parent, struct dentry *child)
  307. {
  308. int status;
  309. if (nfs4_has_reclaimed_state(child->d_name.name))
  310. return 0;
  311. status = nfsd4_clear_clid_dir(parent, child);
  312. if (status)
  313. printk("failed to remove client recovery directory %s\n",
  314. child->d_name.name);
  315. /* Keep trying, success or failure: */
  316. return 0;
  317. }
  318. void
  319. nfsd4_recdir_purge_old(void) {
  320. int status;
  321. if (!rec_dir_init)
  322. return;
  323. status = mnt_want_write(rec_dir.mnt);
  324. if (status)
  325. goto out;
  326. status = nfsd4_list_rec_dir(rec_dir.dentry, purge_old);
  327. if (status == 0)
  328. nfsd4_sync_rec_dir();
  329. mnt_drop_write(rec_dir.mnt);
  330. out:
  331. if (status)
  332. printk("nfsd4: failed to purge old clients from recovery"
  333. " directory %s\n", rec_dir.dentry->d_name.name);
  334. }
  335. static int
  336. load_recdir(struct dentry *parent, struct dentry *child)
  337. {
  338. if (child->d_name.len != HEXDIR_LEN - 1) {
  339. printk("nfsd4: illegal name %s in recovery directory\n",
  340. child->d_name.name);
  341. /* Keep trying; maybe the others are OK: */
  342. return 0;
  343. }
  344. nfs4_client_to_reclaim(child->d_name.name);
  345. return 0;
  346. }
  347. int
  348. nfsd4_recdir_load(void) {
  349. int status;
  350. status = nfsd4_list_rec_dir(rec_dir.dentry, load_recdir);
  351. if (status)
  352. printk("nfsd4: failed loading clients from recovery"
  353. " directory %s\n", rec_dir.dentry->d_name.name);
  354. return status;
  355. }
  356. /*
  357. * Hold reference to the recovery directory.
  358. */
  359. void
  360. nfsd4_init_recdir(char *rec_dirname)
  361. {
  362. const struct cred *original_cred;
  363. int status;
  364. printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
  365. rec_dirname);
  366. BUG_ON(rec_dir_init);
  367. status = nfs4_save_creds(&original_cred);
  368. if (status < 0) {
  369. printk("NFSD: Unable to change credentials to find recovery"
  370. " directory: error %d\n",
  371. status);
  372. return;
  373. }
  374. status = kern_path(rec_dirname, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
  375. &rec_dir);
  376. if (status)
  377. printk("NFSD: unable to find recovery directory %s\n",
  378. rec_dirname);
  379. if (!status)
  380. rec_dir_init = 1;
  381. nfs4_reset_creds(original_cred);
  382. }
  383. void
  384. nfsd4_shutdown_recdir(void)
  385. {
  386. if (!rec_dir_init)
  387. return;
  388. rec_dir_init = 0;
  389. path_put(&rec_dir);
  390. }