nfs4recover.c 10 KB

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