]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/nfsd/nfs4recover.c
2 * linux/fs/nfsd/nfs4recover.c
4 * Copyright (c) 2004 The Regents of the University of Michigan.
7 * Andy Adamson <andros@citi.umich.edu>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
37 #include <linux/sunrpc/svc.h>
38 #include <linux/nfsd/nfsd.h>
39 #include <linux/nfs4.h>
40 #include <linux/nfsd/state.h>
41 #include <linux/nfsd/xdr4.h>
42 #include <linux/param.h>
43 #include <linux/file.h>
44 #include <linux/namei.h>
45 #include <asm/uaccess.h>
46 #include <asm/scatterlist.h>
47 #include <linux/crypto.h>
50 #define NFSDDBG_FACILITY NFSDDBG_PROC
53 char recovery_dirname
[PATH_MAX
] = "/var/lib/nfs/v4recovery";
54 static struct nameidata rec_dir
;
55 static int rec_dir_init
= 0;
58 nfs4_save_user(uid_t
*saveuid
, gid_t
*savegid
)
60 *saveuid
= current
->fsuid
;
61 *savegid
= current
->fsgid
;
67 nfs4_reset_user(uid_t saveuid
, gid_t savegid
)
69 current
->fsuid
= saveuid
;
70 current
->fsgid
= savegid
;
74 md5_to_hex(char *out
, char *md5
)
78 for (i
=0; i
<16; i
++) {
79 unsigned char c
= md5
[i
];
81 *out
++ = '0' + ((c
&0xf0)>>4) + (c
>=0xa0)*('a'-'9'-1);
82 *out
++ = '0' + (c
&0x0f) + ((c
&0x0f)>=0x0a)*('a'-'9'-1);
88 nfs4_make_rec_clidname(char *dname
, struct xdr_netobj
*clname
)
90 struct xdr_netobj cksum
;
91 struct crypto_tfm
*tfm
;
92 struct scatterlist sg
[1];
93 int status
= nfserr_resource
;
95 dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
96 clname
->len
, clname
->data
);
97 tfm
= crypto_alloc_tfm("md5", 0);
100 cksum
.len
= crypto_tfm_alg_digestsize(tfm
);
101 cksum
.data
= kmalloc(cksum
.len
, GFP_KERNEL
);
102 if (cksum
.data
== NULL
)
104 crypto_digest_init(tfm
);
106 sg
[0].page
= virt_to_page(clname
->data
);
107 sg
[0].offset
= offset_in_page(clname
->data
);
108 sg
[0].length
= clname
->len
;
110 crypto_digest_update(tfm
, sg
, 1);
111 crypto_digest_final(tfm
, cksum
.data
);
113 md5_to_hex(dname
, cksum
.data
);
119 crypto_free_tfm(tfm
);
123 typedef int (recdir_func
)(struct dentry
*, struct dentry
*);
126 struct dentry
*dentry
;
127 struct list_head list
;
130 struct dentry_list_arg
{
131 struct list_head dentries
;
132 struct dentry
*parent
;
136 nfsd4_build_dentrylist(void *arg
, const char *name
, int namlen
,
137 loff_t offset
, ino_t ino
, unsigned int d_type
)
139 struct dentry_list_arg
*dla
= arg
;
140 struct list_head
*dentries
= &dla
->dentries
;
141 struct dentry
*parent
= dla
->parent
;
142 struct dentry
*dentry
;
143 struct dentry_list
*child
;
145 if (name
&& isdotent(name
, namlen
))
147 dentry
= lookup_one_len(name
, parent
, namlen
);
149 return PTR_ERR(dentry
);
150 child
= kmalloc(sizeof(*child
), GFP_KERNEL
);
153 child
->dentry
= dentry
;
154 list_add(&child
->list
, dentries
);
159 nfsd4_list_rec_dir(struct dentry
*dir
, recdir_func
*f
)
162 struct dentry_list_arg dla
= {
165 struct list_head
*dentries
= &dla
.dentries
;
166 struct dentry_list
*child
;
174 nfs4_save_user(&uid
, &gid
);
176 filp
= dentry_open(dget(dir
), mntget(rec_dir
.mnt
),
178 status
= PTR_ERR(filp
);
181 INIT_LIST_HEAD(dentries
);
182 status
= vfs_readdir(filp
, nfsd4_build_dentrylist
, &dla
);
184 while (!list_empty(dentries
)) {
185 child
= list_entry(dentries
->next
, struct dentry_list
, list
);
186 status
= f(dir
, child
->dentry
);
189 list_del(&child
->list
);
194 while (!list_empty(dentries
)) {
195 child
= list_entry(dentries
->next
, struct dentry_list
, list
);
196 list_del(&child
->list
);
200 nfs4_reset_user(uid
, gid
);
205 load_recdir(struct dentry
*parent
, struct dentry
*child
)
207 if (child
->d_name
.len
!= HEXDIR_LEN
- 1) {
208 printk("nfsd4: illegal name %s in recovery directory\n",
210 /* Keep trying; maybe the others are OK: */
213 nfs4_client_to_reclaim(child
->d_name
.name
);
218 nfsd4_recdir_load(void) {
221 status
= nfsd4_list_rec_dir(rec_dir
.dentry
, load_recdir
);
223 printk("nfsd4: failed loading clients from recovery"
224 " directory %s\n", rec_dir
.dentry
->d_name
.name
);
229 * Hold reference to the recovery directory.
233 nfsd4_init_recdir(char *rec_dirname
)
239 printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
242 BUG_ON(rec_dir_init
);
244 nfs4_save_user(&uid
, &gid
);
246 status
= path_lookup(rec_dirname
, LOOKUP_FOLLOW
, &rec_dir
);
247 if (status
== -ENOENT
)
248 printk("NFSD: recovery directory %s doesn't exist\n",
253 nfs4_reset_user(uid
, gid
);
257 nfsd4_shutdown_recdir(void)
262 path_release(&rec_dir
);