nfs4recover.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. while (!list_empty(&names)) {
  202. entry = list_entry(names.next, struct name_list, list);
  203. dentry = lookup_one_len(entry->name, dir, HEXDIR_LEN-1);
  204. if (IS_ERR(dentry)) {
  205. status = PTR_ERR(dentry);
  206. goto out;
  207. }
  208. status = f(dir, dentry);
  209. dput(dentry);
  210. if (status)
  211. goto out;
  212. list_del(&entry->list);
  213. kfree(entry);
  214. }
  215. out:
  216. while (!list_empty(&names)) {
  217. entry = list_entry(names.next, struct name_list, list);
  218. list_del(&entry->list);
  219. kfree(entry);
  220. }
  221. nfs4_reset_creds(original_cred);
  222. return status;
  223. }
  224. static int
  225. nfsd4_remove_clid_file(struct dentry *dir, struct dentry *dentry)
  226. {
  227. int status;
  228. if (!S_ISREG(dir->d_inode->i_mode)) {
  229. printk("nfsd4: non-file found in client recovery directory\n");
  230. return -EINVAL;
  231. }
  232. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
  233. status = vfs_unlink(dir->d_inode, dentry);
  234. mutex_unlock(&dir->d_inode->i_mutex);
  235. return status;
  236. }
  237. static int
  238. nfsd4_clear_clid_dir(struct dentry *dir, struct dentry *dentry)
  239. {
  240. int status;
  241. /* For now this directory should already be empty, but we empty it of
  242. * any regular files anyway, just in case the directory was created by
  243. * a kernel from the future.... */
  244. nfsd4_list_rec_dir(dentry, nfsd4_remove_clid_file);
  245. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
  246. status = vfs_rmdir(dir->d_inode, dentry);
  247. mutex_unlock(&dir->d_inode->i_mutex);
  248. return status;
  249. }
  250. static int
  251. nfsd4_unlink_clid_dir(char *name, int namlen)
  252. {
  253. struct dentry *dentry;
  254. int status;
  255. dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name);
  256. mutex_lock(&rec_dir.dentry->d_inode->i_mutex);
  257. dentry = lookup_one_len(name, rec_dir.dentry, namlen);
  258. mutex_unlock(&rec_dir.dentry->d_inode->i_mutex);
  259. if (IS_ERR(dentry)) {
  260. status = PTR_ERR(dentry);
  261. return status;
  262. }
  263. status = -ENOENT;
  264. if (!dentry->d_inode)
  265. goto out;
  266. status = nfsd4_clear_clid_dir(rec_dir.dentry, dentry);
  267. out:
  268. dput(dentry);
  269. return status;
  270. }
  271. void
  272. nfsd4_remove_clid_dir(struct nfs4_client *clp)
  273. {
  274. const struct cred *original_cred;
  275. int status;
  276. if (!rec_dir_init || !clp->cl_firststate)
  277. return;
  278. status = mnt_want_write(rec_dir.mnt);
  279. if (status)
  280. goto out;
  281. clp->cl_firststate = 0;
  282. status = nfs4_save_creds(&original_cred);
  283. if (status < 0)
  284. goto out;
  285. status = nfsd4_unlink_clid_dir(clp->cl_recdir, HEXDIR_LEN-1);
  286. nfs4_reset_creds(original_cred);
  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. /* note: we currently use this path only for minorversion 0 */
  301. if (nfs4_has_reclaimed_state(child->d_name.name, false))
  302. return 0;
  303. status = nfsd4_clear_clid_dir(parent, child);
  304. if (status)
  305. printk("failed to remove client recovery directory %s\n",
  306. child->d_name.name);
  307. /* Keep trying, success or failure: */
  308. return 0;
  309. }
  310. void
  311. nfsd4_recdir_purge_old(void) {
  312. int status;
  313. if (!rec_dir_init)
  314. return;
  315. status = mnt_want_write(rec_dir.mnt);
  316. if (status)
  317. goto out;
  318. status = nfsd4_list_rec_dir(rec_dir.dentry, purge_old);
  319. if (status == 0)
  320. nfsd4_sync_rec_dir();
  321. mnt_drop_write(rec_dir.mnt);
  322. out:
  323. if (status)
  324. printk("nfsd4: failed to purge old clients from recovery"
  325. " directory %s\n", rec_dir.dentry->d_name.name);
  326. }
  327. static int
  328. load_recdir(struct dentry *parent, struct dentry *child)
  329. {
  330. if (child->d_name.len != HEXDIR_LEN - 1) {
  331. printk("nfsd4: illegal name %s in recovery directory\n",
  332. child->d_name.name);
  333. /* Keep trying; maybe the others are OK: */
  334. return 0;
  335. }
  336. nfs4_client_to_reclaim(child->d_name.name);
  337. return 0;
  338. }
  339. int
  340. nfsd4_recdir_load(void) {
  341. int status;
  342. status = nfsd4_list_rec_dir(rec_dir.dentry, load_recdir);
  343. if (status)
  344. printk("nfsd4: failed loading clients from recovery"
  345. " directory %s\n", rec_dir.dentry->d_name.name);
  346. return status;
  347. }
  348. /*
  349. * Hold reference to the recovery directory.
  350. */
  351. void
  352. nfsd4_init_recdir(char *rec_dirname)
  353. {
  354. const struct cred *original_cred;
  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. status = nfs4_save_creds(&original_cred);
  360. if (status < 0) {
  361. printk("NFSD: Unable to change credentials to find recovery"
  362. " directory: error %d\n",
  363. status);
  364. return;
  365. }
  366. status = kern_path(rec_dirname, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
  367. &rec_dir);
  368. if (status)
  369. printk("NFSD: unable to find recovery directory %s\n",
  370. rec_dirname);
  371. if (!status)
  372. rec_dir_init = 1;
  373. nfs4_reset_creds(original_cred);
  374. }
  375. void
  376. nfsd4_shutdown_recdir(void)
  377. {
  378. if (!rec_dir_init)
  379. return;
  380. rec_dir_init = 0;
  381. path_put(&rec_dir);
  382. }