This source file includes following definitions.
- nfs4_label_init_security
- nfs4_label_release_security
- nfs4_bitmask
- nfs4_label_init_security
- nfs4_label_release_security
- nfs4_bitmask
- nfs4_map_errors
- nfs4_bitmap_copy_adjust
- nfs4_bitmap_copy_adjust_setattr
- nfs4_setup_readdir
- nfs4_test_and_free_stateid
- __nfs4_free_revoked_stateid
- nfs4_free_revoked_stateid
- nfs4_update_delay
- nfs4_delay_killable
- nfs4_delay_interruptible
- nfs4_delay
- nfs4_recoverable_stateid
- nfs4_do_handle_exception
- nfs4_handle_exception
- nfs4_async_handle_exception
- nfs4_async_handle_error
- _nfs4_is_integrity_protected
- do_renew_lease
- renew_lease
- nfs4_init_sequence
- nfs40_sequence_free_slot
- nfs40_sequence_done
- nfs41_release_slot
- nfs41_sequence_free_slot
- nfs4_slot_sequence_record_sent
- nfs4_slot_sequence_acked
- nfs41_sequence_process
- nfs41_sequence_done
- nfs4_sequence_process
- nfs4_sequence_free_slot
- nfs4_sequence_done
- nfs41_call_sync_prepare
- nfs41_call_sync_done
- nfs4_sequence_process
- nfs4_sequence_free_slot
- nfs4_sequence_done
- nfs41_sequence_res_init
- nfs4_sequence_attach_slot
- nfs4_setup_sequence
- nfs40_call_sync_prepare
- nfs40_call_sync_done
- nfs4_call_sync_custom
- nfs4_call_sync_sequence
- nfs4_call_sync
- nfs4_inc_nlink_locked
- nfs4_dec_nlink_locked
- update_changeattr_locked
- update_changeattr
- nfs4_clear_cap_atomic_open_v1
- _nfs4_ctx_to_accessmode
- _nfs4_ctx_to_openmode
- nfs4_map_atomic_open_share
- nfs4_map_atomic_open_claim
- nfs4_init_opendata_res
- nfs4_opendata_alloc
- nfs4_opendata_free
- nfs4_opendata_put
- nfs4_mode_match_open_stateid
- can_open_cached
- can_open_delegated
- update_open_stateflags
- nfs_open_stateid_recover_openmode
- nfs_state_log_update_open_stateid
- nfs_state_log_out_of_order_open_stateid
- nfs_test_and_clear_all_open_stateid
- nfs_need_update_open_stateid
- nfs_resync_open_stateid_locked
- nfs_clear_open_stateid_locked
- nfs_clear_open_stateid
- nfs_set_open_stateid_locked
- nfs_state_set_open_stateid
- nfs_state_clear_open_state_flags
- nfs_state_set_delegation
- nfs_state_clear_delegation
- update_open_stateid
- nfs4_update_lock_stateid
- nfs4_return_incompatible_delegation
- nfs4_try_open_cached
- nfs4_opendata_check_deleg
- _nfs4_opendata_reclaim_to_nfs4_state
- nfs4_opendata_get_inode
- nfs4_opendata_find_nfs4_state
- _nfs4_opendata_to_nfs4_state
- nfs4_opendata_to_nfs4_state
- nfs4_state_find_open_context_mode
- nfs4_state_find_open_context
- nfs4_open_recoverdata_alloc
- nfs4_open_recover_helper
- nfs4_open_recover
- _nfs4_do_open_reclaim
- nfs4_do_open_reclaim
- nfs4_open_reclaim
- nfs4_handle_delegation_recall_error
- nfs4_open_delegation_recall
- nfs4_open_confirm_prepare
- nfs4_open_confirm_done
- nfs4_open_confirm_release
- _nfs4_proc_open_confirm
- nfs4_open_prepare
- nfs4_open_done
- nfs4_open_release
- nfs4_run_open_task
- _nfs4_recover_proc_open
- nfs4_opendata_access
- _nfs4_proc_open
- _nfs4_open_expired
- nfs4_do_open_expired
- nfs4_open_expired
- nfs_finish_clear_delegation_stateid
- nfs40_clear_delegation_stateid
- nfs40_open_expired
- nfs40_test_and_free_expired_stateid
- nfs41_test_and_free_expired_stateid
- nfs41_check_delegation_stateid
- nfs41_delegation_recover_stateid
- nfs41_check_expired_locks
- nfs41_check_open_stateid
- nfs41_open_expired
- nfs4_exclusive_attrset
- _nfs4_open_and_get_state
- _nfs4_do_open
- nfs4_do_open
- _nfs4_do_setattr
- nfs4_do_setattr
- nfs4_wait_on_layoutreturn
- nfs4_sync_open_stateid
- nfs4_refresh_open_old_stateid
- nfs4_free_closedata
- nfs4_close_done
- nfs4_close_prepare
- nfs4_do_close
- nfs4_atomic_open
- nfs4_close_context
- _nfs4_server_capabilities
- nfs4_server_capabilities
- _nfs4_lookup_root
- nfs4_lookup_root
- nfs4_lookup_root_sec
- nfs4_find_root_sec
- nfs4_proc_get_rootfh
- nfs4_proc_get_root
- nfs4_get_referral
- _nfs4_proc_getattr
- nfs4_proc_getattr
- nfs4_proc_setattr
- _nfs4_proc_lookup
- nfs_fixup_secinfo_attributes
- nfs4_proc_lookup_common
- nfs4_proc_lookup
- nfs4_proc_lookup_mountpoint
- _nfs4_proc_lookupp
- nfs4_proc_lookupp
- _nfs4_proc_access
- nfs4_proc_access
- _nfs4_proc_readlink
- nfs4_proc_readlink
- nfs4_proc_create
- _nfs4_proc_remove
- nfs4_proc_remove
- nfs4_proc_rmdir
- nfs4_proc_unlink_setup
- nfs4_proc_unlink_rpc_prepare
- nfs4_proc_unlink_done
- nfs4_proc_rename_setup
- nfs4_proc_rename_rpc_prepare
- nfs4_proc_rename_done
- _nfs4_proc_link
- nfs4_proc_link
- nfs4_alloc_createdata
- nfs4_do_create
- nfs4_free_createdata
- _nfs4_proc_symlink
- nfs4_proc_symlink
- _nfs4_proc_mkdir
- nfs4_proc_mkdir
- _nfs4_proc_readdir
- nfs4_proc_readdir
- _nfs4_proc_mknod
- nfs4_proc_mknod
- _nfs4_proc_statfs
- nfs4_proc_statfs
- _nfs4_do_fsinfo
- nfs4_do_fsinfo
- nfs4_proc_fsinfo
- _nfs4_proc_pathconf
- nfs4_proc_pathconf
- nfs4_set_rw_stateid
- nfs4_stateid_is_current
- nfs4_error_stateid_expired
- nfs4_read_done_cb
- nfs4_read_stateid_changed
- nfs4_read_done
- nfs4_proc_read_setup
- nfs4_proc_pgio_rpc_prepare
- nfs4_write_done_cb
- nfs4_write_stateid_changed
- nfs4_write_done
- nfs4_write_need_cache_consistency_data
- nfs4_proc_write_setup
- nfs4_proc_commit_rpc_prepare
- nfs4_commit_done_cb
- nfs4_commit_done
- nfs4_proc_commit_setup
- _nfs4_proc_commit
- nfs4_proc_commit
- nfs4_renew_release
- nfs4_renew_done
- nfs4_proc_async_renew
- nfs4_proc_renew
- nfs4_server_supports_acls
- buf_to_pages_noslab
- nfs4_set_cached_acl
- nfs4_zap_acl_attr
- nfs4_read_cached_acl
- nfs4_write_cached_acl
- __nfs4_get_acl_uncached
- nfs4_get_acl_uncached
- nfs4_proc_get_acl
- __nfs4_proc_set_acl
- nfs4_proc_set_acl
- _nfs4_get_security_label
- nfs4_get_security_label
- _nfs4_do_set_security_label
- nfs4_do_set_security_label
- nfs4_set_security_label
- nfs4_init_boot_verifier
- nfs4_init_nonuniform_client_string
- nfs4_init_uniquifier_client_string
- nfs4_init_uniform_client_string
- nfs4_init_callback_netid
- nfs4_setclientid_done
- nfs4_proc_setclientid
- nfs4_proc_setclientid_confirm
- nfs4_delegreturn_done
- nfs4_delegreturn_release
- nfs4_delegreturn_prepare
- _nfs4_proc_delegreturn
- nfs4_proc_delegreturn
- _nfs4_proc_getlk
- nfs4_proc_getlk
- nfs4_refresh_lock_old_stateid
- nfs4_sync_lock_stateid
- nfs4_alloc_unlockdata
- nfs4_locku_release_calldata
- nfs4_locku_done
- nfs4_locku_prepare
- nfs4_do_unlck
- nfs4_proc_unlck
- nfs4_alloc_lockdata
- nfs4_lock_prepare
- nfs4_lock_done
- nfs4_lock_release
- nfs4_handle_setlk_error
- _nfs4_do_setlk
- nfs4_lock_reclaim
- nfs4_lock_expired
- nfs41_lock_expired
- _nfs4_proc_setlk
- nfs4_proc_setlk
- nfs4_retry_setlk_simple
- nfs4_wake_lock_waiter
- nfs4_retry_setlk
- nfs4_retry_setlk
- nfs4_proc_lock
- nfs4_lock_delegation_recall
- nfs4_release_lockowner_prepare
- nfs4_release_lockowner_done
- nfs4_release_lockowner_release
- nfs4_release_lockowner
- nfs4_xattr_set_nfs4_acl
- nfs4_xattr_get_nfs4_acl
- nfs4_xattr_list_nfs4_acl
- nfs4_xattr_set_nfs4_label
- nfs4_xattr_get_nfs4_label
- nfs4_listxattr_nfs4_label
- nfs4_listxattr_nfs4_label
- nfs_fixup_referral_attributes
- _nfs4_proc_fs_locations
- nfs4_proc_fs_locations
- _nfs40_proc_get_locations
- _nfs41_proc_get_locations
- nfs4_proc_get_locations
- _nfs40_proc_fsid_present
- _nfs41_proc_fsid_present
- nfs4_proc_fsid_present
- _nfs4_proc_secinfo
- nfs4_proc_secinfo
- nfs4_check_cl_exchange_flags
- nfs41_same_server_scope
- nfs4_bind_one_conn_to_session_done
- nfs4_proc_bind_one_conn_to_session
- nfs4_proc_bind_conn_to_session_callback
- nfs4_proc_bind_conn_to_session
- nfs4_sp4_select_mode
- nfs4_exchange_id_release
- nfs4_run_exchange_id
- _nfs4_proc_exchange_id
- nfs4_proc_exchange_id
- nfs4_test_session_trunk
- _nfs4_proc_destroy_clientid
- nfs4_proc_destroy_clientid
- nfs4_destroy_clientid
- nfs4_get_lease_time_prepare
- nfs4_get_lease_time_done
- nfs4_proc_get_lease_time
- nfs4_init_channel_attrs
- nfs4_verify_fore_channel_attrs
- nfs4_verify_back_channel_attrs
- nfs4_verify_channel_attrs
- nfs4_update_session
- _nfs4_proc_create_session
- nfs4_proc_create_session
- nfs4_proc_destroy_session
- nfs41_sequence_release
- nfs41_sequence_handle_errors
- nfs41_sequence_call_done
- nfs41_sequence_prepare
- _nfs41_proc_sequence
- nfs41_proc_async_sequence
- nfs4_proc_sequence
- nfs4_reclaim_complete_prepare
- nfs41_reclaim_complete_handle_errors
- nfs4_reclaim_complete_done
- nfs4_free_reclaim_complete_data
- nfs41_proc_reclaim_complete
- nfs4_layoutget_prepare
- nfs4_layoutget_done
- nfs4_layoutget_handle_exception
- max_response_pages
- nfs4_layoutget_release
- nfs4_proc_layoutget
- nfs4_layoutreturn_prepare
- nfs4_layoutreturn_done
- nfs4_layoutreturn_release
- nfs4_proc_layoutreturn
- _nfs4_proc_getdeviceinfo
- nfs4_proc_getdeviceinfo
- nfs4_layoutcommit_prepare
- nfs4_layoutcommit_done
- nfs4_layoutcommit_release
- nfs4_proc_layoutcommit
- _nfs41_proc_secinfo_no_name
- nfs41_proc_secinfo_no_name
- nfs41_find_root_sec
- _nfs41_test_stateid
- nfs4_handle_delay_or_session_error
- nfs41_test_stateid
- nfs41_free_stateid_prepare
- nfs41_free_stateid_done
- nfs41_free_stateid_release
- nfs41_free_stateid
- nfs41_free_lock_state
- nfs41_match_stateid
- nfs4_match_stateid
- nfs_alloc_no_seqid
- nfs4_listxattr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 #include <linux/mm.h>
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/xattr.h>
55 #include <linux/utsname.h>
56 #include <linux/freezer.h>
57 #include <linux/iversion.h>
58
59 #include "nfs4_fs.h"
60 #include "delegation.h"
61 #include "internal.h"
62 #include "iostat.h"
63 #include "callback.h"
64 #include "pnfs.h"
65 #include "netns.h"
66 #include "nfs4idmap.h"
67 #include "nfs4session.h"
68 #include "fscache.h"
69
70 #include "nfs4trace.h"
71
72 #define NFSDBG_FACILITY NFSDBG_PROC
73
74 #define NFS4_BITMASK_SZ 3
75
76 #define NFS4_POLL_RETRY_MIN (HZ/10)
77 #define NFS4_POLL_RETRY_MAX (15*HZ)
78
79
80 #define NFS4_VALID_ATTRS (ATTR_MODE \
81 | ATTR_UID \
82 | ATTR_GID \
83 | ATTR_SIZE \
84 | ATTR_ATIME \
85 | ATTR_MTIME \
86 | ATTR_CTIME \
87 | ATTR_ATIME_SET \
88 | ATTR_MTIME_SET)
89
90 struct nfs4_opendata;
91 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
92 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
93 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
94 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label, struct inode *inode);
95 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
96 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
97 struct nfs_fattr *fattr, struct iattr *sattr,
98 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
99 struct nfs4_label *olabel);
100 #ifdef CONFIG_NFS_V4_1
101 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
102 const struct cred *cred,
103 struct nfs4_slot *slot,
104 bool is_privileged);
105 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
106 const struct cred *);
107 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
108 const struct cred *, bool);
109 #endif
110
111 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
112 static inline struct nfs4_label *
113 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
114 struct iattr *sattr, struct nfs4_label *label)
115 {
116 int err;
117
118 if (label == NULL)
119 return NULL;
120
121 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
122 return NULL;
123
124 err = security_dentry_init_security(dentry, sattr->ia_mode,
125 &dentry->d_name, (void **)&label->label, &label->len);
126 if (err == 0)
127 return label;
128
129 return NULL;
130 }
131 static inline void
132 nfs4_label_release_security(struct nfs4_label *label)
133 {
134 if (label)
135 security_release_secctx(label->label, label->len);
136 }
137 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
138 {
139 if (label)
140 return server->attr_bitmask;
141
142 return server->attr_bitmask_nl;
143 }
144 #else
145 static inline struct nfs4_label *
146 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
147 struct iattr *sattr, struct nfs4_label *l)
148 { return NULL; }
149 static inline void
150 nfs4_label_release_security(struct nfs4_label *label)
151 { return; }
152 static inline u32 *
153 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
154 { return server->attr_bitmask; }
155 #endif
156
157
158 static int nfs4_map_errors(int err)
159 {
160 if (err >= -1000)
161 return err;
162 switch (err) {
163 case -NFS4ERR_RESOURCE:
164 case -NFS4ERR_LAYOUTTRYLATER:
165 case -NFS4ERR_RECALLCONFLICT:
166 return -EREMOTEIO;
167 case -NFS4ERR_WRONGSEC:
168 case -NFS4ERR_WRONG_CRED:
169 return -EPERM;
170 case -NFS4ERR_BADOWNER:
171 case -NFS4ERR_BADNAME:
172 return -EINVAL;
173 case -NFS4ERR_SHARE_DENIED:
174 return -EACCES;
175 case -NFS4ERR_MINOR_VERS_MISMATCH:
176 return -EPROTONOSUPPORT;
177 case -NFS4ERR_FILE_OPEN:
178 return -EBUSY;
179 default:
180 dprintk("%s could not handle NFSv4 error %d\n",
181 __func__, -err);
182 break;
183 }
184 return -EIO;
185 }
186
187
188
189
190 const u32 nfs4_fattr_bitmap[3] = {
191 FATTR4_WORD0_TYPE
192 | FATTR4_WORD0_CHANGE
193 | FATTR4_WORD0_SIZE
194 | FATTR4_WORD0_FSID
195 | FATTR4_WORD0_FILEID,
196 FATTR4_WORD1_MODE
197 | FATTR4_WORD1_NUMLINKS
198 | FATTR4_WORD1_OWNER
199 | FATTR4_WORD1_OWNER_GROUP
200 | FATTR4_WORD1_RAWDEV
201 | FATTR4_WORD1_SPACE_USED
202 | FATTR4_WORD1_TIME_ACCESS
203 | FATTR4_WORD1_TIME_METADATA
204 | FATTR4_WORD1_TIME_MODIFY
205 | FATTR4_WORD1_MOUNTED_ON_FILEID,
206 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
207 FATTR4_WORD2_SECURITY_LABEL
208 #endif
209 };
210
211 static const u32 nfs4_pnfs_open_bitmap[3] = {
212 FATTR4_WORD0_TYPE
213 | FATTR4_WORD0_CHANGE
214 | FATTR4_WORD0_SIZE
215 | FATTR4_WORD0_FSID
216 | FATTR4_WORD0_FILEID,
217 FATTR4_WORD1_MODE
218 | FATTR4_WORD1_NUMLINKS
219 | FATTR4_WORD1_OWNER
220 | FATTR4_WORD1_OWNER_GROUP
221 | FATTR4_WORD1_RAWDEV
222 | FATTR4_WORD1_SPACE_USED
223 | FATTR4_WORD1_TIME_ACCESS
224 | FATTR4_WORD1_TIME_METADATA
225 | FATTR4_WORD1_TIME_MODIFY,
226 FATTR4_WORD2_MDSTHRESHOLD
227 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
228 | FATTR4_WORD2_SECURITY_LABEL
229 #endif
230 };
231
232 static const u32 nfs4_open_noattr_bitmap[3] = {
233 FATTR4_WORD0_TYPE
234 | FATTR4_WORD0_FILEID,
235 };
236
237 const u32 nfs4_statfs_bitmap[3] = {
238 FATTR4_WORD0_FILES_AVAIL
239 | FATTR4_WORD0_FILES_FREE
240 | FATTR4_WORD0_FILES_TOTAL,
241 FATTR4_WORD1_SPACE_AVAIL
242 | FATTR4_WORD1_SPACE_FREE
243 | FATTR4_WORD1_SPACE_TOTAL
244 };
245
246 const u32 nfs4_pathconf_bitmap[3] = {
247 FATTR4_WORD0_MAXLINK
248 | FATTR4_WORD0_MAXNAME,
249 0
250 };
251
252 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
253 | FATTR4_WORD0_MAXREAD
254 | FATTR4_WORD0_MAXWRITE
255 | FATTR4_WORD0_LEASE_TIME,
256 FATTR4_WORD1_TIME_DELTA
257 | FATTR4_WORD1_FS_LAYOUT_TYPES,
258 FATTR4_WORD2_LAYOUT_BLKSIZE
259 | FATTR4_WORD2_CLONE_BLKSIZE
260 };
261
262 const u32 nfs4_fs_locations_bitmap[3] = {
263 FATTR4_WORD0_CHANGE
264 | FATTR4_WORD0_SIZE
265 | FATTR4_WORD0_FSID
266 | FATTR4_WORD0_FILEID
267 | FATTR4_WORD0_FS_LOCATIONS,
268 FATTR4_WORD1_OWNER
269 | FATTR4_WORD1_OWNER_GROUP
270 | FATTR4_WORD1_RAWDEV
271 | FATTR4_WORD1_SPACE_USED
272 | FATTR4_WORD1_TIME_ACCESS
273 | FATTR4_WORD1_TIME_METADATA
274 | FATTR4_WORD1_TIME_MODIFY
275 | FATTR4_WORD1_MOUNTED_ON_FILEID,
276 };
277
278 static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
279 struct inode *inode)
280 {
281 unsigned long cache_validity;
282
283 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
284 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
285 return;
286
287 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
288 if (!(cache_validity & NFS_INO_REVAL_FORCED))
289 cache_validity &= ~(NFS_INO_INVALID_CHANGE
290 | NFS_INO_INVALID_SIZE);
291
292 if (!(cache_validity & NFS_INO_INVALID_SIZE))
293 dst[0] &= ~FATTR4_WORD0_SIZE;
294
295 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
296 dst[0] &= ~FATTR4_WORD0_CHANGE;
297 }
298
299 static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
300 const __u32 *src, struct inode *inode)
301 {
302 nfs4_bitmap_copy_adjust(dst, src, inode);
303 }
304
305 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
306 struct nfs4_readdir_arg *readdir)
307 {
308 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
309 __be32 *start, *p;
310
311 if (cookie > 2) {
312 readdir->cookie = cookie;
313 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
314 return;
315 }
316
317 readdir->cookie = 0;
318 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
319 if (cookie == 2)
320 return;
321
322
323
324
325
326
327
328
329 start = p = kmap_atomic(*readdir->pages);
330
331 if (cookie == 0) {
332 *p++ = xdr_one;
333 *p++ = xdr_zero;
334 *p++ = xdr_one;
335 *p++ = xdr_one;
336 memcpy(p, ".\0\0\0", 4);
337 p++;
338 *p++ = xdr_one;
339 *p++ = htonl(attrs);
340 *p++ = htonl(12);
341 *p++ = htonl(NF4DIR);
342 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
343 }
344
345 *p++ = xdr_one;
346 *p++ = xdr_zero;
347 *p++ = xdr_two;
348 *p++ = xdr_two;
349 memcpy(p, "..\0\0", 4);
350 p++;
351 *p++ = xdr_one;
352 *p++ = htonl(attrs);
353 *p++ = htonl(12);
354 *p++ = htonl(NF4DIR);
355 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
356
357 readdir->pgbase = (char *)p - (char *)start;
358 readdir->count -= readdir->pgbase;
359 kunmap_atomic(start);
360 }
361
362 static void nfs4_test_and_free_stateid(struct nfs_server *server,
363 nfs4_stateid *stateid,
364 const struct cred *cred)
365 {
366 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
367
368 ops->test_and_free_expired(server, stateid, cred);
369 }
370
371 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
372 nfs4_stateid *stateid,
373 const struct cred *cred)
374 {
375 stateid->type = NFS4_REVOKED_STATEID_TYPE;
376 nfs4_test_and_free_stateid(server, stateid, cred);
377 }
378
379 static void nfs4_free_revoked_stateid(struct nfs_server *server,
380 const nfs4_stateid *stateid,
381 const struct cred *cred)
382 {
383 nfs4_stateid tmp;
384
385 nfs4_stateid_copy(&tmp, stateid);
386 __nfs4_free_revoked_stateid(server, &tmp, cred);
387 }
388
389 static long nfs4_update_delay(long *timeout)
390 {
391 long ret;
392 if (!timeout)
393 return NFS4_POLL_RETRY_MAX;
394 if (*timeout <= 0)
395 *timeout = NFS4_POLL_RETRY_MIN;
396 if (*timeout > NFS4_POLL_RETRY_MAX)
397 *timeout = NFS4_POLL_RETRY_MAX;
398 ret = *timeout;
399 *timeout <<= 1;
400 return ret;
401 }
402
403 static int nfs4_delay_killable(long *timeout)
404 {
405 might_sleep();
406
407 freezable_schedule_timeout_killable_unsafe(
408 nfs4_update_delay(timeout));
409 if (!__fatal_signal_pending(current))
410 return 0;
411 return -EINTR;
412 }
413
414 static int nfs4_delay_interruptible(long *timeout)
415 {
416 might_sleep();
417
418 freezable_schedule_timeout_interruptible(nfs4_update_delay(timeout));
419 if (!signal_pending(current))
420 return 0;
421 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
422 }
423
424 static int nfs4_delay(long *timeout, bool interruptible)
425 {
426 if (interruptible)
427 return nfs4_delay_interruptible(timeout);
428 return nfs4_delay_killable(timeout);
429 }
430
431 static const nfs4_stateid *
432 nfs4_recoverable_stateid(const nfs4_stateid *stateid)
433 {
434 if (!stateid)
435 return NULL;
436 switch (stateid->type) {
437 case NFS4_OPEN_STATEID_TYPE:
438 case NFS4_LOCK_STATEID_TYPE:
439 case NFS4_DELEGATION_STATEID_TYPE:
440 return stateid;
441 default:
442 break;
443 }
444 return NULL;
445 }
446
447
448
449
450 static int nfs4_do_handle_exception(struct nfs_server *server,
451 int errorcode, struct nfs4_exception *exception)
452 {
453 struct nfs_client *clp = server->nfs_client;
454 struct nfs4_state *state = exception->state;
455 const nfs4_stateid *stateid;
456 struct inode *inode = exception->inode;
457 int ret = errorcode;
458
459 exception->delay = 0;
460 exception->recovering = 0;
461 exception->retry = 0;
462
463 stateid = nfs4_recoverable_stateid(exception->stateid);
464 if (stateid == NULL && state != NULL)
465 stateid = nfs4_recoverable_stateid(&state->stateid);
466
467 switch(errorcode) {
468 case 0:
469 return 0;
470 case -NFS4ERR_BADHANDLE:
471 case -ESTALE:
472 if (inode != NULL && S_ISREG(inode->i_mode))
473 pnfs_destroy_layout(NFS_I(inode));
474 break;
475 case -NFS4ERR_DELEG_REVOKED:
476 case -NFS4ERR_ADMIN_REVOKED:
477 case -NFS4ERR_EXPIRED:
478 case -NFS4ERR_BAD_STATEID:
479 if (inode != NULL && stateid != NULL) {
480 nfs_inode_find_state_and_recover(inode,
481 stateid);
482 goto wait_on_recovery;
483 }
484
485 case -NFS4ERR_OPENMODE:
486 if (inode) {
487 int err;
488
489 err = nfs_async_inode_return_delegation(inode,
490 stateid);
491 if (err == 0)
492 goto wait_on_recovery;
493 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
494 exception->retry = 1;
495 break;
496 }
497 }
498 if (state == NULL)
499 break;
500 ret = nfs4_schedule_stateid_recovery(server, state);
501 if (ret < 0)
502 break;
503 goto wait_on_recovery;
504 case -NFS4ERR_STALE_STATEID:
505 case -NFS4ERR_STALE_CLIENTID:
506 nfs4_schedule_lease_recovery(clp);
507 goto wait_on_recovery;
508 case -NFS4ERR_MOVED:
509 ret = nfs4_schedule_migration_recovery(server);
510 if (ret < 0)
511 break;
512 goto wait_on_recovery;
513 case -NFS4ERR_LEASE_MOVED:
514 nfs4_schedule_lease_moved_recovery(clp);
515 goto wait_on_recovery;
516 #if defined(CONFIG_NFS_V4_1)
517 case -NFS4ERR_BADSESSION:
518 case -NFS4ERR_BADSLOT:
519 case -NFS4ERR_BAD_HIGH_SLOT:
520 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
521 case -NFS4ERR_DEADSESSION:
522 case -NFS4ERR_SEQ_FALSE_RETRY:
523 case -NFS4ERR_SEQ_MISORDERED:
524
525 goto wait_on_recovery;
526 #endif
527 case -NFS4ERR_FILE_OPEN:
528 if (exception->timeout > HZ) {
529
530
531
532 ret = -EBUSY;
533 break;
534 }
535
536 case -NFS4ERR_DELAY:
537 nfs_inc_server_stats(server, NFSIOS_DELAY);
538
539 case -NFS4ERR_GRACE:
540 case -NFS4ERR_LAYOUTTRYLATER:
541 case -NFS4ERR_RECALLCONFLICT:
542 exception->delay = 1;
543 return 0;
544
545 case -NFS4ERR_RETRY_UNCACHED_REP:
546 case -NFS4ERR_OLD_STATEID:
547 exception->retry = 1;
548 break;
549 case -NFS4ERR_BADOWNER:
550
551 case -NFS4ERR_BADNAME:
552 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
553 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
554 exception->retry = 1;
555 printk(KERN_WARNING "NFS: v4 server %s "
556 "does not accept raw "
557 "uid/gids. "
558 "Reenabling the idmapper.\n",
559 server->nfs_client->cl_hostname);
560 }
561 }
562
563 return nfs4_map_errors(ret);
564 wait_on_recovery:
565 exception->recovering = 1;
566 return 0;
567 }
568
569
570
571
572 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
573 {
574 struct nfs_client *clp = server->nfs_client;
575 int ret;
576
577 ret = nfs4_do_handle_exception(server, errorcode, exception);
578 if (exception->delay) {
579 ret = nfs4_delay(&exception->timeout,
580 exception->interruptible);
581 goto out_retry;
582 }
583 if (exception->recovering) {
584 ret = nfs4_wait_clnt_recover(clp);
585 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
586 return -EIO;
587 goto out_retry;
588 }
589 return ret;
590 out_retry:
591 if (ret == 0)
592 exception->retry = 1;
593 return ret;
594 }
595
596 static int
597 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
598 int errorcode, struct nfs4_exception *exception)
599 {
600 struct nfs_client *clp = server->nfs_client;
601 int ret;
602
603 ret = nfs4_do_handle_exception(server, errorcode, exception);
604 if (exception->delay) {
605 rpc_delay(task, nfs4_update_delay(&exception->timeout));
606 goto out_retry;
607 }
608 if (exception->recovering) {
609 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
610 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
611 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
612 goto out_retry;
613 }
614 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
615 ret = -EIO;
616 return ret;
617 out_retry:
618 if (ret == 0) {
619 exception->retry = 1;
620
621
622
623
624 if (errorcode == -NFS4ERR_MOVED)
625 rpc_task_release_transport(task);
626 }
627 return ret;
628 }
629
630 int
631 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
632 struct nfs4_state *state, long *timeout)
633 {
634 struct nfs4_exception exception = {
635 .state = state,
636 };
637
638 if (task->tk_status >= 0)
639 return 0;
640 if (timeout)
641 exception.timeout = *timeout;
642 task->tk_status = nfs4_async_handle_exception(task, server,
643 task->tk_status,
644 &exception);
645 if (exception.delay && timeout)
646 *timeout = exception.timeout;
647 if (exception.retry)
648 return -EAGAIN;
649 return 0;
650 }
651
652
653
654
655
656 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
657 {
658 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
659 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
660 }
661
662 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
663 {
664 spin_lock(&clp->cl_lock);
665 if (time_before(clp->cl_last_renewal,timestamp))
666 clp->cl_last_renewal = timestamp;
667 spin_unlock(&clp->cl_lock);
668 }
669
670 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
671 {
672 struct nfs_client *clp = server->nfs_client;
673
674 if (!nfs4_has_session(clp))
675 do_renew_lease(clp, timestamp);
676 }
677
678 struct nfs4_call_sync_data {
679 const struct nfs_server *seq_server;
680 struct nfs4_sequence_args *seq_args;
681 struct nfs4_sequence_res *seq_res;
682 };
683
684 void nfs4_init_sequence(struct nfs4_sequence_args *args,
685 struct nfs4_sequence_res *res, int cache_reply,
686 int privileged)
687 {
688 args->sa_slot = NULL;
689 args->sa_cache_this = cache_reply;
690 args->sa_privileged = privileged;
691
692 res->sr_slot = NULL;
693 }
694
695 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
696 {
697 struct nfs4_slot *slot = res->sr_slot;
698 struct nfs4_slot_table *tbl;
699
700 tbl = slot->table;
701 spin_lock(&tbl->slot_tbl_lock);
702 if (!nfs41_wake_and_assign_slot(tbl, slot))
703 nfs4_free_slot(tbl, slot);
704 spin_unlock(&tbl->slot_tbl_lock);
705
706 res->sr_slot = NULL;
707 }
708
709 static int nfs40_sequence_done(struct rpc_task *task,
710 struct nfs4_sequence_res *res)
711 {
712 if (res->sr_slot != NULL)
713 nfs40_sequence_free_slot(res);
714 return 1;
715 }
716
717 #if defined(CONFIG_NFS_V4_1)
718
719 static void nfs41_release_slot(struct nfs4_slot *slot)
720 {
721 struct nfs4_session *session;
722 struct nfs4_slot_table *tbl;
723 bool send_new_highest_used_slotid = false;
724
725 if (!slot)
726 return;
727 tbl = slot->table;
728 session = tbl->session;
729
730
731 if (slot->seq_done)
732 slot->seq_nr++;
733 slot->seq_done = 0;
734
735 spin_lock(&tbl->slot_tbl_lock);
736
737
738
739 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
740 send_new_highest_used_slotid = true;
741
742 if (nfs41_wake_and_assign_slot(tbl, slot)) {
743 send_new_highest_used_slotid = false;
744 goto out_unlock;
745 }
746 nfs4_free_slot(tbl, slot);
747
748 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
749 send_new_highest_used_slotid = false;
750 out_unlock:
751 spin_unlock(&tbl->slot_tbl_lock);
752 if (send_new_highest_used_slotid)
753 nfs41_notify_server(session->clp);
754 if (waitqueue_active(&tbl->slot_waitq))
755 wake_up_all(&tbl->slot_waitq);
756 }
757
758 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
759 {
760 nfs41_release_slot(res->sr_slot);
761 res->sr_slot = NULL;
762 }
763
764 static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
765 u32 seqnr)
766 {
767 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
768 slot->seq_nr_highest_sent = seqnr;
769 }
770 static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
771 u32 seqnr)
772 {
773 slot->seq_nr_highest_sent = seqnr;
774 slot->seq_nr_last_acked = seqnr;
775 }
776
777 static int nfs41_sequence_process(struct rpc_task *task,
778 struct nfs4_sequence_res *res)
779 {
780 struct nfs4_session *session;
781 struct nfs4_slot *slot = res->sr_slot;
782 struct nfs_client *clp;
783 int status;
784 int ret = 1;
785
786 if (slot == NULL)
787 goto out_noaction;
788
789 if (!RPC_WAS_SENT(task) || slot->seq_done)
790 goto out;
791
792 session = slot->table->session;
793
794 trace_nfs4_sequence_done(session, res);
795
796 status = res->sr_status;
797 if (task->tk_status == -NFS4ERR_DEADSESSION)
798 status = -NFS4ERR_DEADSESSION;
799
800
801 switch (status) {
802 case 0:
803
804 nfs4_slot_sequence_acked(slot, slot->seq_nr);
805
806 slot->seq_done = 1;
807 clp = session->clp;
808 do_renew_lease(clp, res->sr_timestamp);
809
810 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
811 !!slot->privileged);
812 nfs41_update_target_slotid(slot->table, slot, res);
813 break;
814 case 1:
815
816
817
818
819
820 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
821 slot->seq_done = 1;
822 goto out;
823 case -NFS4ERR_DELAY:
824
825
826
827
828 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
829 __func__,
830 slot->slot_nr,
831 slot->seq_nr);
832 nfs4_slot_sequence_acked(slot, slot->seq_nr);
833 goto out_retry;
834 case -NFS4ERR_RETRY_UNCACHED_REP:
835 case -NFS4ERR_SEQ_FALSE_RETRY:
836
837
838
839
840 nfs4_slot_sequence_acked(slot, slot->seq_nr);
841 goto retry_new_seq;
842 case -NFS4ERR_BADSLOT:
843
844
845
846
847 if (slot->slot_nr < slot->table->target_highest_slotid)
848 goto session_recover;
849 goto retry_nowait;
850 case -NFS4ERR_SEQ_MISORDERED:
851 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
852
853
854
855
856
857 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
858 slot->seq_nr--;
859 goto retry_nowait;
860 }
861
862
863
864
865
866
867
868
869
870
871 slot->seq_nr = slot->seq_nr_highest_sent;
872 goto out_retry;
873 case -NFS4ERR_BADSESSION:
874 case -NFS4ERR_DEADSESSION:
875 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
876 goto session_recover;
877 default:
878
879 slot->seq_done = 1;
880 }
881 out:
882
883 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
884 out_noaction:
885 return ret;
886 session_recover:
887 nfs4_schedule_session_recovery(session, status);
888 dprintk("%s ERROR: %d Reset session\n", __func__, status);
889 nfs41_sequence_free_slot(res);
890 goto out;
891 retry_new_seq:
892 ++slot->seq_nr;
893 retry_nowait:
894 if (rpc_restart_call_prepare(task)) {
895 nfs41_sequence_free_slot(res);
896 task->tk_status = 0;
897 ret = 0;
898 }
899 goto out;
900 out_retry:
901 if (!rpc_restart_call(task))
902 goto out;
903 rpc_delay(task, NFS4_POLL_RETRY_MAX);
904 return 0;
905 }
906
907 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
908 {
909 if (!nfs41_sequence_process(task, res))
910 return 0;
911 if (res->sr_slot != NULL)
912 nfs41_sequence_free_slot(res);
913 return 1;
914
915 }
916 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
917
918 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
919 {
920 if (res->sr_slot == NULL)
921 return 1;
922 if (res->sr_slot->table->session != NULL)
923 return nfs41_sequence_process(task, res);
924 return nfs40_sequence_done(task, res);
925 }
926
927 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
928 {
929 if (res->sr_slot != NULL) {
930 if (res->sr_slot->table->session != NULL)
931 nfs41_sequence_free_slot(res);
932 else
933 nfs40_sequence_free_slot(res);
934 }
935 }
936
937 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
938 {
939 if (res->sr_slot == NULL)
940 return 1;
941 if (!res->sr_slot->table->session)
942 return nfs40_sequence_done(task, res);
943 return nfs41_sequence_done(task, res);
944 }
945 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
946
947 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
948 {
949 struct nfs4_call_sync_data *data = calldata;
950
951 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
952
953 nfs4_setup_sequence(data->seq_server->nfs_client,
954 data->seq_args, data->seq_res, task);
955 }
956
957 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
958 {
959 struct nfs4_call_sync_data *data = calldata;
960
961 nfs41_sequence_done(task, data->seq_res);
962 }
963
964 static const struct rpc_call_ops nfs41_call_sync_ops = {
965 .rpc_call_prepare = nfs41_call_sync_prepare,
966 .rpc_call_done = nfs41_call_sync_done,
967 };
968
969 #else
970
971 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
972 {
973 return nfs40_sequence_done(task, res);
974 }
975
976 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
977 {
978 if (res->sr_slot != NULL)
979 nfs40_sequence_free_slot(res);
980 }
981
982 int nfs4_sequence_done(struct rpc_task *task,
983 struct nfs4_sequence_res *res)
984 {
985 return nfs40_sequence_done(task, res);
986 }
987 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
988
989 #endif
990
991 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
992 {
993 res->sr_timestamp = jiffies;
994 res->sr_status_flags = 0;
995 res->sr_status = 1;
996 }
997
998 static
999 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1000 struct nfs4_sequence_res *res,
1001 struct nfs4_slot *slot)
1002 {
1003 if (!slot)
1004 return;
1005 slot->privileged = args->sa_privileged ? 1 : 0;
1006 args->sa_slot = slot;
1007
1008 res->sr_slot = slot;
1009 }
1010
1011 int nfs4_setup_sequence(struct nfs_client *client,
1012 struct nfs4_sequence_args *args,
1013 struct nfs4_sequence_res *res,
1014 struct rpc_task *task)
1015 {
1016 struct nfs4_session *session = nfs4_get_session(client);
1017 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
1018 struct nfs4_slot *slot;
1019
1020
1021 if (res->sr_slot != NULL)
1022 goto out_start;
1023
1024 if (session)
1025 tbl = &session->fc_slot_table;
1026
1027 spin_lock(&tbl->slot_tbl_lock);
1028
1029 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1030 goto out_sleep;
1031
1032 slot = nfs4_alloc_slot(tbl);
1033 if (IS_ERR(slot)) {
1034 if (slot == ERR_PTR(-ENOMEM))
1035 goto out_sleep_timeout;
1036 goto out_sleep;
1037 }
1038 spin_unlock(&tbl->slot_tbl_lock);
1039
1040 nfs4_sequence_attach_slot(args, res, slot);
1041
1042 trace_nfs4_setup_sequence(session, args);
1043 out_start:
1044 nfs41_sequence_res_init(res);
1045 rpc_call_start(task);
1046 return 0;
1047 out_sleep_timeout:
1048
1049 if (args->sa_privileged)
1050 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1051 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1052 else
1053 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1054 NULL, jiffies + (HZ >> 2));
1055 spin_unlock(&tbl->slot_tbl_lock);
1056 return -EAGAIN;
1057 out_sleep:
1058 if (args->sa_privileged)
1059 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1060 RPC_PRIORITY_PRIVILEGED);
1061 else
1062 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1063 spin_unlock(&tbl->slot_tbl_lock);
1064 return -EAGAIN;
1065 }
1066 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1067
1068 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1069 {
1070 struct nfs4_call_sync_data *data = calldata;
1071 nfs4_setup_sequence(data->seq_server->nfs_client,
1072 data->seq_args, data->seq_res, task);
1073 }
1074
1075 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1076 {
1077 struct nfs4_call_sync_data *data = calldata;
1078 nfs4_sequence_done(task, data->seq_res);
1079 }
1080
1081 static const struct rpc_call_ops nfs40_call_sync_ops = {
1082 .rpc_call_prepare = nfs40_call_sync_prepare,
1083 .rpc_call_done = nfs40_call_sync_done,
1084 };
1085
1086 static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1087 {
1088 int ret;
1089 struct rpc_task *task;
1090
1091 task = rpc_run_task(task_setup);
1092 if (IS_ERR(task))
1093 return PTR_ERR(task);
1094
1095 ret = task->tk_status;
1096 rpc_put_task(task);
1097 return ret;
1098 }
1099
1100 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1101 struct nfs_server *server,
1102 struct rpc_message *msg,
1103 struct nfs4_sequence_args *args,
1104 struct nfs4_sequence_res *res)
1105 {
1106 struct nfs_client *clp = server->nfs_client;
1107 struct nfs4_call_sync_data data = {
1108 .seq_server = server,
1109 .seq_args = args,
1110 .seq_res = res,
1111 };
1112 struct rpc_task_setup task_setup = {
1113 .rpc_client = clnt,
1114 .rpc_message = msg,
1115 .callback_ops = clp->cl_mvops->call_sync_ops,
1116 .callback_data = &data
1117 };
1118
1119 return nfs4_call_sync_custom(&task_setup);
1120 }
1121
1122 int nfs4_call_sync(struct rpc_clnt *clnt,
1123 struct nfs_server *server,
1124 struct rpc_message *msg,
1125 struct nfs4_sequence_args *args,
1126 struct nfs4_sequence_res *res,
1127 int cache_reply)
1128 {
1129 nfs4_init_sequence(args, res, cache_reply, 0);
1130 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1131 }
1132
1133 static void
1134 nfs4_inc_nlink_locked(struct inode *inode)
1135 {
1136 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1137 inc_nlink(inode);
1138 }
1139
1140 static void
1141 nfs4_dec_nlink_locked(struct inode *inode)
1142 {
1143 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1144 drop_nlink(inode);
1145 }
1146
1147 static void
1148 update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
1149 unsigned long timestamp, unsigned long cache_validity)
1150 {
1151 struct nfs_inode *nfsi = NFS_I(dir);
1152
1153 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1154 | NFS_INO_INVALID_MTIME
1155 | NFS_INO_INVALID_DATA
1156 | cache_validity;
1157 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
1158 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1159 nfsi->attrtimeo_timestamp = jiffies;
1160 } else {
1161 nfs_force_lookup_revalidate(dir);
1162 if (cinfo->before != inode_peek_iversion_raw(dir))
1163 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1164 NFS_INO_INVALID_ACL;
1165 }
1166 inode_set_iversion_raw(dir, cinfo->after);
1167 nfsi->read_cache_jiffies = timestamp;
1168 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1169 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1170 nfs_fscache_invalidate(dir);
1171 }
1172
1173 static void
1174 update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1175 unsigned long timestamp, unsigned long cache_validity)
1176 {
1177 spin_lock(&dir->i_lock);
1178 update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1179 spin_unlock(&dir->i_lock);
1180 }
1181
1182 struct nfs4_open_createattrs {
1183 struct nfs4_label *label;
1184 struct iattr *sattr;
1185 const __u32 verf[2];
1186 };
1187
1188 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1189 int err, struct nfs4_exception *exception)
1190 {
1191 if (err != -EINVAL)
1192 return false;
1193 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1194 return false;
1195 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1196 exception->retry = 1;
1197 return true;
1198 }
1199
1200 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1201 {
1202 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1203 }
1204
1205 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1206 {
1207 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1208
1209 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1210 }
1211
1212 static u32
1213 nfs4_map_atomic_open_share(struct nfs_server *server,
1214 fmode_t fmode, int openflags)
1215 {
1216 u32 res = 0;
1217
1218 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1219 case FMODE_READ:
1220 res = NFS4_SHARE_ACCESS_READ;
1221 break;
1222 case FMODE_WRITE:
1223 res = NFS4_SHARE_ACCESS_WRITE;
1224 break;
1225 case FMODE_READ|FMODE_WRITE:
1226 res = NFS4_SHARE_ACCESS_BOTH;
1227 }
1228 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1229 goto out;
1230
1231 if (openflags & O_DIRECT)
1232 res |= NFS4_SHARE_WANT_NO_DELEG;
1233 out:
1234 return res;
1235 }
1236
1237 static enum open_claim_type4
1238 nfs4_map_atomic_open_claim(struct nfs_server *server,
1239 enum open_claim_type4 claim)
1240 {
1241 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1242 return claim;
1243 switch (claim) {
1244 default:
1245 return claim;
1246 case NFS4_OPEN_CLAIM_FH:
1247 return NFS4_OPEN_CLAIM_NULL;
1248 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1249 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1250 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1251 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1252 }
1253 }
1254
1255 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1256 {
1257 p->o_res.f_attr = &p->f_attr;
1258 p->o_res.f_label = p->f_label;
1259 p->o_res.seqid = p->o_arg.seqid;
1260 p->c_res.seqid = p->c_arg.seqid;
1261 p->o_res.server = p->o_arg.server;
1262 p->o_res.access_request = p->o_arg.access;
1263 nfs_fattr_init(&p->f_attr);
1264 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1265 }
1266
1267 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1268 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1269 const struct nfs4_open_createattrs *c,
1270 enum open_claim_type4 claim,
1271 gfp_t gfp_mask)
1272 {
1273 struct dentry *parent = dget_parent(dentry);
1274 struct inode *dir = d_inode(parent);
1275 struct nfs_server *server = NFS_SERVER(dir);
1276 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1277 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1278 struct nfs4_opendata *p;
1279
1280 p = kzalloc(sizeof(*p), gfp_mask);
1281 if (p == NULL)
1282 goto err;
1283
1284 p->f_label = nfs4_label_alloc(server, gfp_mask);
1285 if (IS_ERR(p->f_label))
1286 goto err_free_p;
1287
1288 p->a_label = nfs4_label_alloc(server, gfp_mask);
1289 if (IS_ERR(p->a_label))
1290 goto err_free_f;
1291
1292 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1293 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1294 if (IS_ERR(p->o_arg.seqid))
1295 goto err_free_label;
1296 nfs_sb_active(dentry->d_sb);
1297 p->dentry = dget(dentry);
1298 p->dir = parent;
1299 p->owner = sp;
1300 atomic_inc(&sp->so_count);
1301 p->o_arg.open_flags = flags;
1302 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1303 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1304 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1305 fmode, flags);
1306 if (flags & O_CREAT) {
1307 p->o_arg.umask = current_umask();
1308 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1309 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1310 p->o_arg.u.attrs = &p->attrs;
1311 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1312
1313 memcpy(p->o_arg.u.verifier.data, c->verf,
1314 sizeof(p->o_arg.u.verifier.data));
1315 }
1316 }
1317
1318
1319 if (!(flags & O_EXCL)) {
1320
1321
1322 switch (p->o_arg.claim) {
1323 default:
1324 break;
1325 case NFS4_OPEN_CLAIM_NULL:
1326 case NFS4_OPEN_CLAIM_FH:
1327 p->o_arg.access = NFS4_ACCESS_READ |
1328 NFS4_ACCESS_MODIFY |
1329 NFS4_ACCESS_EXTEND |
1330 NFS4_ACCESS_EXECUTE;
1331 }
1332 }
1333 p->o_arg.clientid = server->nfs_client->cl_clientid;
1334 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1335 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1336 p->o_arg.name = &dentry->d_name;
1337 p->o_arg.server = server;
1338 p->o_arg.bitmask = nfs4_bitmask(server, label);
1339 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1340 switch (p->o_arg.claim) {
1341 case NFS4_OPEN_CLAIM_NULL:
1342 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1343 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1344 p->o_arg.fh = NFS_FH(dir);
1345 break;
1346 case NFS4_OPEN_CLAIM_PREVIOUS:
1347 case NFS4_OPEN_CLAIM_FH:
1348 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1349 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1350 p->o_arg.fh = NFS_FH(d_inode(dentry));
1351 }
1352 p->c_arg.fh = &p->o_res.fh;
1353 p->c_arg.stateid = &p->o_res.stateid;
1354 p->c_arg.seqid = p->o_arg.seqid;
1355 nfs4_init_opendata_res(p);
1356 kref_init(&p->kref);
1357 return p;
1358
1359 err_free_label:
1360 nfs4_label_free(p->a_label);
1361 err_free_f:
1362 nfs4_label_free(p->f_label);
1363 err_free_p:
1364 kfree(p);
1365 err:
1366 dput(parent);
1367 return NULL;
1368 }
1369
1370 static void nfs4_opendata_free(struct kref *kref)
1371 {
1372 struct nfs4_opendata *p = container_of(kref,
1373 struct nfs4_opendata, kref);
1374 struct super_block *sb = p->dentry->d_sb;
1375
1376 nfs4_lgopen_release(p->lgp);
1377 nfs_free_seqid(p->o_arg.seqid);
1378 nfs4_sequence_free_slot(&p->o_res.seq_res);
1379 if (p->state != NULL)
1380 nfs4_put_open_state(p->state);
1381 nfs4_put_state_owner(p->owner);
1382
1383 nfs4_label_free(p->a_label);
1384 nfs4_label_free(p->f_label);
1385
1386 dput(p->dir);
1387 dput(p->dentry);
1388 nfs_sb_deactive(sb);
1389 nfs_fattr_free_names(&p->f_attr);
1390 kfree(p->f_attr.mdsthreshold);
1391 kfree(p);
1392 }
1393
1394 static void nfs4_opendata_put(struct nfs4_opendata *p)
1395 {
1396 if (p != NULL)
1397 kref_put(&p->kref, nfs4_opendata_free);
1398 }
1399
1400 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1401 fmode_t fmode)
1402 {
1403 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1404 case FMODE_READ|FMODE_WRITE:
1405 return state->n_rdwr != 0;
1406 case FMODE_WRITE:
1407 return state->n_wronly != 0;
1408 case FMODE_READ:
1409 return state->n_rdonly != 0;
1410 }
1411 WARN_ON_ONCE(1);
1412 return false;
1413 }
1414
1415 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1416 int open_mode, enum open_claim_type4 claim)
1417 {
1418 int ret = 0;
1419
1420 if (open_mode & (O_EXCL|O_TRUNC))
1421 goto out;
1422 switch (claim) {
1423 case NFS4_OPEN_CLAIM_NULL:
1424 case NFS4_OPEN_CLAIM_FH:
1425 goto out;
1426 default:
1427 break;
1428 }
1429 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1430 case FMODE_READ:
1431 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1432 && state->n_rdonly != 0;
1433 break;
1434 case FMODE_WRITE:
1435 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1436 && state->n_wronly != 0;
1437 break;
1438 case FMODE_READ|FMODE_WRITE:
1439 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1440 && state->n_rdwr != 0;
1441 }
1442 out:
1443 return ret;
1444 }
1445
1446 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1447 enum open_claim_type4 claim)
1448 {
1449 if (delegation == NULL)
1450 return 0;
1451 if ((delegation->type & fmode) != fmode)
1452 return 0;
1453 switch (claim) {
1454 case NFS4_OPEN_CLAIM_NULL:
1455 case NFS4_OPEN_CLAIM_FH:
1456 break;
1457 case NFS4_OPEN_CLAIM_PREVIOUS:
1458 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1459 break;
1460
1461 default:
1462 return 0;
1463 }
1464 nfs_mark_delegation_referenced(delegation);
1465 return 1;
1466 }
1467
1468 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1469 {
1470 switch (fmode) {
1471 case FMODE_WRITE:
1472 state->n_wronly++;
1473 break;
1474 case FMODE_READ:
1475 state->n_rdonly++;
1476 break;
1477 case FMODE_READ|FMODE_WRITE:
1478 state->n_rdwr++;
1479 }
1480 nfs4_state_set_mode_locked(state, state->state | fmode);
1481 }
1482
1483 #ifdef CONFIG_NFS_V4_1
1484 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1485 {
1486 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1487 return true;
1488 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1489 return true;
1490 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1491 return true;
1492 return false;
1493 }
1494 #endif
1495
1496 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1497 {
1498 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1499 wake_up_all(&state->waitq);
1500 }
1501
1502 static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1503 const nfs4_stateid *stateid)
1504 {
1505 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1506 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1507
1508 if (stateid_seqid == state_seqid + 1U ||
1509 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1510 nfs_state_log_update_open_stateid(state);
1511 else
1512 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1513 }
1514
1515 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1516 {
1517 struct nfs_client *clp = state->owner->so_server->nfs_client;
1518 bool need_recover = false;
1519
1520 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1521 need_recover = true;
1522 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1523 need_recover = true;
1524 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1525 need_recover = true;
1526 if (need_recover)
1527 nfs4_state_mark_reclaim_nograce(clp, state);
1528 }
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1541 const nfs4_stateid *stateid)
1542 {
1543 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1544 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1545 if (stateid->seqid == cpu_to_be32(1))
1546 nfs_state_log_update_open_stateid(state);
1547 else
1548 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1549 return true;
1550 }
1551
1552 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1553 nfs_state_log_out_of_order_open_stateid(state, stateid);
1554 return true;
1555 }
1556 return false;
1557 }
1558
1559 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1560 {
1561 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1562 return;
1563 if (state->n_wronly)
1564 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1565 if (state->n_rdonly)
1566 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1567 if (state->n_rdwr)
1568 set_bit(NFS_O_RDWR_STATE, &state->flags);
1569 set_bit(NFS_OPEN_STATE, &state->flags);
1570 }
1571
1572 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1573 nfs4_stateid *stateid, fmode_t fmode)
1574 {
1575 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1576 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1577 case FMODE_WRITE:
1578 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1579 break;
1580 case FMODE_READ:
1581 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1582 break;
1583 case 0:
1584 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1585 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1586 clear_bit(NFS_OPEN_STATE, &state->flags);
1587 }
1588 if (stateid == NULL)
1589 return;
1590
1591 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1592 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1593 nfs_resync_open_stateid_locked(state);
1594 goto out;
1595 }
1596 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1597 nfs4_stateid_copy(&state->stateid, stateid);
1598 nfs4_stateid_copy(&state->open_stateid, stateid);
1599 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1600 out:
1601 nfs_state_log_update_open_stateid(state);
1602 }
1603
1604 static void nfs_clear_open_stateid(struct nfs4_state *state,
1605 nfs4_stateid *arg_stateid,
1606 nfs4_stateid *stateid, fmode_t fmode)
1607 {
1608 write_seqlock(&state->seqlock);
1609
1610 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1611 nfs_clear_open_stateid_locked(state, stateid, fmode);
1612 write_sequnlock(&state->seqlock);
1613 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1614 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1615 }
1616
1617 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1618 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1619 __must_hold(&state->owner->so_lock)
1620 __must_hold(&state->seqlock)
1621 __must_hold(RCU)
1622
1623 {
1624 DEFINE_WAIT(wait);
1625 int status = 0;
1626 for (;;) {
1627
1628 if (!nfs_need_update_open_stateid(state, stateid))
1629 return;
1630 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1631 break;
1632 if (status)
1633 break;
1634
1635 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1636 break;
1637
1638 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1639
1640
1641
1642
1643
1644 write_sequnlock(&state->seqlock);
1645 spin_unlock(&state->owner->so_lock);
1646 rcu_read_unlock();
1647 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1648 if (!signal_pending(current)) {
1649 if (schedule_timeout(5*HZ) == 0)
1650 status = -EAGAIN;
1651 else
1652 status = 0;
1653 } else
1654 status = -EINTR;
1655 finish_wait(&state->waitq, &wait);
1656 rcu_read_lock();
1657 spin_lock(&state->owner->so_lock);
1658 write_seqlock(&state->seqlock);
1659 }
1660
1661 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1662 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1663 nfs4_stateid_copy(freeme, &state->open_stateid);
1664 nfs_test_and_clear_all_open_stateid(state);
1665 }
1666
1667 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1668 nfs4_stateid_copy(&state->stateid, stateid);
1669 nfs4_stateid_copy(&state->open_stateid, stateid);
1670 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1671 nfs_state_log_update_open_stateid(state);
1672 }
1673
1674 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1675 const nfs4_stateid *open_stateid,
1676 fmode_t fmode,
1677 nfs4_stateid *freeme)
1678 {
1679
1680
1681
1682
1683 write_seqlock(&state->seqlock);
1684 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1685 switch (fmode) {
1686 case FMODE_READ:
1687 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1688 break;
1689 case FMODE_WRITE:
1690 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1691 break;
1692 case FMODE_READ|FMODE_WRITE:
1693 set_bit(NFS_O_RDWR_STATE, &state->flags);
1694 }
1695 set_bit(NFS_OPEN_STATE, &state->flags);
1696 write_sequnlock(&state->seqlock);
1697 }
1698
1699 static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1700 {
1701 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1702 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1703 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1704 clear_bit(NFS_OPEN_STATE, &state->flags);
1705 }
1706
1707 static void nfs_state_set_delegation(struct nfs4_state *state,
1708 const nfs4_stateid *deleg_stateid,
1709 fmode_t fmode)
1710 {
1711
1712
1713
1714
1715 write_seqlock(&state->seqlock);
1716 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1717 set_bit(NFS_DELEGATED_STATE, &state->flags);
1718 write_sequnlock(&state->seqlock);
1719 }
1720
1721 static void nfs_state_clear_delegation(struct nfs4_state *state)
1722 {
1723 write_seqlock(&state->seqlock);
1724 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1725 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1726 write_sequnlock(&state->seqlock);
1727 }
1728
1729 static int update_open_stateid(struct nfs4_state *state,
1730 const nfs4_stateid *open_stateid,
1731 const nfs4_stateid *delegation,
1732 fmode_t fmode)
1733 {
1734 struct nfs_server *server = NFS_SERVER(state->inode);
1735 struct nfs_client *clp = server->nfs_client;
1736 struct nfs_inode *nfsi = NFS_I(state->inode);
1737 struct nfs_delegation *deleg_cur;
1738 nfs4_stateid freeme = { };
1739 int ret = 0;
1740
1741 fmode &= (FMODE_READ|FMODE_WRITE);
1742
1743 rcu_read_lock();
1744 spin_lock(&state->owner->so_lock);
1745 if (open_stateid != NULL) {
1746 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1747 ret = 1;
1748 }
1749
1750 deleg_cur = rcu_dereference(nfsi->delegation);
1751 if (deleg_cur == NULL)
1752 goto no_delegation;
1753
1754 spin_lock(&deleg_cur->lock);
1755 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1756 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1757 (deleg_cur->type & fmode) != fmode)
1758 goto no_delegation_unlock;
1759
1760 if (delegation == NULL)
1761 delegation = &deleg_cur->stateid;
1762 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1763 goto no_delegation_unlock;
1764
1765 nfs_mark_delegation_referenced(deleg_cur);
1766 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1767 ret = 1;
1768 no_delegation_unlock:
1769 spin_unlock(&deleg_cur->lock);
1770 no_delegation:
1771 if (ret)
1772 update_open_stateflags(state, fmode);
1773 spin_unlock(&state->owner->so_lock);
1774 rcu_read_unlock();
1775
1776 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1777 nfs4_schedule_state_manager(clp);
1778 if (freeme.type != 0)
1779 nfs4_test_and_free_stateid(server, &freeme,
1780 state->owner->so_cred);
1781
1782 return ret;
1783 }
1784
1785 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1786 const nfs4_stateid *stateid)
1787 {
1788 struct nfs4_state *state = lsp->ls_state;
1789 bool ret = false;
1790
1791 spin_lock(&state->state_lock);
1792 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1793 goto out_noupdate;
1794 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1795 goto out_noupdate;
1796 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1797 ret = true;
1798 out_noupdate:
1799 spin_unlock(&state->state_lock);
1800 return ret;
1801 }
1802
1803 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1804 {
1805 struct nfs_delegation *delegation;
1806
1807 fmode &= FMODE_READ|FMODE_WRITE;
1808 rcu_read_lock();
1809 delegation = rcu_dereference(NFS_I(inode)->delegation);
1810 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1811 rcu_read_unlock();
1812 return;
1813 }
1814 rcu_read_unlock();
1815 nfs4_inode_return_delegation(inode);
1816 }
1817
1818 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1819 {
1820 struct nfs4_state *state = opendata->state;
1821 struct nfs_delegation *delegation;
1822 int open_mode = opendata->o_arg.open_flags;
1823 fmode_t fmode = opendata->o_arg.fmode;
1824 enum open_claim_type4 claim = opendata->o_arg.claim;
1825 nfs4_stateid stateid;
1826 int ret = -EAGAIN;
1827
1828 for (;;) {
1829 spin_lock(&state->owner->so_lock);
1830 if (can_open_cached(state, fmode, open_mode, claim)) {
1831 update_open_stateflags(state, fmode);
1832 spin_unlock(&state->owner->so_lock);
1833 goto out_return_state;
1834 }
1835 spin_unlock(&state->owner->so_lock);
1836 rcu_read_lock();
1837 delegation = nfs4_get_valid_delegation(state->inode);
1838 if (!can_open_delegated(delegation, fmode, claim)) {
1839 rcu_read_unlock();
1840 break;
1841 }
1842
1843 nfs4_stateid_copy(&stateid, &delegation->stateid);
1844 rcu_read_unlock();
1845 nfs_release_seqid(opendata->o_arg.seqid);
1846 if (!opendata->is_recover) {
1847 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1848 if (ret != 0)
1849 goto out;
1850 }
1851 ret = -EAGAIN;
1852
1853
1854 if (update_open_stateid(state, NULL, &stateid, fmode))
1855 goto out_return_state;
1856 }
1857 out:
1858 return ERR_PTR(ret);
1859 out_return_state:
1860 refcount_inc(&state->count);
1861 return state;
1862 }
1863
1864 static void
1865 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1866 {
1867 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1868 struct nfs_delegation *delegation;
1869 int delegation_flags = 0;
1870
1871 rcu_read_lock();
1872 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1873 if (delegation)
1874 delegation_flags = delegation->flags;
1875 rcu_read_unlock();
1876 switch (data->o_arg.claim) {
1877 default:
1878 break;
1879 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1880 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1881 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1882 "returning a delegation for "
1883 "OPEN(CLAIM_DELEGATE_CUR)\n",
1884 clp->cl_hostname);
1885 return;
1886 }
1887 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1888 nfs_inode_set_delegation(state->inode,
1889 data->owner->so_cred,
1890 data->o_res.delegation_type,
1891 &data->o_res.delegation,
1892 data->o_res.pagemod_limit);
1893 else
1894 nfs_inode_reclaim_delegation(state->inode,
1895 data->owner->so_cred,
1896 data->o_res.delegation_type,
1897 &data->o_res.delegation,
1898 data->o_res.pagemod_limit);
1899
1900 if (data->o_res.do_recall)
1901 nfs_async_inode_return_delegation(state->inode,
1902 &data->o_res.delegation);
1903 }
1904
1905
1906
1907
1908
1909 static struct nfs4_state *
1910 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1911 {
1912 struct inode *inode = data->state->inode;
1913 struct nfs4_state *state = data->state;
1914 int ret;
1915
1916 if (!data->rpc_done) {
1917 if (data->rpc_status)
1918 return ERR_PTR(data->rpc_status);
1919
1920 goto update;
1921 }
1922
1923 ret = nfs_refresh_inode(inode, &data->f_attr);
1924 if (ret)
1925 return ERR_PTR(ret);
1926
1927 if (data->o_res.delegation_type != 0)
1928 nfs4_opendata_check_deleg(data, state);
1929 update:
1930 if (!update_open_stateid(state, &data->o_res.stateid,
1931 NULL, data->o_arg.fmode))
1932 return ERR_PTR(-EAGAIN);
1933 refcount_inc(&state->count);
1934
1935 return state;
1936 }
1937
1938 static struct inode *
1939 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1940 {
1941 struct inode *inode;
1942
1943 switch (data->o_arg.claim) {
1944 case NFS4_OPEN_CLAIM_NULL:
1945 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1946 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1947 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1948 return ERR_PTR(-EAGAIN);
1949 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1950 &data->f_attr, data->f_label);
1951 break;
1952 default:
1953 inode = d_inode(data->dentry);
1954 ihold(inode);
1955 nfs_refresh_inode(inode, &data->f_attr);
1956 }
1957 return inode;
1958 }
1959
1960 static struct nfs4_state *
1961 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1962 {
1963 struct nfs4_state *state;
1964 struct inode *inode;
1965
1966 inode = nfs4_opendata_get_inode(data);
1967 if (IS_ERR(inode))
1968 return ERR_CAST(inode);
1969 if (data->state != NULL && data->state->inode == inode) {
1970 state = data->state;
1971 refcount_inc(&state->count);
1972 } else
1973 state = nfs4_get_open_state(inode, data->owner);
1974 iput(inode);
1975 if (state == NULL)
1976 state = ERR_PTR(-ENOMEM);
1977 return state;
1978 }
1979
1980 static struct nfs4_state *
1981 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1982 {
1983 struct nfs4_state *state;
1984
1985 if (!data->rpc_done) {
1986 state = nfs4_try_open_cached(data);
1987 trace_nfs4_cached_open(data->state);
1988 goto out;
1989 }
1990
1991 state = nfs4_opendata_find_nfs4_state(data);
1992 if (IS_ERR(state))
1993 goto out;
1994
1995 if (data->o_res.delegation_type != 0)
1996 nfs4_opendata_check_deleg(data, state);
1997 if (!update_open_stateid(state, &data->o_res.stateid,
1998 NULL, data->o_arg.fmode)) {
1999 nfs4_put_open_state(state);
2000 state = ERR_PTR(-EAGAIN);
2001 }
2002 out:
2003 nfs_release_seqid(data->o_arg.seqid);
2004 return state;
2005 }
2006
2007 static struct nfs4_state *
2008 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2009 {
2010 struct nfs4_state *ret;
2011
2012 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2013 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2014 else
2015 ret = _nfs4_opendata_to_nfs4_state(data);
2016 nfs4_sequence_free_slot(&data->o_res.seq_res);
2017 return ret;
2018 }
2019
2020 static struct nfs_open_context *
2021 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2022 {
2023 struct nfs_inode *nfsi = NFS_I(state->inode);
2024 struct nfs_open_context *ctx;
2025
2026 rcu_read_lock();
2027 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2028 if (ctx->state != state)
2029 continue;
2030 if ((ctx->mode & mode) != mode)
2031 continue;
2032 if (!get_nfs_open_context(ctx))
2033 continue;
2034 rcu_read_unlock();
2035 return ctx;
2036 }
2037 rcu_read_unlock();
2038 return ERR_PTR(-ENOENT);
2039 }
2040
2041 static struct nfs_open_context *
2042 nfs4_state_find_open_context(struct nfs4_state *state)
2043 {
2044 struct nfs_open_context *ctx;
2045
2046 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2047 if (!IS_ERR(ctx))
2048 return ctx;
2049 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2050 if (!IS_ERR(ctx))
2051 return ctx;
2052 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2053 }
2054
2055 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2056 struct nfs4_state *state, enum open_claim_type4 claim)
2057 {
2058 struct nfs4_opendata *opendata;
2059
2060 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2061 NULL, claim, GFP_NOFS);
2062 if (opendata == NULL)
2063 return ERR_PTR(-ENOMEM);
2064 opendata->state = state;
2065 refcount_inc(&state->count);
2066 return opendata;
2067 }
2068
2069 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2070 fmode_t fmode)
2071 {
2072 struct nfs4_state *newstate;
2073 int ret;
2074
2075 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2076 return 0;
2077 opendata->o_arg.open_flags = 0;
2078 opendata->o_arg.fmode = fmode;
2079 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2080 NFS_SB(opendata->dentry->d_sb),
2081 fmode, 0);
2082 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2083 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2084 nfs4_init_opendata_res(opendata);
2085 ret = _nfs4_recover_proc_open(opendata);
2086 if (ret != 0)
2087 return ret;
2088 newstate = nfs4_opendata_to_nfs4_state(opendata);
2089 if (IS_ERR(newstate))
2090 return PTR_ERR(newstate);
2091 if (newstate != opendata->state)
2092 ret = -ESTALE;
2093 nfs4_close_state(newstate, fmode);
2094 return ret;
2095 }
2096
2097 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2098 {
2099 int ret;
2100
2101
2102 smp_rmb();
2103 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2104 if (ret != 0)
2105 return ret;
2106 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2107 if (ret != 0)
2108 return ret;
2109 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2110 if (ret != 0)
2111 return ret;
2112
2113
2114
2115
2116 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2117 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2118 write_seqlock(&state->seqlock);
2119 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2120 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2121 write_sequnlock(&state->seqlock);
2122 }
2123 return 0;
2124 }
2125
2126
2127
2128
2129
2130 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2131 {
2132 struct nfs_delegation *delegation;
2133 struct nfs4_opendata *opendata;
2134 fmode_t delegation_type = 0;
2135 int status;
2136
2137 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2138 NFS4_OPEN_CLAIM_PREVIOUS);
2139 if (IS_ERR(opendata))
2140 return PTR_ERR(opendata);
2141 rcu_read_lock();
2142 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2143 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2144 delegation_type = delegation->type;
2145 rcu_read_unlock();
2146 opendata->o_arg.u.delegation_type = delegation_type;
2147 status = nfs4_open_recover(opendata, state);
2148 nfs4_opendata_put(opendata);
2149 return status;
2150 }
2151
2152 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2153 {
2154 struct nfs_server *server = NFS_SERVER(state->inode);
2155 struct nfs4_exception exception = { };
2156 int err;
2157 do {
2158 err = _nfs4_do_open_reclaim(ctx, state);
2159 trace_nfs4_open_reclaim(ctx, 0, err);
2160 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2161 continue;
2162 if (err != -NFS4ERR_DELAY)
2163 break;
2164 nfs4_handle_exception(server, err, &exception);
2165 } while (exception.retry);
2166 return err;
2167 }
2168
2169 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2170 {
2171 struct nfs_open_context *ctx;
2172 int ret;
2173
2174 ctx = nfs4_state_find_open_context(state);
2175 if (IS_ERR(ctx))
2176 return -EAGAIN;
2177 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2178 nfs_state_clear_open_state_flags(state);
2179 ret = nfs4_do_open_reclaim(ctx, state);
2180 put_nfs_open_context(ctx);
2181 return ret;
2182 }
2183
2184 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err)
2185 {
2186 switch (err) {
2187 default:
2188 printk(KERN_ERR "NFS: %s: unhandled error "
2189 "%d.\n", __func__, err);
2190 case 0:
2191 case -ENOENT:
2192 case -EAGAIN:
2193 case -ESTALE:
2194 case -ETIMEDOUT:
2195 break;
2196 case -NFS4ERR_BADSESSION:
2197 case -NFS4ERR_BADSLOT:
2198 case -NFS4ERR_BAD_HIGH_SLOT:
2199 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2200 case -NFS4ERR_DEADSESSION:
2201 return -EAGAIN;
2202 case -NFS4ERR_STALE_CLIENTID:
2203 case -NFS4ERR_STALE_STATEID:
2204
2205 nfs4_schedule_lease_recovery(server->nfs_client);
2206 return -EAGAIN;
2207 case -NFS4ERR_MOVED:
2208 nfs4_schedule_migration_recovery(server);
2209 return -EAGAIN;
2210 case -NFS4ERR_LEASE_MOVED:
2211 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2212 return -EAGAIN;
2213 case -NFS4ERR_DELEG_REVOKED:
2214 case -NFS4ERR_ADMIN_REVOKED:
2215 case -NFS4ERR_EXPIRED:
2216 case -NFS4ERR_BAD_STATEID:
2217 case -NFS4ERR_OPENMODE:
2218 nfs_inode_find_state_and_recover(state->inode,
2219 stateid);
2220 nfs4_schedule_stateid_recovery(server, state);
2221 return -EAGAIN;
2222 case -NFS4ERR_DELAY:
2223 case -NFS4ERR_GRACE:
2224 ssleep(1);
2225 return -EAGAIN;
2226 case -ENOMEM:
2227 case -NFS4ERR_DENIED:
2228 if (fl) {
2229 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2230 if (lsp)
2231 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2232 }
2233 return 0;
2234 }
2235 return err;
2236 }
2237
2238 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2239 struct nfs4_state *state, const nfs4_stateid *stateid)
2240 {
2241 struct nfs_server *server = NFS_SERVER(state->inode);
2242 struct nfs4_opendata *opendata;
2243 int err = 0;
2244
2245 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2246 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2247 if (IS_ERR(opendata))
2248 return PTR_ERR(opendata);
2249 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2250 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2251 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2252 if (err)
2253 goto out;
2254 }
2255 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2256 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2257 if (err)
2258 goto out;
2259 }
2260 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2261 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2262 if (err)
2263 goto out;
2264 }
2265 nfs_state_clear_delegation(state);
2266 out:
2267 nfs4_opendata_put(opendata);
2268 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2269 }
2270
2271 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2272 {
2273 struct nfs4_opendata *data = calldata;
2274
2275 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2276 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2277 }
2278
2279 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2280 {
2281 struct nfs4_opendata *data = calldata;
2282
2283 nfs40_sequence_done(task, &data->c_res.seq_res);
2284
2285 data->rpc_status = task->tk_status;
2286 if (data->rpc_status == 0) {
2287 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2288 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2289 renew_lease(data->o_res.server, data->timestamp);
2290 data->rpc_done = true;
2291 }
2292 }
2293
2294 static void nfs4_open_confirm_release(void *calldata)
2295 {
2296 struct nfs4_opendata *data = calldata;
2297 struct nfs4_state *state = NULL;
2298
2299
2300 if (!data->cancelled)
2301 goto out_free;
2302
2303 if (!data->rpc_done)
2304 goto out_free;
2305 state = nfs4_opendata_to_nfs4_state(data);
2306 if (!IS_ERR(state))
2307 nfs4_close_state(state, data->o_arg.fmode);
2308 out_free:
2309 nfs4_opendata_put(data);
2310 }
2311
2312 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2313 .rpc_call_prepare = nfs4_open_confirm_prepare,
2314 .rpc_call_done = nfs4_open_confirm_done,
2315 .rpc_release = nfs4_open_confirm_release,
2316 };
2317
2318
2319
2320
2321 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2322 {
2323 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2324 struct rpc_task *task;
2325 struct rpc_message msg = {
2326 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2327 .rpc_argp = &data->c_arg,
2328 .rpc_resp = &data->c_res,
2329 .rpc_cred = data->owner->so_cred,
2330 };
2331 struct rpc_task_setup task_setup_data = {
2332 .rpc_client = server->client,
2333 .rpc_message = &msg,
2334 .callback_ops = &nfs4_open_confirm_ops,
2335 .callback_data = data,
2336 .workqueue = nfsiod_workqueue,
2337 .flags = RPC_TASK_ASYNC,
2338 };
2339 int status;
2340
2341 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2342 data->is_recover);
2343 kref_get(&data->kref);
2344 data->rpc_done = false;
2345 data->rpc_status = 0;
2346 data->timestamp = jiffies;
2347 task = rpc_run_task(&task_setup_data);
2348 if (IS_ERR(task))
2349 return PTR_ERR(task);
2350 status = rpc_wait_for_completion_task(task);
2351 if (status != 0) {
2352 data->cancelled = true;
2353 smp_wmb();
2354 } else
2355 status = data->rpc_status;
2356 rpc_put_task(task);
2357 return status;
2358 }
2359
2360 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2361 {
2362 struct nfs4_opendata *data = calldata;
2363 struct nfs4_state_owner *sp = data->owner;
2364 struct nfs_client *clp = sp->so_server->nfs_client;
2365 enum open_claim_type4 claim = data->o_arg.claim;
2366
2367 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2368 goto out_wait;
2369
2370
2371
2372
2373 if (data->state != NULL) {
2374 struct nfs_delegation *delegation;
2375
2376 if (can_open_cached(data->state, data->o_arg.fmode,
2377 data->o_arg.open_flags, claim))
2378 goto out_no_action;
2379 rcu_read_lock();
2380 delegation = nfs4_get_valid_delegation(data->state->inode);
2381 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2382 goto unlock_no_action;
2383 rcu_read_unlock();
2384 }
2385
2386 data->o_arg.clientid = clp->cl_clientid;
2387 switch (claim) {
2388 default:
2389 break;
2390 case NFS4_OPEN_CLAIM_PREVIOUS:
2391 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2392 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2393 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2394
2395 case NFS4_OPEN_CLAIM_FH:
2396 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2397 }
2398 data->timestamp = jiffies;
2399 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2400 &data->o_arg.seq_args,
2401 &data->o_res.seq_res,
2402 task) != 0)
2403 nfs_release_seqid(data->o_arg.seqid);
2404
2405
2406 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2407 if (data->o_arg.open_flags & O_EXCL) {
2408 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2409 if (nfs4_has_persistent_session(clp))
2410 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2411 else if (clp->cl_mvops->minor_version > 0)
2412 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2413 }
2414 return;
2415 unlock_no_action:
2416 trace_nfs4_cached_open(data->state);
2417 rcu_read_unlock();
2418 out_no_action:
2419 task->tk_action = NULL;
2420 out_wait:
2421 nfs4_sequence_done(task, &data->o_res.seq_res);
2422 }
2423
2424 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2425 {
2426 struct nfs4_opendata *data = calldata;
2427
2428 data->rpc_status = task->tk_status;
2429
2430 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2431 return;
2432
2433 if (task->tk_status == 0) {
2434 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2435 switch (data->o_res.f_attr->mode & S_IFMT) {
2436 case S_IFREG:
2437 break;
2438 case S_IFLNK:
2439 data->rpc_status = -ELOOP;
2440 break;
2441 case S_IFDIR:
2442 data->rpc_status = -EISDIR;
2443 break;
2444 default:
2445 data->rpc_status = -ENOTDIR;
2446 }
2447 }
2448 renew_lease(data->o_res.server, data->timestamp);
2449 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2450 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2451 }
2452 data->rpc_done = true;
2453 }
2454
2455 static void nfs4_open_release(void *calldata)
2456 {
2457 struct nfs4_opendata *data = calldata;
2458 struct nfs4_state *state = NULL;
2459
2460
2461 if (!data->cancelled)
2462 goto out_free;
2463
2464 if (data->rpc_status != 0 || !data->rpc_done)
2465 goto out_free;
2466
2467 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2468 goto out_free;
2469 state = nfs4_opendata_to_nfs4_state(data);
2470 if (!IS_ERR(state))
2471 nfs4_close_state(state, data->o_arg.fmode);
2472 out_free:
2473 nfs4_opendata_put(data);
2474 }
2475
2476 static const struct rpc_call_ops nfs4_open_ops = {
2477 .rpc_call_prepare = nfs4_open_prepare,
2478 .rpc_call_done = nfs4_open_done,
2479 .rpc_release = nfs4_open_release,
2480 };
2481
2482 static int nfs4_run_open_task(struct nfs4_opendata *data,
2483 struct nfs_open_context *ctx)
2484 {
2485 struct inode *dir = d_inode(data->dir);
2486 struct nfs_server *server = NFS_SERVER(dir);
2487 struct nfs_openargs *o_arg = &data->o_arg;
2488 struct nfs_openres *o_res = &data->o_res;
2489 struct rpc_task *task;
2490 struct rpc_message msg = {
2491 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2492 .rpc_argp = o_arg,
2493 .rpc_resp = o_res,
2494 .rpc_cred = data->owner->so_cred,
2495 };
2496 struct rpc_task_setup task_setup_data = {
2497 .rpc_client = server->client,
2498 .rpc_message = &msg,
2499 .callback_ops = &nfs4_open_ops,
2500 .callback_data = data,
2501 .workqueue = nfsiod_workqueue,
2502 .flags = RPC_TASK_ASYNC,
2503 };
2504 int status;
2505
2506 kref_get(&data->kref);
2507 data->rpc_done = false;
2508 data->rpc_status = 0;
2509 data->cancelled = false;
2510 data->is_recover = false;
2511 if (!ctx) {
2512 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2513 data->is_recover = true;
2514 task_setup_data.flags |= RPC_TASK_TIMEOUT;
2515 } else {
2516 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2517 pnfs_lgopen_prepare(data, ctx);
2518 }
2519 task = rpc_run_task(&task_setup_data);
2520 if (IS_ERR(task))
2521 return PTR_ERR(task);
2522 status = rpc_wait_for_completion_task(task);
2523 if (status != 0) {
2524 data->cancelled = true;
2525 smp_wmb();
2526 } else
2527 status = data->rpc_status;
2528 rpc_put_task(task);
2529
2530 return status;
2531 }
2532
2533 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2534 {
2535 struct inode *dir = d_inode(data->dir);
2536 struct nfs_openres *o_res = &data->o_res;
2537 int status;
2538
2539 status = nfs4_run_open_task(data, NULL);
2540 if (status != 0 || !data->rpc_done)
2541 return status;
2542
2543 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2544
2545 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2546 status = _nfs4_proc_open_confirm(data);
2547
2548 return status;
2549 }
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559 static int nfs4_opendata_access(const struct cred *cred,
2560 struct nfs4_opendata *opendata,
2561 struct nfs4_state *state, fmode_t fmode,
2562 int openflags)
2563 {
2564 struct nfs_access_entry cache;
2565 u32 mask, flags;
2566
2567
2568
2569 if (opendata->o_res.access_supported == 0)
2570 return 0;
2571
2572 mask = 0;
2573
2574
2575
2576
2577 if (openflags & __FMODE_EXEC) {
2578
2579 if (S_ISDIR(state->inode->i_mode))
2580 mask = NFS4_ACCESS_LOOKUP;
2581 else
2582 mask = NFS4_ACCESS_EXECUTE;
2583 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2584 mask = NFS4_ACCESS_READ;
2585
2586 cache.cred = cred;
2587 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2588 nfs_access_add_cache(state->inode, &cache);
2589
2590 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2591 if ((mask & ~cache.mask & flags) == 0)
2592 return 0;
2593
2594 return -EACCES;
2595 }
2596
2597
2598
2599
2600 static int _nfs4_proc_open(struct nfs4_opendata *data,
2601 struct nfs_open_context *ctx)
2602 {
2603 struct inode *dir = d_inode(data->dir);
2604 struct nfs_server *server = NFS_SERVER(dir);
2605 struct nfs_openargs *o_arg = &data->o_arg;
2606 struct nfs_openres *o_res = &data->o_res;
2607 int status;
2608
2609 status = nfs4_run_open_task(data, ctx);
2610 if (!data->rpc_done)
2611 return status;
2612 if (status != 0) {
2613 if (status == -NFS4ERR_BADNAME &&
2614 !(o_arg->open_flags & O_CREAT))
2615 return -ENOENT;
2616 return status;
2617 }
2618
2619 nfs_fattr_map_and_free_names(server, &data->f_attr);
2620
2621 if (o_arg->open_flags & O_CREAT) {
2622 if (o_arg->open_flags & O_EXCL)
2623 data->file_created = true;
2624 else if (o_res->cinfo.before != o_res->cinfo.after)
2625 data->file_created = true;
2626 if (data->file_created ||
2627 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2628 update_changeattr(dir, &o_res->cinfo,
2629 o_res->f_attr->time_start, 0);
2630 }
2631 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2632 server->caps &= ~NFS_CAP_POSIX_LOCK;
2633 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2634 status = _nfs4_proc_open_confirm(data);
2635 if (status != 0)
2636 return status;
2637 }
2638 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2639 nfs4_sequence_free_slot(&o_res->seq_res);
2640 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2641 o_res->f_label, NULL);
2642 }
2643 return 0;
2644 }
2645
2646
2647
2648
2649
2650
2651 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2652 {
2653 struct nfs4_opendata *opendata;
2654 int ret;
2655
2656 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2657 NFS4_OPEN_CLAIM_FH);
2658 if (IS_ERR(opendata))
2659 return PTR_ERR(opendata);
2660 ret = nfs4_open_recover(opendata, state);
2661 if (ret == -ESTALE)
2662 d_drop(ctx->dentry);
2663 nfs4_opendata_put(opendata);
2664 return ret;
2665 }
2666
2667 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2668 {
2669 struct nfs_server *server = NFS_SERVER(state->inode);
2670 struct nfs4_exception exception = { };
2671 int err;
2672
2673 do {
2674 err = _nfs4_open_expired(ctx, state);
2675 trace_nfs4_open_expired(ctx, 0, err);
2676 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2677 continue;
2678 switch (err) {
2679 default:
2680 goto out;
2681 case -NFS4ERR_GRACE:
2682 case -NFS4ERR_DELAY:
2683 nfs4_handle_exception(server, err, &exception);
2684 err = 0;
2685 }
2686 } while (exception.retry);
2687 out:
2688 return err;
2689 }
2690
2691 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2692 {
2693 struct nfs_open_context *ctx;
2694 int ret;
2695
2696 ctx = nfs4_state_find_open_context(state);
2697 if (IS_ERR(ctx))
2698 return -EAGAIN;
2699 ret = nfs4_do_open_expired(ctx, state);
2700 put_nfs_open_context(ctx);
2701 return ret;
2702 }
2703
2704 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2705 const nfs4_stateid *stateid)
2706 {
2707 nfs_remove_bad_delegation(state->inode, stateid);
2708 nfs_state_clear_delegation(state);
2709 }
2710
2711 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2712 {
2713 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2714 nfs_finish_clear_delegation_stateid(state, NULL);
2715 }
2716
2717 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2718 {
2719
2720 nfs40_clear_delegation_stateid(state);
2721 nfs_state_clear_open_state_flags(state);
2722 return nfs4_open_expired(sp, state);
2723 }
2724
2725 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2726 nfs4_stateid *stateid,
2727 const struct cred *cred)
2728 {
2729 return -NFS4ERR_BAD_STATEID;
2730 }
2731
2732 #if defined(CONFIG_NFS_V4_1)
2733 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2734 nfs4_stateid *stateid,
2735 const struct cred *cred)
2736 {
2737 int status;
2738
2739 switch (stateid->type) {
2740 default:
2741 break;
2742 case NFS4_INVALID_STATEID_TYPE:
2743 case NFS4_SPECIAL_STATEID_TYPE:
2744 return -NFS4ERR_BAD_STATEID;
2745 case NFS4_REVOKED_STATEID_TYPE:
2746 goto out_free;
2747 }
2748
2749 status = nfs41_test_stateid(server, stateid, cred);
2750 switch (status) {
2751 case -NFS4ERR_EXPIRED:
2752 case -NFS4ERR_ADMIN_REVOKED:
2753 case -NFS4ERR_DELEG_REVOKED:
2754 break;
2755 default:
2756 return status;
2757 }
2758 out_free:
2759
2760 nfs41_free_stateid(server, stateid, cred, true);
2761 return -NFS4ERR_EXPIRED;
2762 }
2763
2764 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2765 {
2766 struct nfs_server *server = NFS_SERVER(state->inode);
2767 nfs4_stateid stateid;
2768 struct nfs_delegation *delegation;
2769 const struct cred *cred = NULL;
2770 int status, ret = NFS_OK;
2771
2772
2773 rcu_read_lock();
2774 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2775 if (delegation == NULL) {
2776 rcu_read_unlock();
2777 nfs_state_clear_delegation(state);
2778 return NFS_OK;
2779 }
2780
2781 nfs4_stateid_copy(&stateid, &delegation->stateid);
2782
2783 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2784 &delegation->flags)) {
2785 rcu_read_unlock();
2786 return NFS_OK;
2787 }
2788
2789 if (delegation->cred)
2790 cred = get_cred(delegation->cred);
2791 rcu_read_unlock();
2792 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2793 trace_nfs4_test_delegation_stateid(state, NULL, status);
2794 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2795 nfs_finish_clear_delegation_stateid(state, &stateid);
2796 else
2797 ret = status;
2798
2799 put_cred(cred);
2800 return ret;
2801 }
2802
2803 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2804 {
2805 nfs4_stateid tmp;
2806
2807 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2808 nfs4_copy_delegation_stateid(state->inode, state->state,
2809 &tmp, NULL) &&
2810 nfs4_stateid_match_other(&state->stateid, &tmp))
2811 nfs_state_set_delegation(state, &tmp, state->state);
2812 else
2813 nfs_state_clear_delegation(state);
2814 }
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824 static int nfs41_check_expired_locks(struct nfs4_state *state)
2825 {
2826 int status, ret = NFS_OK;
2827 struct nfs4_lock_state *lsp, *prev = NULL;
2828 struct nfs_server *server = NFS_SERVER(state->inode);
2829
2830 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2831 goto out;
2832
2833 spin_lock(&state->state_lock);
2834 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2835 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2836 const struct cred *cred = lsp->ls_state->owner->so_cred;
2837
2838 refcount_inc(&lsp->ls_count);
2839 spin_unlock(&state->state_lock);
2840
2841 nfs4_put_lock_state(prev);
2842 prev = lsp;
2843
2844 status = nfs41_test_and_free_expired_stateid(server,
2845 &lsp->ls_stateid,
2846 cred);
2847 trace_nfs4_test_lock_stateid(state, lsp, status);
2848 if (status == -NFS4ERR_EXPIRED ||
2849 status == -NFS4ERR_BAD_STATEID) {
2850 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2851 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2852 if (!recover_lost_locks)
2853 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2854 } else if (status != NFS_OK) {
2855 ret = status;
2856 nfs4_put_lock_state(prev);
2857 goto out;
2858 }
2859 spin_lock(&state->state_lock);
2860 }
2861 }
2862 spin_unlock(&state->state_lock);
2863 nfs4_put_lock_state(prev);
2864 out:
2865 return ret;
2866 }
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876 static int nfs41_check_open_stateid(struct nfs4_state *state)
2877 {
2878 struct nfs_server *server = NFS_SERVER(state->inode);
2879 nfs4_stateid *stateid = &state->open_stateid;
2880 const struct cred *cred = state->owner->so_cred;
2881 int status;
2882
2883 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2884 return -NFS4ERR_BAD_STATEID;
2885 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2886 trace_nfs4_test_open_stateid(state, NULL, status);
2887 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2888 nfs_state_clear_open_state_flags(state);
2889 stateid->type = NFS4_INVALID_STATEID_TYPE;
2890 return status;
2891 }
2892 if (nfs_open_stateid_recover_openmode(state))
2893 return -NFS4ERR_OPENMODE;
2894 return NFS_OK;
2895 }
2896
2897 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2898 {
2899 int status;
2900
2901 status = nfs41_check_delegation_stateid(state);
2902 if (status != NFS_OK)
2903 return status;
2904 nfs41_delegation_recover_stateid(state);
2905
2906 status = nfs41_check_expired_locks(state);
2907 if (status != NFS_OK)
2908 return status;
2909 status = nfs41_check_open_stateid(state);
2910 if (status != NFS_OK)
2911 status = nfs4_open_expired(sp, state);
2912 return status;
2913 }
2914 #endif
2915
2916
2917
2918
2919
2920
2921 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2922 struct iattr *sattr, struct nfs4_label **label)
2923 {
2924 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2925 __u32 attrset[3];
2926 unsigned ret;
2927 unsigned i;
2928
2929 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2930 attrset[i] = opendata->o_res.attrset[i];
2931 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2932 attrset[i] &= ~bitmask[i];
2933 }
2934
2935 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2936 sattr->ia_valid : 0;
2937
2938 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2939 if (sattr->ia_valid & ATTR_ATIME_SET)
2940 ret |= ATTR_ATIME_SET;
2941 else
2942 ret |= ATTR_ATIME;
2943 }
2944
2945 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2946 if (sattr->ia_valid & ATTR_MTIME_SET)
2947 ret |= ATTR_MTIME_SET;
2948 else
2949 ret |= ATTR_MTIME;
2950 }
2951
2952 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
2953 *label = NULL;
2954 return ret;
2955 }
2956
2957 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2958 int flags, struct nfs_open_context *ctx)
2959 {
2960 struct nfs4_state_owner *sp = opendata->owner;
2961 struct nfs_server *server = sp->so_server;
2962 struct dentry *dentry;
2963 struct nfs4_state *state;
2964 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
2965 struct inode *dir = d_inode(opendata->dir);
2966 unsigned long dir_verifier;
2967 unsigned int seq;
2968 int ret;
2969
2970 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2971 dir_verifier = nfs_save_change_attribute(dir);
2972
2973 ret = _nfs4_proc_open(opendata, ctx);
2974 if (ret != 0)
2975 goto out;
2976
2977 state = _nfs4_opendata_to_nfs4_state(opendata);
2978 ret = PTR_ERR(state);
2979 if (IS_ERR(state))
2980 goto out;
2981 ctx->state = state;
2982 if (server->caps & NFS_CAP_POSIX_LOCK)
2983 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2984 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2985 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2986
2987 dentry = opendata->dentry;
2988 if (d_really_is_negative(dentry)) {
2989 struct dentry *alias;
2990 d_drop(dentry);
2991 alias = d_exact_alias(dentry, state->inode);
2992 if (!alias)
2993 alias = d_splice_alias(igrab(state->inode), dentry);
2994
2995 if (alias) {
2996 dput(ctx->dentry);
2997 ctx->dentry = dentry = alias;
2998 }
2999 }
3000
3001 switch(opendata->o_arg.claim) {
3002 default:
3003 break;
3004 case NFS4_OPEN_CLAIM_NULL:
3005 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3006 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3007 if (!opendata->rpc_done)
3008 break;
3009 if (opendata->o_res.delegation_type != 0)
3010 dir_verifier = nfs_save_change_attribute(dir);
3011 nfs_set_verifier(dentry, dir_verifier);
3012 }
3013
3014
3015 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3016
3017 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3018 acc_mode, flags);
3019 if (ret != 0)
3020 goto out;
3021
3022 if (d_inode(dentry) == state->inode) {
3023 nfs_inode_attach_open_context(ctx);
3024 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3025 nfs4_schedule_stateid_recovery(server, state);
3026 }
3027
3028 out:
3029 if (!opendata->cancelled)
3030 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3031 return ret;
3032 }
3033
3034
3035
3036
3037 static int _nfs4_do_open(struct inode *dir,
3038 struct nfs_open_context *ctx,
3039 int flags,
3040 const struct nfs4_open_createattrs *c,
3041 int *opened)
3042 {
3043 struct nfs4_state_owner *sp;
3044 struct nfs4_state *state = NULL;
3045 struct nfs_server *server = NFS_SERVER(dir);
3046 struct nfs4_opendata *opendata;
3047 struct dentry *dentry = ctx->dentry;
3048 const struct cred *cred = ctx->cred;
3049 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3050 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3051 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3052 struct iattr *sattr = c->sattr;
3053 struct nfs4_label *label = c->label;
3054 struct nfs4_label *olabel = NULL;
3055 int status;
3056
3057
3058 status = -ENOMEM;
3059 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3060 if (sp == NULL) {
3061 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3062 goto out_err;
3063 }
3064 status = nfs4_client_recover_expired_lease(server->nfs_client);
3065 if (status != 0)
3066 goto err_put_state_owner;
3067 if (d_really_is_positive(dentry))
3068 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3069 status = -ENOMEM;
3070 if (d_really_is_positive(dentry))
3071 claim = NFS4_OPEN_CLAIM_FH;
3072 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3073 c, claim, GFP_KERNEL);
3074 if (opendata == NULL)
3075 goto err_put_state_owner;
3076
3077 if (label) {
3078 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3079 if (IS_ERR(olabel)) {
3080 status = PTR_ERR(olabel);
3081 goto err_opendata_put;
3082 }
3083 }
3084
3085 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3086 if (!opendata->f_attr.mdsthreshold) {
3087 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3088 if (!opendata->f_attr.mdsthreshold)
3089 goto err_free_label;
3090 }
3091 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3092 }
3093 if (d_really_is_positive(dentry))
3094 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3095
3096 status = _nfs4_open_and_get_state(opendata, flags, ctx);
3097 if (status != 0)
3098 goto err_free_label;
3099 state = ctx->state;
3100
3101 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3102 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3103 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3104
3105
3106
3107
3108 if (attrs || label) {
3109 unsigned ia_old = sattr->ia_valid;
3110
3111 sattr->ia_valid = attrs;
3112 nfs_fattr_init(opendata->o_res.f_attr);
3113 status = nfs4_do_setattr(state->inode, cred,
3114 opendata->o_res.f_attr, sattr,
3115 ctx, label, olabel);
3116 if (status == 0) {
3117 nfs_setattr_update_inode(state->inode, sattr,
3118 opendata->o_res.f_attr);
3119 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3120 }
3121 sattr->ia_valid = ia_old;
3122 }
3123 }
3124 if (opened && opendata->file_created)
3125 *opened = 1;
3126
3127 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3128 *ctx_th = opendata->f_attr.mdsthreshold;
3129 opendata->f_attr.mdsthreshold = NULL;
3130 }
3131
3132 nfs4_label_free(olabel);
3133
3134 nfs4_opendata_put(opendata);
3135 nfs4_put_state_owner(sp);
3136 return 0;
3137 err_free_label:
3138 nfs4_label_free(olabel);
3139 err_opendata_put:
3140 nfs4_opendata_put(opendata);
3141 err_put_state_owner:
3142 nfs4_put_state_owner(sp);
3143 out_err:
3144 return status;
3145 }
3146
3147
3148 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3149 struct nfs_open_context *ctx,
3150 int flags,
3151 struct iattr *sattr,
3152 struct nfs4_label *label,
3153 int *opened)
3154 {
3155 struct nfs_server *server = NFS_SERVER(dir);
3156 struct nfs4_exception exception = {
3157 .interruptible = true,
3158 };
3159 struct nfs4_state *res;
3160 struct nfs4_open_createattrs c = {
3161 .label = label,
3162 .sattr = sattr,
3163 .verf = {
3164 [0] = (__u32)jiffies,
3165 [1] = (__u32)current->pid,
3166 },
3167 };
3168 int status;
3169
3170 do {
3171 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3172 res = ctx->state;
3173 trace_nfs4_open_file(ctx, flags, status);
3174 if (status == 0)
3175 break;
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187 if (status == -NFS4ERR_BAD_SEQID) {
3188 pr_warn_ratelimited("NFS: v4 server %s "
3189 " returned a bad sequence-id error!\n",
3190 NFS_SERVER(dir)->nfs_client->cl_hostname);
3191 exception.retry = 1;
3192 continue;
3193 }
3194
3195
3196
3197
3198
3199
3200 if (status == -NFS4ERR_BAD_STATEID) {
3201 exception.retry = 1;
3202 continue;
3203 }
3204 if (status == -NFS4ERR_EXPIRED) {
3205 nfs4_schedule_lease_recovery(server->nfs_client);
3206 exception.retry = 1;
3207 continue;
3208 }
3209 if (status == -EAGAIN) {
3210
3211 exception.retry = 1;
3212 continue;
3213 }
3214 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3215 continue;
3216 res = ERR_PTR(nfs4_handle_exception(server,
3217 status, &exception));
3218 } while (exception.retry);
3219 return res;
3220 }
3221
3222 static int _nfs4_do_setattr(struct inode *inode,
3223 struct nfs_setattrargs *arg,
3224 struct nfs_setattrres *res,
3225 const struct cred *cred,
3226 struct nfs_open_context *ctx)
3227 {
3228 struct nfs_server *server = NFS_SERVER(inode);
3229 struct rpc_message msg = {
3230 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3231 .rpc_argp = arg,
3232 .rpc_resp = res,
3233 .rpc_cred = cred,
3234 };
3235 const struct cred *delegation_cred = NULL;
3236 unsigned long timestamp = jiffies;
3237 bool truncate;
3238 int status;
3239
3240 nfs_fattr_init(res->fattr);
3241
3242
3243 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3244 if (!truncate)
3245 goto zero_stateid;
3246
3247 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3248
3249 } else if (ctx != NULL && ctx->state) {
3250 struct nfs_lock_context *l_ctx;
3251 if (!nfs4_valid_open_stateid(ctx->state))
3252 return -EBADF;
3253 l_ctx = nfs_get_lock_context(ctx);
3254 if (IS_ERR(l_ctx))
3255 return PTR_ERR(l_ctx);
3256 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3257 &arg->stateid, &delegation_cred);
3258 nfs_put_lock_context(l_ctx);
3259 if (status == -EIO)
3260 return -EBADF;
3261 } else {
3262 zero_stateid:
3263 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3264 }
3265 if (delegation_cred)
3266 msg.rpc_cred = delegation_cred;
3267
3268 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3269
3270 put_cred(delegation_cred);
3271 if (status == 0 && ctx != NULL)
3272 renew_lease(server, timestamp);
3273 trace_nfs4_setattr(inode, &arg->stateid, status);
3274 return status;
3275 }
3276
3277 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3278 struct nfs_fattr *fattr, struct iattr *sattr,
3279 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3280 struct nfs4_label *olabel)
3281 {
3282 struct nfs_server *server = NFS_SERVER(inode);
3283 __u32 bitmask[NFS4_BITMASK_SZ];
3284 struct nfs4_state *state = ctx ? ctx->state : NULL;
3285 struct nfs_setattrargs arg = {
3286 .fh = NFS_FH(inode),
3287 .iap = sattr,
3288 .server = server,
3289 .bitmask = bitmask,
3290 .label = ilabel,
3291 };
3292 struct nfs_setattrres res = {
3293 .fattr = fattr,
3294 .label = olabel,
3295 .server = server,
3296 };
3297 struct nfs4_exception exception = {
3298 .state = state,
3299 .inode = inode,
3300 .stateid = &arg.stateid,
3301 };
3302 int err;
3303
3304 do {
3305 nfs4_bitmap_copy_adjust_setattr(bitmask,
3306 nfs4_bitmask(server, olabel),
3307 inode);
3308
3309 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3310 switch (err) {
3311 case -NFS4ERR_OPENMODE:
3312 if (!(sattr->ia_valid & ATTR_SIZE)) {
3313 pr_warn_once("NFSv4: server %s is incorrectly "
3314 "applying open mode checks to "
3315 "a SETATTR that is not "
3316 "changing file size.\n",
3317 server->nfs_client->cl_hostname);
3318 }
3319 if (state && !(state->state & FMODE_WRITE)) {
3320 err = -EBADF;
3321 if (sattr->ia_valid & ATTR_OPEN)
3322 err = -EACCES;
3323 goto out;
3324 }
3325 }
3326 err = nfs4_handle_exception(server, err, &exception);
3327 } while (exception.retry);
3328 out:
3329 return err;
3330 }
3331
3332 static bool
3333 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3334 {
3335 if (inode == NULL || !nfs_have_layout(inode))
3336 return false;
3337
3338 return pnfs_wait_on_layoutreturn(inode, task);
3339 }
3340
3341
3342
3343
3344 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3345 struct nfs4_state *state)
3346 {
3347 __be32 seqid_open;
3348 u32 dst_seqid;
3349 int seq;
3350
3351 for (;;) {
3352 if (!nfs4_valid_open_stateid(state))
3353 break;
3354 seq = read_seqbegin(&state->seqlock);
3355 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3356 nfs4_stateid_copy(dst, &state->open_stateid);
3357 if (read_seqretry(&state->seqlock, seq))
3358 continue;
3359 break;
3360 }
3361 seqid_open = state->open_stateid.seqid;
3362 if (read_seqretry(&state->seqlock, seq))
3363 continue;
3364
3365 dst_seqid = be32_to_cpu(dst->seqid);
3366 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3367 dst->seqid = seqid_open;
3368 break;
3369 }
3370 }
3371
3372
3373
3374
3375
3376 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3377 struct nfs4_state *state)
3378 {
3379 __be32 seqid_open;
3380 u32 dst_seqid;
3381 bool ret;
3382 int seq;
3383
3384 for (;;) {
3385 ret = false;
3386 if (!nfs4_valid_open_stateid(state))
3387 break;
3388 seq = read_seqbegin(&state->seqlock);
3389 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3390 if (read_seqretry(&state->seqlock, seq))
3391 continue;
3392 break;
3393 }
3394 seqid_open = state->open_stateid.seqid;
3395 if (read_seqretry(&state->seqlock, seq))
3396 continue;
3397
3398 dst_seqid = be32_to_cpu(dst->seqid);
3399 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) >= 0)
3400 dst->seqid = cpu_to_be32(dst_seqid + 1);
3401 else
3402 dst->seqid = seqid_open;
3403 ret = true;
3404 break;
3405 }
3406
3407 return ret;
3408 }
3409
3410 struct nfs4_closedata {
3411 struct inode *inode;
3412 struct nfs4_state *state;
3413 struct nfs_closeargs arg;
3414 struct nfs_closeres res;
3415 struct {
3416 struct nfs4_layoutreturn_args arg;
3417 struct nfs4_layoutreturn_res res;
3418 struct nfs4_xdr_opaque_data ld_private;
3419 u32 roc_barrier;
3420 bool roc;
3421 } lr;
3422 struct nfs_fattr fattr;
3423 unsigned long timestamp;
3424 };
3425
3426 static void nfs4_free_closedata(void *data)
3427 {
3428 struct nfs4_closedata *calldata = data;
3429 struct nfs4_state_owner *sp = calldata->state->owner;
3430 struct super_block *sb = calldata->state->inode->i_sb;
3431
3432 if (calldata->lr.roc)
3433 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3434 calldata->res.lr_ret);
3435 nfs4_put_open_state(calldata->state);
3436 nfs_free_seqid(calldata->arg.seqid);
3437 nfs4_put_state_owner(sp);
3438 nfs_sb_deactive(sb);
3439 kfree(calldata);
3440 }
3441
3442 static void nfs4_close_done(struct rpc_task *task, void *data)
3443 {
3444 struct nfs4_closedata *calldata = data;
3445 struct nfs4_state *state = calldata->state;
3446 struct nfs_server *server = NFS_SERVER(calldata->inode);
3447 nfs4_stateid *res_stateid = NULL;
3448 struct nfs4_exception exception = {
3449 .state = state,
3450 .inode = calldata->inode,
3451 .stateid = &calldata->arg.stateid,
3452 };
3453
3454 dprintk("%s: begin!\n", __func__);
3455 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3456 return;
3457 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3458
3459
3460 if (pnfs_roc_done(task, calldata->inode,
3461 &calldata->arg.lr_args,
3462 &calldata->res.lr_res,
3463 &calldata->res.lr_ret) == -EAGAIN)
3464 goto out_restart;
3465
3466
3467
3468
3469 switch (task->tk_status) {
3470 case 0:
3471 res_stateid = &calldata->res.stateid;
3472 renew_lease(server, calldata->timestamp);
3473 break;
3474 case -NFS4ERR_ACCESS:
3475 if (calldata->arg.bitmask != NULL) {
3476 calldata->arg.bitmask = NULL;
3477 calldata->res.fattr = NULL;
3478 goto out_restart;
3479
3480 }
3481 break;
3482 case -NFS4ERR_OLD_STATEID:
3483
3484 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3485 state))
3486 goto out_restart;
3487 goto out_release;
3488 case -NFS4ERR_ADMIN_REVOKED:
3489 case -NFS4ERR_STALE_STATEID:
3490 case -NFS4ERR_EXPIRED:
3491 nfs4_free_revoked_stateid(server,
3492 &calldata->arg.stateid,
3493 task->tk_msg.rpc_cred);
3494
3495 case -NFS4ERR_BAD_STATEID:
3496 if (calldata->arg.fmode == 0)
3497 break;
3498
3499 default:
3500 task->tk_status = nfs4_async_handle_exception(task,
3501 server, task->tk_status, &exception);
3502 if (exception.retry)
3503 goto out_restart;
3504 }
3505 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3506 res_stateid, calldata->arg.fmode);
3507 out_release:
3508 task->tk_status = 0;
3509 nfs_release_seqid(calldata->arg.seqid);
3510 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3511 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3512 return;
3513 out_restart:
3514 task->tk_status = 0;
3515 rpc_restart_call_prepare(task);
3516 goto out_release;
3517 }
3518
3519 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3520 {
3521 struct nfs4_closedata *calldata = data;
3522 struct nfs4_state *state = calldata->state;
3523 struct inode *inode = calldata->inode;
3524 struct pnfs_layout_hdr *lo;
3525 bool is_rdonly, is_wronly, is_rdwr;
3526 int call_close = 0;
3527
3528 dprintk("%s: begin!\n", __func__);
3529 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3530 goto out_wait;
3531
3532 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3533 spin_lock(&state->owner->so_lock);
3534 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3535 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3536 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3537
3538 calldata->arg.fmode = 0;
3539 if (state->n_rdwr == 0) {
3540 if (state->n_rdonly == 0)
3541 call_close |= is_rdonly;
3542 else if (is_rdonly)
3543 calldata->arg.fmode |= FMODE_READ;
3544 if (state->n_wronly == 0)
3545 call_close |= is_wronly;
3546 else if (is_wronly)
3547 calldata->arg.fmode |= FMODE_WRITE;
3548 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3549 call_close |= is_rdwr;
3550 } else if (is_rdwr)
3551 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3552
3553 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3554 if (!nfs4_valid_open_stateid(state))
3555 call_close = 0;
3556 spin_unlock(&state->owner->so_lock);
3557
3558 if (!call_close) {
3559
3560 goto out_no_action;
3561 }
3562
3563 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3564 nfs_release_seqid(calldata->arg.seqid);
3565 goto out_wait;
3566 }
3567
3568 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3569 if (lo && !pnfs_layout_is_valid(lo)) {
3570 calldata->arg.lr_args = NULL;
3571 calldata->res.lr_res = NULL;
3572 }
3573
3574 if (calldata->arg.fmode == 0)
3575 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3576
3577 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3578
3579 if (!nfs4_have_delegation(inode, FMODE_READ))
3580 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3581 else
3582 calldata->arg.bitmask = NULL;
3583 }
3584
3585 calldata->arg.share_access =
3586 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3587 calldata->arg.fmode, 0);
3588
3589 if (calldata->res.fattr == NULL)
3590 calldata->arg.bitmask = NULL;
3591 else if (calldata->arg.bitmask == NULL)
3592 calldata->res.fattr = NULL;
3593 calldata->timestamp = jiffies;
3594 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3595 &calldata->arg.seq_args,
3596 &calldata->res.seq_res,
3597 task) != 0)
3598 nfs_release_seqid(calldata->arg.seqid);
3599 dprintk("%s: done!\n", __func__);
3600 return;
3601 out_no_action:
3602 task->tk_action = NULL;
3603 out_wait:
3604 nfs4_sequence_done(task, &calldata->res.seq_res);
3605 }
3606
3607 static const struct rpc_call_ops nfs4_close_ops = {
3608 .rpc_call_prepare = nfs4_close_prepare,
3609 .rpc_call_done = nfs4_close_done,
3610 .rpc_release = nfs4_free_closedata,
3611 };
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3625 {
3626 struct nfs_server *server = NFS_SERVER(state->inode);
3627 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3628 struct nfs4_closedata *calldata;
3629 struct nfs4_state_owner *sp = state->owner;
3630 struct rpc_task *task;
3631 struct rpc_message msg = {
3632 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3633 .rpc_cred = state->owner->so_cred,
3634 };
3635 struct rpc_task_setup task_setup_data = {
3636 .rpc_client = server->client,
3637 .rpc_message = &msg,
3638 .callback_ops = &nfs4_close_ops,
3639 .workqueue = nfsiod_workqueue,
3640 .flags = RPC_TASK_ASYNC,
3641 };
3642 int status = -ENOMEM;
3643
3644 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3645 &task_setup_data.rpc_client, &msg);
3646
3647 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3648 if (calldata == NULL)
3649 goto out;
3650 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3651 calldata->inode = state->inode;
3652 calldata->state = state;
3653 calldata->arg.fh = NFS_FH(state->inode);
3654 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3655 goto out_free_calldata;
3656
3657 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3658 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3659 if (IS_ERR(calldata->arg.seqid))
3660 goto out_free_calldata;
3661 nfs_fattr_init(&calldata->fattr);
3662 calldata->arg.fmode = 0;
3663 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3664 calldata->res.fattr = &calldata->fattr;
3665 calldata->res.seqid = calldata->arg.seqid;
3666 calldata->res.server = server;
3667 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3668 calldata->lr.roc = pnfs_roc(state->inode,
3669 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3670 if (calldata->lr.roc) {
3671 calldata->arg.lr_args = &calldata->lr.arg;
3672 calldata->res.lr_res = &calldata->lr.res;
3673 }
3674 nfs_sb_active(calldata->inode->i_sb);
3675
3676 msg.rpc_argp = &calldata->arg;
3677 msg.rpc_resp = &calldata->res;
3678 task_setup_data.callback_data = calldata;
3679 task = rpc_run_task(&task_setup_data);
3680 if (IS_ERR(task))
3681 return PTR_ERR(task);
3682 status = 0;
3683 if (wait)
3684 status = rpc_wait_for_completion_task(task);
3685 rpc_put_task(task);
3686 return status;
3687 out_free_calldata:
3688 kfree(calldata);
3689 out:
3690 nfs4_put_open_state(state);
3691 nfs4_put_state_owner(sp);
3692 return status;
3693 }
3694
3695 static struct inode *
3696 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3697 int open_flags, struct iattr *attr, int *opened)
3698 {
3699 struct nfs4_state *state;
3700 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3701
3702 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3703
3704
3705 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3706
3707 nfs4_label_release_security(label);
3708
3709 if (IS_ERR(state))
3710 return ERR_CAST(state);
3711 return state->inode;
3712 }
3713
3714 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3715 {
3716 if (ctx->state == NULL)
3717 return;
3718 if (is_sync)
3719 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3720 else
3721 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3722 }
3723
3724 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3725 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3726 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3727
3728 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3729 {
3730 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3731 struct nfs4_server_caps_arg args = {
3732 .fhandle = fhandle,
3733 .bitmask = bitmask,
3734 };
3735 struct nfs4_server_caps_res res = {};
3736 struct rpc_message msg = {
3737 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3738 .rpc_argp = &args,
3739 .rpc_resp = &res,
3740 };
3741 int status;
3742 int i;
3743
3744 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3745 FATTR4_WORD0_FH_EXPIRE_TYPE |
3746 FATTR4_WORD0_LINK_SUPPORT |
3747 FATTR4_WORD0_SYMLINK_SUPPORT |
3748 FATTR4_WORD0_ACLSUPPORT;
3749 if (minorversion)
3750 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3751
3752 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3753 if (status == 0) {
3754
3755 switch (minorversion) {
3756 case 0:
3757 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3758 res.attr_bitmask[2] = 0;
3759 break;
3760 case 1:
3761 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3762 break;
3763 case 2:
3764 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3765 }
3766 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3767 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3768 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3769 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3770 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3771 NFS_CAP_CTIME|NFS_CAP_MTIME|
3772 NFS_CAP_SECURITY_LABEL);
3773 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3774 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3775 server->caps |= NFS_CAP_ACLS;
3776 if (res.has_links != 0)
3777 server->caps |= NFS_CAP_HARDLINKS;
3778 if (res.has_symlinks != 0)
3779 server->caps |= NFS_CAP_SYMLINKS;
3780 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3781 server->caps |= NFS_CAP_FILEID;
3782 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3783 server->caps |= NFS_CAP_MODE;
3784 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3785 server->caps |= NFS_CAP_NLINK;
3786 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3787 server->caps |= NFS_CAP_OWNER;
3788 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3789 server->caps |= NFS_CAP_OWNER_GROUP;
3790 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3791 server->caps |= NFS_CAP_ATIME;
3792 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3793 server->caps |= NFS_CAP_CTIME;
3794 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3795 server->caps |= NFS_CAP_MTIME;
3796 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3797 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3798 server->caps |= NFS_CAP_SECURITY_LABEL;
3799 #endif
3800 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3801 sizeof(server->attr_bitmask));
3802 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3803
3804 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3805 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3806 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3807 server->cache_consistency_bitmask[2] = 0;
3808
3809
3810 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3811 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3812 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3813 sizeof(server->exclcreat_bitmask));
3814
3815 server->acl_bitmask = res.acl_bitmask;
3816 server->fh_expire_type = res.fh_expire_type;
3817 }
3818
3819 return status;
3820 }
3821
3822 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3823 {
3824 struct nfs4_exception exception = {
3825 .interruptible = true,
3826 };
3827 int err;
3828 do {
3829 err = nfs4_handle_exception(server,
3830 _nfs4_server_capabilities(server, fhandle),
3831 &exception);
3832 } while (exception.retry);
3833 return err;
3834 }
3835
3836 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3837 struct nfs_fsinfo *info)
3838 {
3839 u32 bitmask[3];
3840 struct nfs4_lookup_root_arg args = {
3841 .bitmask = bitmask,
3842 };
3843 struct nfs4_lookup_res res = {
3844 .server = server,
3845 .fattr = info->fattr,
3846 .fh = fhandle,
3847 };
3848 struct rpc_message msg = {
3849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3850 .rpc_argp = &args,
3851 .rpc_resp = &res,
3852 };
3853
3854 bitmask[0] = nfs4_fattr_bitmap[0];
3855 bitmask[1] = nfs4_fattr_bitmap[1];
3856
3857
3858
3859 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3860
3861 nfs_fattr_init(info->fattr);
3862 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3863 }
3864
3865 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3866 struct nfs_fsinfo *info)
3867 {
3868 struct nfs4_exception exception = {
3869 .interruptible = true,
3870 };
3871 int err;
3872 do {
3873 err = _nfs4_lookup_root(server, fhandle, info);
3874 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3875 switch (err) {
3876 case 0:
3877 case -NFS4ERR_WRONGSEC:
3878 goto out;
3879 default:
3880 err = nfs4_handle_exception(server, err, &exception);
3881 }
3882 } while (exception.retry);
3883 out:
3884 return err;
3885 }
3886
3887 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3888 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3889 {
3890 struct rpc_auth_create_args auth_args = {
3891 .pseudoflavor = flavor,
3892 };
3893 struct rpc_auth *auth;
3894
3895 auth = rpcauth_create(&auth_args, server->client);
3896 if (IS_ERR(auth))
3897 return -EACCES;
3898 return nfs4_lookup_root(server, fhandle, info);
3899 }
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3911 struct nfs_fsinfo *info)
3912 {
3913
3914 static const rpc_authflavor_t flav_array[] = {
3915 RPC_AUTH_GSS_KRB5P,
3916 RPC_AUTH_GSS_KRB5I,
3917 RPC_AUTH_GSS_KRB5,
3918 RPC_AUTH_UNIX,
3919 RPC_AUTH_NULL,
3920 };
3921 int status = -EPERM;
3922 size_t i;
3923
3924 if (server->auth_info.flavor_len > 0) {
3925
3926 for (i = 0; i < server->auth_info.flavor_len; i++) {
3927 status = nfs4_lookup_root_sec(server, fhandle, info,
3928 server->auth_info.flavors[i]);
3929 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3930 continue;
3931 break;
3932 }
3933 } else {
3934
3935 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3936 status = nfs4_lookup_root_sec(server, fhandle, info,
3937 flav_array[i]);
3938 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3939 continue;
3940 break;
3941 }
3942 }
3943
3944
3945
3946
3947
3948
3949
3950
3951 if (status == -EACCES)
3952 status = -EPERM;
3953 return status;
3954 }
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3966 struct nfs_fsinfo *info,
3967 bool auth_probe)
3968 {
3969 int status = 0;
3970
3971 if (!auth_probe)
3972 status = nfs4_lookup_root(server, fhandle, info);
3973
3974 if (auth_probe || status == NFS4ERR_WRONGSEC)
3975 status = server->nfs_client->cl_mvops->find_root_sec(server,
3976 fhandle, info);
3977
3978 if (status == 0)
3979 status = nfs4_server_capabilities(server, fhandle);
3980 if (status == 0)
3981 status = nfs4_do_fsinfo(server, fhandle, info);
3982
3983 return nfs4_map_errors(status);
3984 }
3985
3986 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3987 struct nfs_fsinfo *info)
3988 {
3989 int error;
3990 struct nfs_fattr *fattr = info->fattr;
3991 struct nfs4_label *label = NULL;
3992
3993 error = nfs4_server_capabilities(server, mntfh);
3994 if (error < 0) {
3995 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3996 return error;
3997 }
3998
3999 label = nfs4_label_alloc(server, GFP_KERNEL);
4000 if (IS_ERR(label))
4001 return PTR_ERR(label);
4002
4003 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
4004 if (error < 0) {
4005 dprintk("nfs4_get_root: getattr error = %d\n", -error);
4006 goto err_free_label;
4007 }
4008
4009 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4010 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4011 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4012
4013 err_free_label:
4014 nfs4_label_free(label);
4015
4016 return error;
4017 }
4018
4019
4020
4021
4022
4023
4024 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4025 const struct qstr *name, struct nfs_fattr *fattr,
4026 struct nfs_fh *fhandle)
4027 {
4028 int status = -ENOMEM;
4029 struct page *page = NULL;
4030 struct nfs4_fs_locations *locations = NULL;
4031
4032 page = alloc_page(GFP_KERNEL);
4033 if (page == NULL)
4034 goto out;
4035 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4036 if (locations == NULL)
4037 goto out;
4038
4039 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4040 if (status != 0)
4041 goto out;
4042
4043
4044
4045
4046
4047
4048 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
4049 dprintk("%s: server did not return a different fsid for"
4050 " a referral at %s\n", __func__, name->name);
4051 status = -NFS4ERR_MOVED;
4052 goto out;
4053 }
4054
4055 nfs_fixup_referral_attributes(&locations->fattr);
4056
4057
4058 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
4059 memset(fhandle, 0, sizeof(struct nfs_fh));
4060 out:
4061 if (page)
4062 __free_page(page);
4063 kfree(locations);
4064 return status;
4065 }
4066
4067 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4068 struct nfs_fattr *fattr, struct nfs4_label *label,
4069 struct inode *inode)
4070 {
4071 __u32 bitmask[NFS4_BITMASK_SZ];
4072 struct nfs4_getattr_arg args = {
4073 .fh = fhandle,
4074 .bitmask = bitmask,
4075 };
4076 struct nfs4_getattr_res res = {
4077 .fattr = fattr,
4078 .label = label,
4079 .server = server,
4080 };
4081 struct rpc_message msg = {
4082 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4083 .rpc_argp = &args,
4084 .rpc_resp = &res,
4085 };
4086
4087 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
4088
4089 nfs_fattr_init(fattr);
4090 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4091 }
4092
4093 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4094 struct nfs_fattr *fattr, struct nfs4_label *label,
4095 struct inode *inode)
4096 {
4097 struct nfs4_exception exception = {
4098 .interruptible = true,
4099 };
4100 int err;
4101 do {
4102 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
4103 trace_nfs4_getattr(server, fhandle, fattr, err);
4104 err = nfs4_handle_exception(server, err,
4105 &exception);
4106 } while (exception.retry);
4107 return err;
4108 }
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127 static int
4128 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4129 struct iattr *sattr)
4130 {
4131 struct inode *inode = d_inode(dentry);
4132 const struct cred *cred = NULL;
4133 struct nfs_open_context *ctx = NULL;
4134 struct nfs4_label *label = NULL;
4135 int status;
4136
4137 if (pnfs_ld_layoutret_on_setattr(inode) &&
4138 sattr->ia_valid & ATTR_SIZE &&
4139 sattr->ia_size < i_size_read(inode))
4140 pnfs_commit_and_return_layout(inode);
4141
4142 nfs_fattr_init(fattr);
4143
4144
4145 if (sattr->ia_valid & ATTR_OPEN)
4146 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4147
4148
4149 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4150 return 0;
4151
4152
4153 if (sattr->ia_valid & ATTR_FILE) {
4154
4155 ctx = nfs_file_open_context(sattr->ia_file);
4156 if (ctx)
4157 cred = ctx->cred;
4158 }
4159
4160 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4161 if (IS_ERR(label))
4162 return PTR_ERR(label);
4163
4164
4165 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4166 nfs4_inode_make_writeable(inode);
4167
4168 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
4169 if (status == 0) {
4170 nfs_setattr_update_inode(inode, sattr, fattr);
4171 nfs_setsecurity(inode, fattr, label);
4172 }
4173 nfs4_label_free(label);
4174 return status;
4175 }
4176
4177 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4178 const struct qstr *name, struct nfs_fh *fhandle,
4179 struct nfs_fattr *fattr, struct nfs4_label *label)
4180 {
4181 struct nfs_server *server = NFS_SERVER(dir);
4182 int status;
4183 struct nfs4_lookup_arg args = {
4184 .bitmask = server->attr_bitmask,
4185 .dir_fh = NFS_FH(dir),
4186 .name = name,
4187 };
4188 struct nfs4_lookup_res res = {
4189 .server = server,
4190 .fattr = fattr,
4191 .label = label,
4192 .fh = fhandle,
4193 };
4194 struct rpc_message msg = {
4195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4196 .rpc_argp = &args,
4197 .rpc_resp = &res,
4198 };
4199
4200 args.bitmask = nfs4_bitmask(server, label);
4201
4202 nfs_fattr_init(fattr);
4203
4204 dprintk("NFS call lookup %s\n", name->name);
4205 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
4206 dprintk("NFS reply lookup: %d\n", status);
4207 return status;
4208 }
4209
4210 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4211 {
4212 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4213 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4214 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4215 fattr->nlink = 2;
4216 }
4217
4218 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4219 const struct qstr *name, struct nfs_fh *fhandle,
4220 struct nfs_fattr *fattr, struct nfs4_label *label)
4221 {
4222 struct nfs4_exception exception = {
4223 .interruptible = true,
4224 };
4225 struct rpc_clnt *client = *clnt;
4226 int err;
4227 do {
4228 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
4229 trace_nfs4_lookup(dir, name, err);
4230 switch (err) {
4231 case -NFS4ERR_BADNAME:
4232 err = -ENOENT;
4233 goto out;
4234 case -NFS4ERR_MOVED:
4235 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4236 if (err == -NFS4ERR_MOVED)
4237 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4238 goto out;
4239 case -NFS4ERR_WRONGSEC:
4240 err = -EPERM;
4241 if (client != *clnt)
4242 goto out;
4243 client = nfs4_negotiate_security(client, dir, name);
4244 if (IS_ERR(client))
4245 return PTR_ERR(client);
4246
4247 exception.retry = 1;
4248 break;
4249 default:
4250 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4251 }
4252 } while (exception.retry);
4253
4254 out:
4255 if (err == 0)
4256 *clnt = client;
4257 else if (client != *clnt)
4258 rpc_shutdown_client(client);
4259
4260 return err;
4261 }
4262
4263 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
4264 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4265 struct nfs4_label *label)
4266 {
4267 int status;
4268 struct rpc_clnt *client = NFS_CLIENT(dir);
4269
4270 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
4271 if (client != NFS_CLIENT(dir)) {
4272 rpc_shutdown_client(client);
4273 nfs_fixup_secinfo_attributes(fattr);
4274 }
4275 return status;
4276 }
4277
4278 struct rpc_clnt *
4279 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
4280 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4281 {
4282 struct rpc_clnt *client = NFS_CLIENT(dir);
4283 int status;
4284
4285 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
4286 if (status < 0)
4287 return ERR_PTR(status);
4288 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4289 }
4290
4291 static int _nfs4_proc_lookupp(struct inode *inode,
4292 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4293 struct nfs4_label *label)
4294 {
4295 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4296 struct nfs_server *server = NFS_SERVER(inode);
4297 int status;
4298 struct nfs4_lookupp_arg args = {
4299 .bitmask = server->attr_bitmask,
4300 .fh = NFS_FH(inode),
4301 };
4302 struct nfs4_lookupp_res res = {
4303 .server = server,
4304 .fattr = fattr,
4305 .label = label,
4306 .fh = fhandle,
4307 };
4308 struct rpc_message msg = {
4309 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4310 .rpc_argp = &args,
4311 .rpc_resp = &res,
4312 };
4313
4314 args.bitmask = nfs4_bitmask(server, label);
4315
4316 nfs_fattr_init(fattr);
4317
4318 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4319 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4320 &res.seq_res, 0);
4321 dprintk("NFS reply lookupp: %d\n", status);
4322 return status;
4323 }
4324
4325 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4326 struct nfs_fattr *fattr, struct nfs4_label *label)
4327 {
4328 struct nfs4_exception exception = {
4329 .interruptible = true,
4330 };
4331 int err;
4332 do {
4333 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4334 trace_nfs4_lookupp(inode, err);
4335 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4336 &exception);
4337 } while (exception.retry);
4338 return err;
4339 }
4340
4341 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4342 {
4343 struct nfs_server *server = NFS_SERVER(inode);
4344 struct nfs4_accessargs args = {
4345 .fh = NFS_FH(inode),
4346 .access = entry->mask,
4347 };
4348 struct nfs4_accessres res = {
4349 .server = server,
4350 };
4351 struct rpc_message msg = {
4352 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4353 .rpc_argp = &args,
4354 .rpc_resp = &res,
4355 .rpc_cred = entry->cred,
4356 };
4357 int status = 0;
4358
4359 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4360 res.fattr = nfs_alloc_fattr();
4361 if (res.fattr == NULL)
4362 return -ENOMEM;
4363 args.bitmask = server->cache_consistency_bitmask;
4364 }
4365 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4366 if (!status) {
4367 nfs_access_set_mask(entry, res.access);
4368 if (res.fattr)
4369 nfs_refresh_inode(inode, res.fattr);
4370 }
4371 nfs_free_fattr(res.fattr);
4372 return status;
4373 }
4374
4375 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4376 {
4377 struct nfs4_exception exception = {
4378 .interruptible = true,
4379 };
4380 int err;
4381 do {
4382 err = _nfs4_proc_access(inode, entry);
4383 trace_nfs4_access(inode, err);
4384 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4385 &exception);
4386 } while (exception.retry);
4387 return err;
4388 }
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4413 unsigned int pgbase, unsigned int pglen)
4414 {
4415 struct nfs4_readlink args = {
4416 .fh = NFS_FH(inode),
4417 .pgbase = pgbase,
4418 .pglen = pglen,
4419 .pages = &page,
4420 };
4421 struct nfs4_readlink_res res;
4422 struct rpc_message msg = {
4423 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4424 .rpc_argp = &args,
4425 .rpc_resp = &res,
4426 };
4427
4428 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4429 }
4430
4431 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4432 unsigned int pgbase, unsigned int pglen)
4433 {
4434 struct nfs4_exception exception = {
4435 .interruptible = true,
4436 };
4437 int err;
4438 do {
4439 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4440 trace_nfs4_readlink(inode, err);
4441 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4442 &exception);
4443 } while (exception.retry);
4444 return err;
4445 }
4446
4447
4448
4449
4450 static int
4451 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4452 int flags)
4453 {
4454 struct nfs_server *server = NFS_SERVER(dir);
4455 struct nfs4_label l, *ilabel = NULL;
4456 struct nfs_open_context *ctx;
4457 struct nfs4_state *state;
4458 int status = 0;
4459
4460 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4461 if (IS_ERR(ctx))
4462 return PTR_ERR(ctx);
4463
4464 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4465
4466 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4467 sattr->ia_mode &= ~current_umask();
4468 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4469 if (IS_ERR(state)) {
4470 status = PTR_ERR(state);
4471 goto out;
4472 }
4473 out:
4474 nfs4_label_release_security(ilabel);
4475 put_nfs_open_context(ctx);
4476 return status;
4477 }
4478
4479 static int
4480 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4481 {
4482 struct nfs_server *server = NFS_SERVER(dir);
4483 struct nfs_removeargs args = {
4484 .fh = NFS_FH(dir),
4485 .name = *name,
4486 };
4487 struct nfs_removeres res = {
4488 .server = server,
4489 };
4490 struct rpc_message msg = {
4491 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4492 .rpc_argp = &args,
4493 .rpc_resp = &res,
4494 };
4495 unsigned long timestamp = jiffies;
4496 int status;
4497
4498 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4499 if (status == 0) {
4500 spin_lock(&dir->i_lock);
4501 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
4502
4503 if (ftype == NF4DIR && dir->i_nlink > 2)
4504 nfs4_dec_nlink_locked(dir);
4505 spin_unlock(&dir->i_lock);
4506 }
4507 return status;
4508 }
4509
4510 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4511 {
4512 struct nfs4_exception exception = {
4513 .interruptible = true,
4514 };
4515 struct inode *inode = d_inode(dentry);
4516 int err;
4517
4518 if (inode) {
4519 if (inode->i_nlink == 1)
4520 nfs4_inode_return_delegation(inode);
4521 else
4522 nfs4_inode_make_writeable(inode);
4523 }
4524 do {
4525 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4526 trace_nfs4_remove(dir, &dentry->d_name, err);
4527 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4528 &exception);
4529 } while (exception.retry);
4530 return err;
4531 }
4532
4533 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4534 {
4535 struct nfs4_exception exception = {
4536 .interruptible = true,
4537 };
4538 int err;
4539
4540 do {
4541 err = _nfs4_proc_remove(dir, name, NF4DIR);
4542 trace_nfs4_remove(dir, name, err);
4543 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4544 &exception);
4545 } while (exception.retry);
4546 return err;
4547 }
4548
4549 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4550 struct dentry *dentry,
4551 struct inode *inode)
4552 {
4553 struct nfs_removeargs *args = msg->rpc_argp;
4554 struct nfs_removeres *res = msg->rpc_resp;
4555
4556 res->server = NFS_SB(dentry->d_sb);
4557 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4558 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4559
4560 nfs_fattr_init(res->dir_attr);
4561
4562 if (inode)
4563 nfs4_inode_return_delegation(inode);
4564 }
4565
4566 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4567 {
4568 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4569 &data->args.seq_args,
4570 &data->res.seq_res,
4571 task);
4572 }
4573
4574 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4575 {
4576 struct nfs_unlinkdata *data = task->tk_calldata;
4577 struct nfs_removeres *res = &data->res;
4578
4579 if (!nfs4_sequence_done(task, &res->seq_res))
4580 return 0;
4581 if (nfs4_async_handle_error(task, res->server, NULL,
4582 &data->timeout) == -EAGAIN)
4583 return 0;
4584 if (task->tk_status == 0)
4585 update_changeattr(dir, &res->cinfo,
4586 res->dir_attr->time_start, 0);
4587 return 1;
4588 }
4589
4590 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4591 struct dentry *old_dentry,
4592 struct dentry *new_dentry)
4593 {
4594 struct nfs_renameargs *arg = msg->rpc_argp;
4595 struct nfs_renameres *res = msg->rpc_resp;
4596 struct inode *old_inode = d_inode(old_dentry);
4597 struct inode *new_inode = d_inode(new_dentry);
4598
4599 if (old_inode)
4600 nfs4_inode_make_writeable(old_inode);
4601 if (new_inode)
4602 nfs4_inode_return_delegation(new_inode);
4603 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4604 res->server = NFS_SB(old_dentry->d_sb);
4605 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4606 }
4607
4608 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4609 {
4610 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4611 &data->args.seq_args,
4612 &data->res.seq_res,
4613 task);
4614 }
4615
4616 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4617 struct inode *new_dir)
4618 {
4619 struct nfs_renamedata *data = task->tk_calldata;
4620 struct nfs_renameres *res = &data->res;
4621
4622 if (!nfs4_sequence_done(task, &res->seq_res))
4623 return 0;
4624 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4625 return 0;
4626
4627 if (task->tk_status == 0) {
4628 if (new_dir != old_dir) {
4629
4630 update_changeattr(old_dir, &res->old_cinfo,
4631 res->old_fattr->time_start,
4632 NFS_INO_INVALID_OTHER);
4633 update_changeattr(new_dir, &res->new_cinfo,
4634 res->new_fattr->time_start,
4635 NFS_INO_INVALID_OTHER);
4636 } else
4637 update_changeattr(old_dir, &res->old_cinfo,
4638 res->old_fattr->time_start,
4639 0);
4640 }
4641 return 1;
4642 }
4643
4644 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4645 {
4646 struct nfs_server *server = NFS_SERVER(inode);
4647 __u32 bitmask[NFS4_BITMASK_SZ];
4648 struct nfs4_link_arg arg = {
4649 .fh = NFS_FH(inode),
4650 .dir_fh = NFS_FH(dir),
4651 .name = name,
4652 .bitmask = bitmask,
4653 };
4654 struct nfs4_link_res res = {
4655 .server = server,
4656 .label = NULL,
4657 };
4658 struct rpc_message msg = {
4659 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4660 .rpc_argp = &arg,
4661 .rpc_resp = &res,
4662 };
4663 int status = -ENOMEM;
4664
4665 res.fattr = nfs_alloc_fattr();
4666 if (res.fattr == NULL)
4667 goto out;
4668
4669 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4670 if (IS_ERR(res.label)) {
4671 status = PTR_ERR(res.label);
4672 goto out;
4673 }
4674
4675 nfs4_inode_make_writeable(inode);
4676 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
4677
4678 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4679 if (!status) {
4680 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
4681 status = nfs_post_op_update_inode(inode, res.fattr);
4682 if (!status)
4683 nfs_setsecurity(inode, res.fattr, res.label);
4684 }
4685
4686
4687 nfs4_label_free(res.label);
4688
4689 out:
4690 nfs_free_fattr(res.fattr);
4691 return status;
4692 }
4693
4694 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4695 {
4696 struct nfs4_exception exception = {
4697 .interruptible = true,
4698 };
4699 int err;
4700 do {
4701 err = nfs4_handle_exception(NFS_SERVER(inode),
4702 _nfs4_proc_link(inode, dir, name),
4703 &exception);
4704 } while (exception.retry);
4705 return err;
4706 }
4707
4708 struct nfs4_createdata {
4709 struct rpc_message msg;
4710 struct nfs4_create_arg arg;
4711 struct nfs4_create_res res;
4712 struct nfs_fh fh;
4713 struct nfs_fattr fattr;
4714 struct nfs4_label *label;
4715 };
4716
4717 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4718 const struct qstr *name, struct iattr *sattr, u32 ftype)
4719 {
4720 struct nfs4_createdata *data;
4721
4722 data = kzalloc(sizeof(*data), GFP_KERNEL);
4723 if (data != NULL) {
4724 struct nfs_server *server = NFS_SERVER(dir);
4725
4726 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4727 if (IS_ERR(data->label))
4728 goto out_free;
4729
4730 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4731 data->msg.rpc_argp = &data->arg;
4732 data->msg.rpc_resp = &data->res;
4733 data->arg.dir_fh = NFS_FH(dir);
4734 data->arg.server = server;
4735 data->arg.name = name;
4736 data->arg.attrs = sattr;
4737 data->arg.ftype = ftype;
4738 data->arg.bitmask = nfs4_bitmask(server, data->label);
4739 data->arg.umask = current_umask();
4740 data->res.server = server;
4741 data->res.fh = &data->fh;
4742 data->res.fattr = &data->fattr;
4743 data->res.label = data->label;
4744 nfs_fattr_init(data->res.fattr);
4745 }
4746 return data;
4747 out_free:
4748 kfree(data);
4749 return NULL;
4750 }
4751
4752 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4753 {
4754 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4755 &data->arg.seq_args, &data->res.seq_res, 1);
4756 if (status == 0) {
4757 spin_lock(&dir->i_lock);
4758 update_changeattr_locked(dir, &data->res.dir_cinfo,
4759 data->res.fattr->time_start, 0);
4760
4761 if (data->arg.ftype == NF4DIR)
4762 nfs4_inc_nlink_locked(dir);
4763 spin_unlock(&dir->i_lock);
4764 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4765 }
4766 return status;
4767 }
4768
4769 static void nfs4_free_createdata(struct nfs4_createdata *data)
4770 {
4771 nfs4_label_free(data->label);
4772 kfree(data);
4773 }
4774
4775 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4776 struct page *page, unsigned int len, struct iattr *sattr,
4777 struct nfs4_label *label)
4778 {
4779 struct nfs4_createdata *data;
4780 int status = -ENAMETOOLONG;
4781
4782 if (len > NFS4_MAXPATHLEN)
4783 goto out;
4784
4785 status = -ENOMEM;
4786 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4787 if (data == NULL)
4788 goto out;
4789
4790 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4791 data->arg.u.symlink.pages = &page;
4792 data->arg.u.symlink.len = len;
4793 data->arg.label = label;
4794
4795 status = nfs4_do_create(dir, dentry, data);
4796
4797 nfs4_free_createdata(data);
4798 out:
4799 return status;
4800 }
4801
4802 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4803 struct page *page, unsigned int len, struct iattr *sattr)
4804 {
4805 struct nfs4_exception exception = {
4806 .interruptible = true,
4807 };
4808 struct nfs4_label l, *label = NULL;
4809 int err;
4810
4811 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4812
4813 do {
4814 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4815 trace_nfs4_symlink(dir, &dentry->d_name, err);
4816 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4817 &exception);
4818 } while (exception.retry);
4819
4820 nfs4_label_release_security(label);
4821 return err;
4822 }
4823
4824 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4825 struct iattr *sattr, struct nfs4_label *label)
4826 {
4827 struct nfs4_createdata *data;
4828 int status = -ENOMEM;
4829
4830 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4831 if (data == NULL)
4832 goto out;
4833
4834 data->arg.label = label;
4835 status = nfs4_do_create(dir, dentry, data);
4836
4837 nfs4_free_createdata(data);
4838 out:
4839 return status;
4840 }
4841
4842 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4843 struct iattr *sattr)
4844 {
4845 struct nfs_server *server = NFS_SERVER(dir);
4846 struct nfs4_exception exception = {
4847 .interruptible = true,
4848 };
4849 struct nfs4_label l, *label = NULL;
4850 int err;
4851
4852 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4853
4854 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4855 sattr->ia_mode &= ~current_umask();
4856 do {
4857 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4858 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4859 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4860 &exception);
4861 } while (exception.retry);
4862 nfs4_label_release_security(label);
4863
4864 return err;
4865 }
4866
4867 static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4868 u64 cookie, struct page **pages, unsigned int count, bool plus)
4869 {
4870 struct inode *dir = d_inode(dentry);
4871 struct nfs4_readdir_arg args = {
4872 .fh = NFS_FH(dir),
4873 .pages = pages,
4874 .pgbase = 0,
4875 .count = count,
4876 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4877 .plus = plus,
4878 };
4879 struct nfs4_readdir_res res;
4880 struct rpc_message msg = {
4881 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4882 .rpc_argp = &args,
4883 .rpc_resp = &res,
4884 .rpc_cred = cred,
4885 };
4886 int status;
4887
4888 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4889 dentry,
4890 (unsigned long long)cookie);
4891 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4892 res.pgbase = args.pgbase;
4893 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4894 if (status >= 0) {
4895 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4896 status += args.pgbase;
4897 }
4898
4899 nfs_invalidate_atime(dir);
4900
4901 dprintk("%s: returns %d\n", __func__, status);
4902 return status;
4903 }
4904
4905 static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4906 u64 cookie, struct page **pages, unsigned int count, bool plus)
4907 {
4908 struct nfs4_exception exception = {
4909 .interruptible = true,
4910 };
4911 int err;
4912 do {
4913 err = _nfs4_proc_readdir(dentry, cred, cookie,
4914 pages, count, plus);
4915 trace_nfs4_readdir(d_inode(dentry), err);
4916 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4917 &exception);
4918 } while (exception.retry);
4919 return err;
4920 }
4921
4922 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4923 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4924 {
4925 struct nfs4_createdata *data;
4926 int mode = sattr->ia_mode;
4927 int status = -ENOMEM;
4928
4929 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4930 if (data == NULL)
4931 goto out;
4932
4933 if (S_ISFIFO(mode))
4934 data->arg.ftype = NF4FIFO;
4935 else if (S_ISBLK(mode)) {
4936 data->arg.ftype = NF4BLK;
4937 data->arg.u.device.specdata1 = MAJOR(rdev);
4938 data->arg.u.device.specdata2 = MINOR(rdev);
4939 }
4940 else if (S_ISCHR(mode)) {
4941 data->arg.ftype = NF4CHR;
4942 data->arg.u.device.specdata1 = MAJOR(rdev);
4943 data->arg.u.device.specdata2 = MINOR(rdev);
4944 } else if (!S_ISSOCK(mode)) {
4945 status = -EINVAL;
4946 goto out_free;
4947 }
4948
4949 data->arg.label = label;
4950 status = nfs4_do_create(dir, dentry, data);
4951 out_free:
4952 nfs4_free_createdata(data);
4953 out:
4954 return status;
4955 }
4956
4957 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4958 struct iattr *sattr, dev_t rdev)
4959 {
4960 struct nfs_server *server = NFS_SERVER(dir);
4961 struct nfs4_exception exception = {
4962 .interruptible = true,
4963 };
4964 struct nfs4_label l, *label = NULL;
4965 int err;
4966
4967 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4968
4969 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4970 sattr->ia_mode &= ~current_umask();
4971 do {
4972 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4973 trace_nfs4_mknod(dir, &dentry->d_name, err);
4974 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4975 &exception);
4976 } while (exception.retry);
4977
4978 nfs4_label_release_security(label);
4979
4980 return err;
4981 }
4982
4983 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4984 struct nfs_fsstat *fsstat)
4985 {
4986 struct nfs4_statfs_arg args = {
4987 .fh = fhandle,
4988 .bitmask = server->attr_bitmask,
4989 };
4990 struct nfs4_statfs_res res = {
4991 .fsstat = fsstat,
4992 };
4993 struct rpc_message msg = {
4994 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4995 .rpc_argp = &args,
4996 .rpc_resp = &res,
4997 };
4998
4999 nfs_fattr_init(fsstat->fattr);
5000 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5001 }
5002
5003 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5004 {
5005 struct nfs4_exception exception = {
5006 .interruptible = true,
5007 };
5008 int err;
5009 do {
5010 err = nfs4_handle_exception(server,
5011 _nfs4_proc_statfs(server, fhandle, fsstat),
5012 &exception);
5013 } while (exception.retry);
5014 return err;
5015 }
5016
5017 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5018 struct nfs_fsinfo *fsinfo)
5019 {
5020 struct nfs4_fsinfo_arg args = {
5021 .fh = fhandle,
5022 .bitmask = server->attr_bitmask,
5023 };
5024 struct nfs4_fsinfo_res res = {
5025 .fsinfo = fsinfo,
5026 };
5027 struct rpc_message msg = {
5028 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5029 .rpc_argp = &args,
5030 .rpc_resp = &res,
5031 };
5032
5033 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5034 }
5035
5036 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5037 {
5038 struct nfs4_exception exception = {
5039 .interruptible = true,
5040 };
5041 int err;
5042
5043 do {
5044 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5045 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5046 if (err == 0) {
5047 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5048 break;
5049 }
5050 err = nfs4_handle_exception(server, err, &exception);
5051 } while (exception.retry);
5052 return err;
5053 }
5054
5055 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5056 {
5057 int error;
5058
5059 nfs_fattr_init(fsinfo->fattr);
5060 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5061 if (error == 0) {
5062
5063 server->pnfs_blksize = fsinfo->blksize;
5064 set_pnfs_layoutdriver(server, fhandle, fsinfo);
5065 }
5066
5067 return error;
5068 }
5069
5070 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5071 struct nfs_pathconf *pathconf)
5072 {
5073 struct nfs4_pathconf_arg args = {
5074 .fh = fhandle,
5075 .bitmask = server->attr_bitmask,
5076 };
5077 struct nfs4_pathconf_res res = {
5078 .pathconf = pathconf,
5079 };
5080 struct rpc_message msg = {
5081 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5082 .rpc_argp = &args,
5083 .rpc_resp = &res,
5084 };
5085
5086
5087 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5088 memset(pathconf, 0, sizeof(*pathconf));
5089 return 0;
5090 }
5091
5092 nfs_fattr_init(pathconf->fattr);
5093 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5094 }
5095
5096 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5097 struct nfs_pathconf *pathconf)
5098 {
5099 struct nfs4_exception exception = {
5100 .interruptible = true,
5101 };
5102 int err;
5103
5104 do {
5105 err = nfs4_handle_exception(server,
5106 _nfs4_proc_pathconf(server, fhandle, pathconf),
5107 &exception);
5108 } while (exception.retry);
5109 return err;
5110 }
5111
5112 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5113 const struct nfs_open_context *ctx,
5114 const struct nfs_lock_context *l_ctx,
5115 fmode_t fmode)
5116 {
5117 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5118 }
5119 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5120
5121 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5122 const struct nfs_open_context *ctx,
5123 const struct nfs_lock_context *l_ctx,
5124 fmode_t fmode)
5125 {
5126 nfs4_stateid current_stateid;
5127
5128
5129 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
5130 return true;
5131 return nfs4_stateid_match(stateid, ¤t_stateid);
5132 }
5133
5134 static bool nfs4_error_stateid_expired(int err)
5135 {
5136 switch (err) {
5137 case -NFS4ERR_DELEG_REVOKED:
5138 case -NFS4ERR_ADMIN_REVOKED:
5139 case -NFS4ERR_BAD_STATEID:
5140 case -NFS4ERR_STALE_STATEID:
5141 case -NFS4ERR_OLD_STATEID:
5142 case -NFS4ERR_OPENMODE:
5143 case -NFS4ERR_EXPIRED:
5144 return true;
5145 }
5146 return false;
5147 }
5148
5149 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5150 {
5151 struct nfs_server *server = NFS_SERVER(hdr->inode);
5152
5153 trace_nfs4_read(hdr, task->tk_status);
5154 if (task->tk_status < 0) {
5155 struct nfs4_exception exception = {
5156 .inode = hdr->inode,
5157 .state = hdr->args.context->state,
5158 .stateid = &hdr->args.stateid,
5159 };
5160 task->tk_status = nfs4_async_handle_exception(task,
5161 server, task->tk_status, &exception);
5162 if (exception.retry) {
5163 rpc_restart_call_prepare(task);
5164 return -EAGAIN;
5165 }
5166 }
5167
5168 if (task->tk_status > 0)
5169 renew_lease(server, hdr->timestamp);
5170 return 0;
5171 }
5172
5173 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5174 struct nfs_pgio_args *args)
5175 {
5176
5177 if (!nfs4_error_stateid_expired(task->tk_status) ||
5178 nfs4_stateid_is_current(&args->stateid,
5179 args->context,
5180 args->lock_context,
5181 FMODE_READ))
5182 return false;
5183 rpc_restart_call_prepare(task);
5184 return true;
5185 }
5186
5187 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5188 {
5189
5190 dprintk("--> %s\n", __func__);
5191
5192 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5193 return -EAGAIN;
5194 if (nfs4_read_stateid_changed(task, &hdr->args))
5195 return -EAGAIN;
5196 if (task->tk_status > 0)
5197 nfs_invalidate_atime(hdr->inode);
5198 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5199 nfs4_read_done_cb(task, hdr);
5200 }
5201
5202 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5203 struct rpc_message *msg)
5204 {
5205 hdr->timestamp = jiffies;
5206 if (!hdr->pgio_done_cb)
5207 hdr->pgio_done_cb = nfs4_read_done_cb;
5208 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5209 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5210 }
5211
5212 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5213 struct nfs_pgio_header *hdr)
5214 {
5215 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5216 &hdr->args.seq_args,
5217 &hdr->res.seq_res,
5218 task))
5219 return 0;
5220 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5221 hdr->args.lock_context,
5222 hdr->rw_mode) == -EIO)
5223 return -EIO;
5224 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5225 return -EIO;
5226 return 0;
5227 }
5228
5229 static int nfs4_write_done_cb(struct rpc_task *task,
5230 struct nfs_pgio_header *hdr)
5231 {
5232 struct inode *inode = hdr->inode;
5233
5234 trace_nfs4_write(hdr, task->tk_status);
5235 if (task->tk_status < 0) {
5236 struct nfs4_exception exception = {
5237 .inode = hdr->inode,
5238 .state = hdr->args.context->state,
5239 .stateid = &hdr->args.stateid,
5240 };
5241 task->tk_status = nfs4_async_handle_exception(task,
5242 NFS_SERVER(inode), task->tk_status,
5243 &exception);
5244 if (exception.retry) {
5245 rpc_restart_call_prepare(task);
5246 return -EAGAIN;
5247 }
5248 }
5249 if (task->tk_status >= 0) {
5250 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5251 nfs_writeback_update_inode(hdr);
5252 }
5253 return 0;
5254 }
5255
5256 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5257 struct nfs_pgio_args *args)
5258 {
5259
5260 if (!nfs4_error_stateid_expired(task->tk_status) ||
5261 nfs4_stateid_is_current(&args->stateid,
5262 args->context,
5263 args->lock_context,
5264 FMODE_WRITE))
5265 return false;
5266 rpc_restart_call_prepare(task);
5267 return true;
5268 }
5269
5270 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5271 {
5272 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5273 return -EAGAIN;
5274 if (nfs4_write_stateid_changed(task, &hdr->args))
5275 return -EAGAIN;
5276 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5277 nfs4_write_done_cb(task, hdr);
5278 }
5279
5280 static
5281 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5282 {
5283
5284 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5285 return false;
5286
5287
5288
5289 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5290 }
5291
5292 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5293 struct rpc_message *msg,
5294 struct rpc_clnt **clnt)
5295 {
5296 struct nfs_server *server = NFS_SERVER(hdr->inode);
5297
5298 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5299 hdr->args.bitmask = NULL;
5300 hdr->res.fattr = NULL;
5301 } else
5302 hdr->args.bitmask = server->cache_consistency_bitmask;
5303
5304 if (!hdr->pgio_done_cb)
5305 hdr->pgio_done_cb = nfs4_write_done_cb;
5306 hdr->res.server = server;
5307 hdr->timestamp = jiffies;
5308
5309 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5310 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5311 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5312 }
5313
5314 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5315 {
5316 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5317 &data->args.seq_args,
5318 &data->res.seq_res,
5319 task);
5320 }
5321
5322 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5323 {
5324 struct inode *inode = data->inode;
5325
5326 trace_nfs4_commit(data, task->tk_status);
5327 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5328 NULL, NULL) == -EAGAIN) {
5329 rpc_restart_call_prepare(task);
5330 return -EAGAIN;
5331 }
5332 return 0;
5333 }
5334
5335 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5336 {
5337 if (!nfs4_sequence_done(task, &data->res.seq_res))
5338 return -EAGAIN;
5339 return data->commit_done_cb(task, data);
5340 }
5341
5342 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5343 struct rpc_clnt **clnt)
5344 {
5345 struct nfs_server *server = NFS_SERVER(data->inode);
5346
5347 if (data->commit_done_cb == NULL)
5348 data->commit_done_cb = nfs4_commit_done_cb;
5349 data->res.server = server;
5350 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5351 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5352 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5353 }
5354
5355 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5356 struct nfs_commitres *res)
5357 {
5358 struct inode *dst_inode = file_inode(dst);
5359 struct nfs_server *server = NFS_SERVER(dst_inode);
5360 struct rpc_message msg = {
5361 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5362 .rpc_argp = args,
5363 .rpc_resp = res,
5364 };
5365
5366 args->fh = NFS_FH(dst_inode);
5367 return nfs4_call_sync(server->client, server, &msg,
5368 &args->seq_args, &res->seq_res, 1);
5369 }
5370
5371 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5372 {
5373 struct nfs_commitargs args = {
5374 .offset = offset,
5375 .count = count,
5376 };
5377 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5378 struct nfs4_exception exception = { };
5379 int status;
5380
5381 do {
5382 status = _nfs4_proc_commit(dst, &args, res);
5383 status = nfs4_handle_exception(dst_server, status, &exception);
5384 } while (exception.retry);
5385
5386 return status;
5387 }
5388
5389 struct nfs4_renewdata {
5390 struct nfs_client *client;
5391 unsigned long timestamp;
5392 };
5393
5394
5395
5396
5397
5398 static void nfs4_renew_release(void *calldata)
5399 {
5400 struct nfs4_renewdata *data = calldata;
5401 struct nfs_client *clp = data->client;
5402
5403 if (refcount_read(&clp->cl_count) > 1)
5404 nfs4_schedule_state_renewal(clp);
5405 nfs_put_client(clp);
5406 kfree(data);
5407 }
5408
5409 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5410 {
5411 struct nfs4_renewdata *data = calldata;
5412 struct nfs_client *clp = data->client;
5413 unsigned long timestamp = data->timestamp;
5414
5415 trace_nfs4_renew_async(clp, task->tk_status);
5416 switch (task->tk_status) {
5417 case 0:
5418 break;
5419 case -NFS4ERR_LEASE_MOVED:
5420 nfs4_schedule_lease_moved_recovery(clp);
5421 break;
5422 default:
5423
5424 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5425 return;
5426 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5427 nfs4_schedule_lease_recovery(clp);
5428 return;
5429 }
5430 nfs4_schedule_path_down_recovery(clp);
5431 }
5432 do_renew_lease(clp, timestamp);
5433 }
5434
5435 static const struct rpc_call_ops nfs4_renew_ops = {
5436 .rpc_call_done = nfs4_renew_done,
5437 .rpc_release = nfs4_renew_release,
5438 };
5439
5440 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5441 {
5442 struct rpc_message msg = {
5443 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5444 .rpc_argp = clp,
5445 .rpc_cred = cred,
5446 };
5447 struct nfs4_renewdata *data;
5448
5449 if (renew_flags == 0)
5450 return 0;
5451 if (!refcount_inc_not_zero(&clp->cl_count))
5452 return -EIO;
5453 data = kmalloc(sizeof(*data), GFP_NOFS);
5454 if (data == NULL) {
5455 nfs_put_client(clp);
5456 return -ENOMEM;
5457 }
5458 data->client = clp;
5459 data->timestamp = jiffies;
5460 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5461 &nfs4_renew_ops, data);
5462 }
5463
5464 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5465 {
5466 struct rpc_message msg = {
5467 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5468 .rpc_argp = clp,
5469 .rpc_cred = cred,
5470 };
5471 unsigned long now = jiffies;
5472 int status;
5473
5474 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5475 if (status < 0)
5476 return status;
5477 do_renew_lease(clp, now);
5478 return 0;
5479 }
5480
5481 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5482 {
5483 return server->caps & NFS_CAP_ACLS;
5484 }
5485
5486
5487
5488
5489
5490 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5491
5492 static int buf_to_pages_noslab(const void *buf, size_t buflen,
5493 struct page **pages)
5494 {
5495 struct page *newpage, **spages;
5496 int rc = 0;
5497 size_t len;
5498 spages = pages;
5499
5500 do {
5501 len = min_t(size_t, PAGE_SIZE, buflen);
5502 newpage = alloc_page(GFP_KERNEL);
5503
5504 if (newpage == NULL)
5505 goto unwind;
5506 memcpy(page_address(newpage), buf, len);
5507 buf += len;
5508 buflen -= len;
5509 *pages++ = newpage;
5510 rc++;
5511 } while (buflen != 0);
5512
5513 return rc;
5514
5515 unwind:
5516 for(; rc > 0; rc--)
5517 __free_page(spages[rc-1]);
5518 return -ENOMEM;
5519 }
5520
5521 struct nfs4_cached_acl {
5522 int cached;
5523 size_t len;
5524 char data[0];
5525 };
5526
5527 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5528 {
5529 struct nfs_inode *nfsi = NFS_I(inode);
5530
5531 spin_lock(&inode->i_lock);
5532 kfree(nfsi->nfs4_acl);
5533 nfsi->nfs4_acl = acl;
5534 spin_unlock(&inode->i_lock);
5535 }
5536
5537 static void nfs4_zap_acl_attr(struct inode *inode)
5538 {
5539 nfs4_set_cached_acl(inode, NULL);
5540 }
5541
5542 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5543 {
5544 struct nfs_inode *nfsi = NFS_I(inode);
5545 struct nfs4_cached_acl *acl;
5546 int ret = -ENOENT;
5547
5548 spin_lock(&inode->i_lock);
5549 acl = nfsi->nfs4_acl;
5550 if (acl == NULL)
5551 goto out;
5552 if (buf == NULL)
5553 goto out_len;
5554 if (acl->cached == 0)
5555 goto out;
5556 ret = -ERANGE;
5557 if (acl->len > buflen)
5558 goto out;
5559 memcpy(buf, acl->data, acl->len);
5560 out_len:
5561 ret = acl->len;
5562 out:
5563 spin_unlock(&inode->i_lock);
5564 return ret;
5565 }
5566
5567 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5568 {
5569 struct nfs4_cached_acl *acl;
5570 size_t buflen = sizeof(*acl) + acl_len;
5571
5572 if (buflen <= PAGE_SIZE) {
5573 acl = kmalloc(buflen, GFP_KERNEL);
5574 if (acl == NULL)
5575 goto out;
5576 acl->cached = 1;
5577 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5578 } else {
5579 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5580 if (acl == NULL)
5581 goto out;
5582 acl->cached = 0;
5583 }
5584 acl->len = acl_len;
5585 out:
5586 nfs4_set_cached_acl(inode, acl);
5587 }
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5600 {
5601 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
5602 struct nfs_getaclargs args = {
5603 .fh = NFS_FH(inode),
5604 .acl_pages = pages,
5605 .acl_len = buflen,
5606 };
5607 struct nfs_getaclres res = {
5608 .acl_len = buflen,
5609 };
5610 struct rpc_message msg = {
5611 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5612 .rpc_argp = &args,
5613 .rpc_resp = &res,
5614 };
5615 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5616 int ret = -ENOMEM, i;
5617
5618 if (npages > ARRAY_SIZE(pages))
5619 return -ERANGE;
5620
5621 for (i = 0; i < npages; i++) {
5622 pages[i] = alloc_page(GFP_KERNEL);
5623 if (!pages[i])
5624 goto out_free;
5625 }
5626
5627
5628 res.acl_scratch = alloc_page(GFP_KERNEL);
5629 if (!res.acl_scratch)
5630 goto out_free;
5631
5632 args.acl_len = npages * PAGE_SIZE;
5633
5634 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5635 __func__, buf, buflen, npages, args.acl_len);
5636 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5637 &msg, &args.seq_args, &res.seq_res, 0);
5638 if (ret)
5639 goto out_free;
5640
5641
5642 if (res.acl_flags & NFS4_ACL_TRUNC) {
5643
5644 if (buf == NULL)
5645 goto out_ok;
5646 ret = -ERANGE;
5647 goto out_free;
5648 }
5649 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5650 if (buf) {
5651 if (res.acl_len > buflen) {
5652 ret = -ERANGE;
5653 goto out_free;
5654 }
5655 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5656 }
5657 out_ok:
5658 ret = res.acl_len;
5659 out_free:
5660 for (i = 0; i < npages; i++)
5661 if (pages[i])
5662 __free_page(pages[i]);
5663 if (res.acl_scratch)
5664 __free_page(res.acl_scratch);
5665 return ret;
5666 }
5667
5668 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5669 {
5670 struct nfs4_exception exception = {
5671 .interruptible = true,
5672 };
5673 ssize_t ret;
5674 do {
5675 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5676 trace_nfs4_get_acl(inode, ret);
5677 if (ret >= 0)
5678 break;
5679 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5680 } while (exception.retry);
5681 return ret;
5682 }
5683
5684 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5685 {
5686 struct nfs_server *server = NFS_SERVER(inode);
5687 int ret;
5688
5689 if (!nfs4_server_supports_acls(server))
5690 return -EOPNOTSUPP;
5691 ret = nfs_revalidate_inode(server, inode);
5692 if (ret < 0)
5693 return ret;
5694 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5695 nfs_zap_acl_cache(inode);
5696 ret = nfs4_read_cached_acl(inode, buf, buflen);
5697 if (ret != -ENOENT)
5698
5699
5700 return ret;
5701 return nfs4_get_acl_uncached(inode, buf, buflen);
5702 }
5703
5704 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5705 {
5706 struct nfs_server *server = NFS_SERVER(inode);
5707 struct page *pages[NFS4ACL_MAXPAGES];
5708 struct nfs_setaclargs arg = {
5709 .fh = NFS_FH(inode),
5710 .acl_pages = pages,
5711 .acl_len = buflen,
5712 };
5713 struct nfs_setaclres res;
5714 struct rpc_message msg = {
5715 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5716 .rpc_argp = &arg,
5717 .rpc_resp = &res,
5718 };
5719 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5720 int ret, i;
5721
5722 if (!nfs4_server_supports_acls(server))
5723 return -EOPNOTSUPP;
5724 if (npages > ARRAY_SIZE(pages))
5725 return -ERANGE;
5726 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5727 if (i < 0)
5728 return i;
5729 nfs4_inode_make_writeable(inode);
5730 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5731
5732
5733
5734
5735
5736 for (; i > 0; i--)
5737 put_page(pages[i-1]);
5738
5739
5740
5741
5742
5743 spin_lock(&inode->i_lock);
5744 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
5745 | NFS_INO_INVALID_CTIME
5746 | NFS_INO_REVAL_FORCED;
5747 spin_unlock(&inode->i_lock);
5748 nfs_access_zap_cache(inode);
5749 nfs_zap_acl_cache(inode);
5750 return ret;
5751 }
5752
5753 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5754 {
5755 struct nfs4_exception exception = { };
5756 int err;
5757 do {
5758 err = __nfs4_proc_set_acl(inode, buf, buflen);
5759 trace_nfs4_set_acl(inode, err);
5760 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5761 &exception);
5762 } while (exception.retry);
5763 return err;
5764 }
5765
5766 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5767 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5768 size_t buflen)
5769 {
5770 struct nfs_server *server = NFS_SERVER(inode);
5771 struct nfs_fattr fattr;
5772 struct nfs4_label label = {0, 0, buflen, buf};
5773
5774 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5775 struct nfs4_getattr_arg arg = {
5776 .fh = NFS_FH(inode),
5777 .bitmask = bitmask,
5778 };
5779 struct nfs4_getattr_res res = {
5780 .fattr = &fattr,
5781 .label = &label,
5782 .server = server,
5783 };
5784 struct rpc_message msg = {
5785 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5786 .rpc_argp = &arg,
5787 .rpc_resp = &res,
5788 };
5789 int ret;
5790
5791 nfs_fattr_init(&fattr);
5792
5793 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5794 if (ret)
5795 return ret;
5796 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5797 return -ENOENT;
5798 if (buflen < label.len)
5799 return -ERANGE;
5800 return 0;
5801 }
5802
5803 static int nfs4_get_security_label(struct inode *inode, void *buf,
5804 size_t buflen)
5805 {
5806 struct nfs4_exception exception = {
5807 .interruptible = true,
5808 };
5809 int err;
5810
5811 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5812 return -EOPNOTSUPP;
5813
5814 do {
5815 err = _nfs4_get_security_label(inode, buf, buflen);
5816 trace_nfs4_get_security_label(inode, err);
5817 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5818 &exception);
5819 } while (exception.retry);
5820 return err;
5821 }
5822
5823 static int _nfs4_do_set_security_label(struct inode *inode,
5824 struct nfs4_label *ilabel,
5825 struct nfs_fattr *fattr,
5826 struct nfs4_label *olabel)
5827 {
5828
5829 struct iattr sattr = {0};
5830 struct nfs_server *server = NFS_SERVER(inode);
5831 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5832 struct nfs_setattrargs arg = {
5833 .fh = NFS_FH(inode),
5834 .iap = &sattr,
5835 .server = server,
5836 .bitmask = bitmask,
5837 .label = ilabel,
5838 };
5839 struct nfs_setattrres res = {
5840 .fattr = fattr,
5841 .label = olabel,
5842 .server = server,
5843 };
5844 struct rpc_message msg = {
5845 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5846 .rpc_argp = &arg,
5847 .rpc_resp = &res,
5848 };
5849 int status;
5850
5851 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5852
5853 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5854 if (status)
5855 dprintk("%s failed: %d\n", __func__, status);
5856
5857 return status;
5858 }
5859
5860 static int nfs4_do_set_security_label(struct inode *inode,
5861 struct nfs4_label *ilabel,
5862 struct nfs_fattr *fattr,
5863 struct nfs4_label *olabel)
5864 {
5865 struct nfs4_exception exception = { };
5866 int err;
5867
5868 do {
5869 err = _nfs4_do_set_security_label(inode, ilabel,
5870 fattr, olabel);
5871 trace_nfs4_set_security_label(inode, err);
5872 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5873 &exception);
5874 } while (exception.retry);
5875 return err;
5876 }
5877
5878 static int
5879 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5880 {
5881 struct nfs4_label ilabel, *olabel = NULL;
5882 struct nfs_fattr fattr;
5883 int status;
5884
5885 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5886 return -EOPNOTSUPP;
5887
5888 nfs_fattr_init(&fattr);
5889
5890 ilabel.pi = 0;
5891 ilabel.lfs = 0;
5892 ilabel.label = (char *)buf;
5893 ilabel.len = buflen;
5894
5895 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5896 if (IS_ERR(olabel)) {
5897 status = -PTR_ERR(olabel);
5898 goto out;
5899 }
5900
5901 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5902 if (status == 0)
5903 nfs_setsecurity(inode, &fattr, olabel);
5904
5905 nfs4_label_free(olabel);
5906 out:
5907 return status;
5908 }
5909 #endif
5910
5911
5912 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5913 nfs4_verifier *bootverf)
5914 {
5915 __be32 verf[2];
5916
5917 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5918
5919
5920 verf[0] = cpu_to_be32(U32_MAX);
5921 verf[1] = cpu_to_be32(U32_MAX);
5922 } else {
5923 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5924 u64 ns = ktime_to_ns(nn->boot_time);
5925
5926 verf[0] = cpu_to_be32(ns >> 32);
5927 verf[1] = cpu_to_be32(ns);
5928 }
5929 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5930 }
5931
5932 static int
5933 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5934 {
5935 size_t len;
5936 char *str;
5937
5938 if (clp->cl_owner_id != NULL)
5939 return 0;
5940
5941 rcu_read_lock();
5942 len = 14 +
5943 strlen(clp->cl_rpcclient->cl_nodename) +
5944 1 +
5945 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5946 1;
5947 rcu_read_unlock();
5948 if (nfs4_client_id_uniquifier[0] != '\0')
5949 len += strlen(nfs4_client_id_uniquifier) + 1;
5950 if (len > NFS4_OPAQUE_LIMIT + 1)
5951 return -EINVAL;
5952
5953
5954
5955
5956
5957
5958 str = kmalloc(len, GFP_KERNEL);
5959 if (!str)
5960 return -ENOMEM;
5961
5962 rcu_read_lock();
5963 if (nfs4_client_id_uniquifier[0] != '\0')
5964 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
5965 clp->cl_rpcclient->cl_nodename,
5966 nfs4_client_id_uniquifier,
5967 rpc_peeraddr2str(clp->cl_rpcclient,
5968 RPC_DISPLAY_ADDR));
5969 else
5970 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
5971 clp->cl_rpcclient->cl_nodename,
5972 rpc_peeraddr2str(clp->cl_rpcclient,
5973 RPC_DISPLAY_ADDR));
5974 rcu_read_unlock();
5975
5976 clp->cl_owner_id = str;
5977 return 0;
5978 }
5979
5980 static int
5981 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5982 {
5983 size_t len;
5984 char *str;
5985
5986 len = 10 + 10 + 1 + 10 + 1 +
5987 strlen(nfs4_client_id_uniquifier) + 1 +
5988 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5989
5990 if (len > NFS4_OPAQUE_LIMIT + 1)
5991 return -EINVAL;
5992
5993
5994
5995
5996
5997
5998 str = kmalloc(len, GFP_KERNEL);
5999 if (!str)
6000 return -ENOMEM;
6001
6002 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6003 clp->rpc_ops->version, clp->cl_minorversion,
6004 nfs4_client_id_uniquifier,
6005 clp->cl_rpcclient->cl_nodename);
6006 clp->cl_owner_id = str;
6007 return 0;
6008 }
6009
6010 static int
6011 nfs4_init_uniform_client_string(struct nfs_client *clp)
6012 {
6013 size_t len;
6014 char *str;
6015
6016 if (clp->cl_owner_id != NULL)
6017 return 0;
6018
6019 if (nfs4_client_id_uniquifier[0] != '\0')
6020 return nfs4_init_uniquifier_client_string(clp);
6021
6022 len = 10 + 10 + 1 + 10 + 1 +
6023 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6024
6025 if (len > NFS4_OPAQUE_LIMIT + 1)
6026 return -EINVAL;
6027
6028
6029
6030
6031
6032
6033 str = kmalloc(len, GFP_KERNEL);
6034 if (!str)
6035 return -ENOMEM;
6036
6037 scnprintf(str, len, "Linux NFSv%u.%u %s",
6038 clp->rpc_ops->version, clp->cl_minorversion,
6039 clp->cl_rpcclient->cl_nodename);
6040 clp->cl_owner_id = str;
6041 return 0;
6042 }
6043
6044
6045
6046
6047
6048
6049 static unsigned int
6050 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6051 {
6052 if (strchr(clp->cl_ipaddr, ':') != NULL)
6053 return scnprintf(buf, len, "tcp6");
6054 else
6055 return scnprintf(buf, len, "tcp");
6056 }
6057
6058 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6059 {
6060 struct nfs4_setclientid *sc = calldata;
6061
6062 if (task->tk_status == 0)
6063 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6064 }
6065
6066 static const struct rpc_call_ops nfs4_setclientid_ops = {
6067 .rpc_call_done = nfs4_setclientid_done,
6068 };
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6081 unsigned short port, const struct cred *cred,
6082 struct nfs4_setclientid_res *res)
6083 {
6084 nfs4_verifier sc_verifier;
6085 struct nfs4_setclientid setclientid = {
6086 .sc_verifier = &sc_verifier,
6087 .sc_prog = program,
6088 .sc_clnt = clp,
6089 };
6090 struct rpc_message msg = {
6091 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6092 .rpc_argp = &setclientid,
6093 .rpc_resp = res,
6094 .rpc_cred = cred,
6095 };
6096 struct rpc_task_setup task_setup_data = {
6097 .rpc_client = clp->cl_rpcclient,
6098 .rpc_message = &msg,
6099 .callback_ops = &nfs4_setclientid_ops,
6100 .callback_data = &setclientid,
6101 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6102 };
6103 unsigned long now = jiffies;
6104 int status;
6105
6106
6107 nfs4_init_boot_verifier(clp, &sc_verifier);
6108
6109 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6110 status = nfs4_init_uniform_client_string(clp);
6111 else
6112 status = nfs4_init_nonuniform_client_string(clp);
6113
6114 if (status)
6115 goto out;
6116
6117
6118 setclientid.sc_netid_len =
6119 nfs4_init_callback_netid(clp,
6120 setclientid.sc_netid,
6121 sizeof(setclientid.sc_netid));
6122 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6123 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6124 clp->cl_ipaddr, port >> 8, port & 255);
6125
6126 dprintk("NFS call setclientid auth=%s, '%s'\n",
6127 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6128 clp->cl_owner_id);
6129
6130 status = nfs4_call_sync_custom(&task_setup_data);
6131 if (setclientid.sc_cred) {
6132 kfree(clp->cl_acceptor);
6133 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6134 put_rpccred(setclientid.sc_cred);
6135 }
6136
6137 if (status == 0)
6138 do_renew_lease(clp, now);
6139 out:
6140 trace_nfs4_setclientid(clp, status);
6141 dprintk("NFS reply setclientid: %d\n", status);
6142 return status;
6143 }
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6154 struct nfs4_setclientid_res *arg,
6155 const struct cred *cred)
6156 {
6157 struct rpc_message msg = {
6158 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6159 .rpc_argp = arg,
6160 .rpc_cred = cred,
6161 };
6162 int status;
6163
6164 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6165 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6166 clp->cl_clientid);
6167 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6168 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6169 trace_nfs4_setclientid_confirm(clp, status);
6170 dprintk("NFS reply setclientid_confirm: %d\n", status);
6171 return status;
6172 }
6173
6174 struct nfs4_delegreturndata {
6175 struct nfs4_delegreturnargs args;
6176 struct nfs4_delegreturnres res;
6177 struct nfs_fh fh;
6178 nfs4_stateid stateid;
6179 unsigned long timestamp;
6180 struct {
6181 struct nfs4_layoutreturn_args arg;
6182 struct nfs4_layoutreturn_res res;
6183 struct nfs4_xdr_opaque_data ld_private;
6184 u32 roc_barrier;
6185 bool roc;
6186 } lr;
6187 struct nfs_fattr fattr;
6188 int rpc_status;
6189 struct inode *inode;
6190 };
6191
6192 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6193 {
6194 struct nfs4_delegreturndata *data = calldata;
6195 struct nfs4_exception exception = {
6196 .inode = data->inode,
6197 .stateid = &data->stateid,
6198 };
6199
6200 if (!nfs4_sequence_done(task, &data->res.seq_res))
6201 return;
6202
6203 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6204
6205
6206 if (pnfs_roc_done(task, data->inode,
6207 &data->args.lr_args,
6208 &data->res.lr_res,
6209 &data->res.lr_ret) == -EAGAIN)
6210 goto out_restart;
6211
6212 switch (task->tk_status) {
6213 case 0:
6214 renew_lease(data->res.server, data->timestamp);
6215 break;
6216 case -NFS4ERR_ADMIN_REVOKED:
6217 case -NFS4ERR_DELEG_REVOKED:
6218 case -NFS4ERR_EXPIRED:
6219 nfs4_free_revoked_stateid(data->res.server,
6220 data->args.stateid,
6221 task->tk_msg.rpc_cred);
6222
6223 case -NFS4ERR_BAD_STATEID:
6224 case -NFS4ERR_STALE_STATEID:
6225 task->tk_status = 0;
6226 break;
6227 case -NFS4ERR_OLD_STATEID:
6228 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6229 goto out_restart;
6230 task->tk_status = 0;
6231 break;
6232 case -NFS4ERR_ACCESS:
6233 if (data->args.bitmask) {
6234 data->args.bitmask = NULL;
6235 data->res.fattr = NULL;
6236 goto out_restart;
6237 }
6238
6239 default:
6240 task->tk_status = nfs4_async_handle_exception(task,
6241 data->res.server, task->tk_status,
6242 &exception);
6243 if (exception.retry)
6244 goto out_restart;
6245 }
6246 data->rpc_status = task->tk_status;
6247 return;
6248 out_restart:
6249 task->tk_status = 0;
6250 rpc_restart_call_prepare(task);
6251 }
6252
6253 static void nfs4_delegreturn_release(void *calldata)
6254 {
6255 struct nfs4_delegreturndata *data = calldata;
6256 struct inode *inode = data->inode;
6257
6258 if (inode) {
6259 if (data->lr.roc)
6260 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6261 data->res.lr_ret);
6262 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
6263 nfs_iput_and_deactive(inode);
6264 }
6265 kfree(calldata);
6266 }
6267
6268 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6269 {
6270 struct nfs4_delegreturndata *d_data;
6271 struct pnfs_layout_hdr *lo;
6272
6273 d_data = (struct nfs4_delegreturndata *)data;
6274
6275 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6276 nfs4_sequence_done(task, &d_data->res.seq_res);
6277 return;
6278 }
6279
6280 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6281 if (lo && !pnfs_layout_is_valid(lo)) {
6282 d_data->args.lr_args = NULL;
6283 d_data->res.lr_res = NULL;
6284 }
6285
6286 nfs4_setup_sequence(d_data->res.server->nfs_client,
6287 &d_data->args.seq_args,
6288 &d_data->res.seq_res,
6289 task);
6290 }
6291
6292 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6293 .rpc_call_prepare = nfs4_delegreturn_prepare,
6294 .rpc_call_done = nfs4_delegreturn_done,
6295 .rpc_release = nfs4_delegreturn_release,
6296 };
6297
6298 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6299 {
6300 struct nfs4_delegreturndata *data;
6301 struct nfs_server *server = NFS_SERVER(inode);
6302 struct rpc_task *task;
6303 struct rpc_message msg = {
6304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6305 .rpc_cred = cred,
6306 };
6307 struct rpc_task_setup task_setup_data = {
6308 .rpc_client = server->client,
6309 .rpc_message = &msg,
6310 .callback_ops = &nfs4_delegreturn_ops,
6311 .flags = RPC_TASK_ASYNC,
6312 };
6313 int status = 0;
6314
6315 data = kzalloc(sizeof(*data), GFP_NOFS);
6316 if (data == NULL)
6317 return -ENOMEM;
6318 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
6319
6320 nfs4_state_protect(server->nfs_client,
6321 NFS_SP4_MACH_CRED_CLEANUP,
6322 &task_setup_data.rpc_client, &msg);
6323
6324 data->args.fhandle = &data->fh;
6325 data->args.stateid = &data->stateid;
6326 data->args.bitmask = server->cache_consistency_bitmask;
6327 nfs_copy_fh(&data->fh, NFS_FH(inode));
6328 nfs4_stateid_copy(&data->stateid, stateid);
6329 data->res.fattr = &data->fattr;
6330 data->res.server = server;
6331 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6332 data->lr.arg.ld_private = &data->lr.ld_private;
6333 nfs_fattr_init(data->res.fattr);
6334 data->timestamp = jiffies;
6335 data->rpc_status = 0;
6336 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
6337 data->inode = nfs_igrab_and_active(inode);
6338 if (data->inode) {
6339 if (data->lr.roc) {
6340 data->args.lr_args = &data->lr.arg;
6341 data->res.lr_res = &data->lr.res;
6342 }
6343 } else if (data->lr.roc) {
6344 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6345 data->lr.roc = false;
6346 }
6347
6348 task_setup_data.callback_data = data;
6349 msg.rpc_argp = &data->args;
6350 msg.rpc_resp = &data->res;
6351 task = rpc_run_task(&task_setup_data);
6352 if (IS_ERR(task))
6353 return PTR_ERR(task);
6354 if (!issync)
6355 goto out;
6356 status = rpc_wait_for_completion_task(task);
6357 if (status != 0)
6358 goto out;
6359 status = data->rpc_status;
6360 out:
6361 rpc_put_task(task);
6362 return status;
6363 }
6364
6365 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6366 {
6367 struct nfs_server *server = NFS_SERVER(inode);
6368 struct nfs4_exception exception = { };
6369 int err;
6370 do {
6371 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6372 trace_nfs4_delegreturn(inode, stateid, err);
6373 switch (err) {
6374 case -NFS4ERR_STALE_STATEID:
6375 case -NFS4ERR_EXPIRED:
6376 case 0:
6377 return 0;
6378 }
6379 err = nfs4_handle_exception(server, err, &exception);
6380 } while (exception.retry);
6381 return err;
6382 }
6383
6384 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6385 {
6386 struct inode *inode = state->inode;
6387 struct nfs_server *server = NFS_SERVER(inode);
6388 struct nfs_client *clp = server->nfs_client;
6389 struct nfs_lockt_args arg = {
6390 .fh = NFS_FH(inode),
6391 .fl = request,
6392 };
6393 struct nfs_lockt_res res = {
6394 .denied = request,
6395 };
6396 struct rpc_message msg = {
6397 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6398 .rpc_argp = &arg,
6399 .rpc_resp = &res,
6400 .rpc_cred = state->owner->so_cred,
6401 };
6402 struct nfs4_lock_state *lsp;
6403 int status;
6404
6405 arg.lock_owner.clientid = clp->cl_clientid;
6406 status = nfs4_set_lock_state(state, request);
6407 if (status != 0)
6408 goto out;
6409 lsp = request->fl_u.nfs4_fl.owner;
6410 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6411 arg.lock_owner.s_dev = server->s_dev;
6412 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6413 switch (status) {
6414 case 0:
6415 request->fl_type = F_UNLCK;
6416 break;
6417 case -NFS4ERR_DENIED:
6418 status = 0;
6419 }
6420 request->fl_ops->fl_release_private(request);
6421 request->fl_ops = NULL;
6422 out:
6423 return status;
6424 }
6425
6426 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6427 {
6428 struct nfs4_exception exception = {
6429 .interruptible = true,
6430 };
6431 int err;
6432
6433 do {
6434 err = _nfs4_proc_getlk(state, cmd, request);
6435 trace_nfs4_get_lock(request, state, cmd, err);
6436 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6437 &exception);
6438 } while (exception.retry);
6439 return err;
6440 }
6441
6442
6443
6444
6445
6446 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6447 struct nfs4_lock_state *lsp)
6448 {
6449 struct nfs4_state *state = lsp->ls_state;
6450 bool ret = false;
6451
6452 spin_lock(&state->state_lock);
6453 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6454 goto out;
6455 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6456 nfs4_stateid_seqid_inc(dst);
6457 else
6458 dst->seqid = lsp->ls_stateid.seqid;
6459 ret = true;
6460 out:
6461 spin_unlock(&state->state_lock);
6462 return ret;
6463 }
6464
6465 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6466 struct nfs4_lock_state *lsp)
6467 {
6468 struct nfs4_state *state = lsp->ls_state;
6469 bool ret;
6470
6471 spin_lock(&state->state_lock);
6472 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6473 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6474 spin_unlock(&state->state_lock);
6475 return ret;
6476 }
6477
6478 struct nfs4_unlockdata {
6479 struct nfs_locku_args arg;
6480 struct nfs_locku_res res;
6481 struct nfs4_lock_state *lsp;
6482 struct nfs_open_context *ctx;
6483 struct nfs_lock_context *l_ctx;
6484 struct file_lock fl;
6485 struct nfs_server *server;
6486 unsigned long timestamp;
6487 };
6488
6489 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6490 struct nfs_open_context *ctx,
6491 struct nfs4_lock_state *lsp,
6492 struct nfs_seqid *seqid)
6493 {
6494 struct nfs4_unlockdata *p;
6495 struct nfs4_state *state = lsp->ls_state;
6496 struct inode *inode = state->inode;
6497
6498 p = kzalloc(sizeof(*p), GFP_NOFS);
6499 if (p == NULL)
6500 return NULL;
6501 p->arg.fh = NFS_FH(inode);
6502 p->arg.fl = &p->fl;
6503 p->arg.seqid = seqid;
6504 p->res.seqid = seqid;
6505 p->lsp = lsp;
6506
6507 p->ctx = get_nfs_open_context(ctx);
6508 p->l_ctx = nfs_get_lock_context(ctx);
6509 locks_init_lock(&p->fl);
6510 locks_copy_lock(&p->fl, fl);
6511 p->server = NFS_SERVER(inode);
6512 spin_lock(&state->state_lock);
6513 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6514 spin_unlock(&state->state_lock);
6515 return p;
6516 }
6517
6518 static void nfs4_locku_release_calldata(void *data)
6519 {
6520 struct nfs4_unlockdata *calldata = data;
6521 nfs_free_seqid(calldata->arg.seqid);
6522 nfs4_put_lock_state(calldata->lsp);
6523 nfs_put_lock_context(calldata->l_ctx);
6524 put_nfs_open_context(calldata->ctx);
6525 kfree(calldata);
6526 }
6527
6528 static void nfs4_locku_done(struct rpc_task *task, void *data)
6529 {
6530 struct nfs4_unlockdata *calldata = data;
6531 struct nfs4_exception exception = {
6532 .inode = calldata->lsp->ls_state->inode,
6533 .stateid = &calldata->arg.stateid,
6534 };
6535
6536 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6537 return;
6538 switch (task->tk_status) {
6539 case 0:
6540 renew_lease(calldata->server, calldata->timestamp);
6541 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6542 if (nfs4_update_lock_stateid(calldata->lsp,
6543 &calldata->res.stateid))
6544 break;
6545
6546 case -NFS4ERR_ADMIN_REVOKED:
6547 case -NFS4ERR_EXPIRED:
6548 nfs4_free_revoked_stateid(calldata->server,
6549 &calldata->arg.stateid,
6550 task->tk_msg.rpc_cred);
6551
6552 case -NFS4ERR_BAD_STATEID:
6553 case -NFS4ERR_STALE_STATEID:
6554 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6555 calldata->lsp))
6556 rpc_restart_call_prepare(task);
6557 break;
6558 case -NFS4ERR_OLD_STATEID:
6559 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6560 calldata->lsp))
6561 rpc_restart_call_prepare(task);
6562 break;
6563 default:
6564 task->tk_status = nfs4_async_handle_exception(task,
6565 calldata->server, task->tk_status,
6566 &exception);
6567 if (exception.retry)
6568 rpc_restart_call_prepare(task);
6569 }
6570 nfs_release_seqid(calldata->arg.seqid);
6571 }
6572
6573 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6574 {
6575 struct nfs4_unlockdata *calldata = data;
6576
6577 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6578 nfs_async_iocounter_wait(task, calldata->l_ctx))
6579 return;
6580
6581 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6582 goto out_wait;
6583 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6584
6585 goto out_no_action;
6586 }
6587 calldata->timestamp = jiffies;
6588 if (nfs4_setup_sequence(calldata->server->nfs_client,
6589 &calldata->arg.seq_args,
6590 &calldata->res.seq_res,
6591 task) != 0)
6592 nfs_release_seqid(calldata->arg.seqid);
6593 return;
6594 out_no_action:
6595 task->tk_action = NULL;
6596 out_wait:
6597 nfs4_sequence_done(task, &calldata->res.seq_res);
6598 }
6599
6600 static const struct rpc_call_ops nfs4_locku_ops = {
6601 .rpc_call_prepare = nfs4_locku_prepare,
6602 .rpc_call_done = nfs4_locku_done,
6603 .rpc_release = nfs4_locku_release_calldata,
6604 };
6605
6606 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6607 struct nfs_open_context *ctx,
6608 struct nfs4_lock_state *lsp,
6609 struct nfs_seqid *seqid)
6610 {
6611 struct nfs4_unlockdata *data;
6612 struct rpc_message msg = {
6613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6614 .rpc_cred = ctx->cred,
6615 };
6616 struct rpc_task_setup task_setup_data = {
6617 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6618 .rpc_message = &msg,
6619 .callback_ops = &nfs4_locku_ops,
6620 .workqueue = nfsiod_workqueue,
6621 .flags = RPC_TASK_ASYNC,
6622 };
6623
6624 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6625 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6626
6627
6628
6629
6630 fl->fl_type = F_UNLCK;
6631 if (fl->fl_flags & FL_CLOSE)
6632 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6633
6634 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6635 if (data == NULL) {
6636 nfs_free_seqid(seqid);
6637 return ERR_PTR(-ENOMEM);
6638 }
6639
6640 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6641 msg.rpc_argp = &data->arg;
6642 msg.rpc_resp = &data->res;
6643 task_setup_data.callback_data = data;
6644 return rpc_run_task(&task_setup_data);
6645 }
6646
6647 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6648 {
6649 struct inode *inode = state->inode;
6650 struct nfs4_state_owner *sp = state->owner;
6651 struct nfs_inode *nfsi = NFS_I(inode);
6652 struct nfs_seqid *seqid;
6653 struct nfs4_lock_state *lsp;
6654 struct rpc_task *task;
6655 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6656 int status = 0;
6657 unsigned char fl_flags = request->fl_flags;
6658
6659 status = nfs4_set_lock_state(state, request);
6660
6661 request->fl_flags |= FL_EXISTS;
6662
6663 mutex_lock(&sp->so_delegreturn_mutex);
6664
6665 down_read(&nfsi->rwsem);
6666 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6667 up_read(&nfsi->rwsem);
6668 mutex_unlock(&sp->so_delegreturn_mutex);
6669 goto out;
6670 }
6671 up_read(&nfsi->rwsem);
6672 mutex_unlock(&sp->so_delegreturn_mutex);
6673 if (status != 0)
6674 goto out;
6675
6676 lsp = request->fl_u.nfs4_fl.owner;
6677 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6678 goto out;
6679 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6680 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6681 status = -ENOMEM;
6682 if (IS_ERR(seqid))
6683 goto out;
6684 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6685 status = PTR_ERR(task);
6686 if (IS_ERR(task))
6687 goto out;
6688 status = rpc_wait_for_completion_task(task);
6689 rpc_put_task(task);
6690 out:
6691 request->fl_flags = fl_flags;
6692 trace_nfs4_unlock(request, state, F_SETLK, status);
6693 return status;
6694 }
6695
6696 struct nfs4_lockdata {
6697 struct nfs_lock_args arg;
6698 struct nfs_lock_res res;
6699 struct nfs4_lock_state *lsp;
6700 struct nfs_open_context *ctx;
6701 struct file_lock fl;
6702 unsigned long timestamp;
6703 int rpc_status;
6704 int cancelled;
6705 struct nfs_server *server;
6706 };
6707
6708 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6709 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6710 gfp_t gfp_mask)
6711 {
6712 struct nfs4_lockdata *p;
6713 struct inode *inode = lsp->ls_state->inode;
6714 struct nfs_server *server = NFS_SERVER(inode);
6715 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6716
6717 p = kzalloc(sizeof(*p), gfp_mask);
6718 if (p == NULL)
6719 return NULL;
6720
6721 p->arg.fh = NFS_FH(inode);
6722 p->arg.fl = &p->fl;
6723 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6724 if (IS_ERR(p->arg.open_seqid))
6725 goto out_free;
6726 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6727 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6728 if (IS_ERR(p->arg.lock_seqid))
6729 goto out_free_seqid;
6730 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6731 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6732 p->arg.lock_owner.s_dev = server->s_dev;
6733 p->res.lock_seqid = p->arg.lock_seqid;
6734 p->lsp = lsp;
6735 p->server = server;
6736 p->ctx = get_nfs_open_context(ctx);
6737 locks_init_lock(&p->fl);
6738 locks_copy_lock(&p->fl, fl);
6739 return p;
6740 out_free_seqid:
6741 nfs_free_seqid(p->arg.open_seqid);
6742 out_free:
6743 kfree(p);
6744 return NULL;
6745 }
6746
6747 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6748 {
6749 struct nfs4_lockdata *data = calldata;
6750 struct nfs4_state *state = data->lsp->ls_state;
6751
6752 dprintk("%s: begin!\n", __func__);
6753 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6754 goto out_wait;
6755
6756 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6757 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6758 goto out_release_lock_seqid;
6759 }
6760 nfs4_stateid_copy(&data->arg.open_stateid,
6761 &state->open_stateid);
6762 data->arg.new_lock_owner = 1;
6763 data->res.open_seqid = data->arg.open_seqid;
6764 } else {
6765 data->arg.new_lock_owner = 0;
6766 nfs4_stateid_copy(&data->arg.lock_stateid,
6767 &data->lsp->ls_stateid);
6768 }
6769 if (!nfs4_valid_open_stateid(state)) {
6770 data->rpc_status = -EBADF;
6771 task->tk_action = NULL;
6772 goto out_release_open_seqid;
6773 }
6774 data->timestamp = jiffies;
6775 if (nfs4_setup_sequence(data->server->nfs_client,
6776 &data->arg.seq_args,
6777 &data->res.seq_res,
6778 task) == 0)
6779 return;
6780 out_release_open_seqid:
6781 nfs_release_seqid(data->arg.open_seqid);
6782 out_release_lock_seqid:
6783 nfs_release_seqid(data->arg.lock_seqid);
6784 out_wait:
6785 nfs4_sequence_done(task, &data->res.seq_res);
6786 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6787 }
6788
6789 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6790 {
6791 struct nfs4_lockdata *data = calldata;
6792 struct nfs4_lock_state *lsp = data->lsp;
6793
6794 dprintk("%s: begin!\n", __func__);
6795
6796 if (!nfs4_sequence_done(task, &data->res.seq_res))
6797 return;
6798
6799 data->rpc_status = task->tk_status;
6800 switch (task->tk_status) {
6801 case 0:
6802 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6803 data->timestamp);
6804 if (data->arg.new_lock && !data->cancelled) {
6805 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6806 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
6807 goto out_restart;
6808 }
6809 if (data->arg.new_lock_owner != 0) {
6810 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6811 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6812 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6813 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6814 goto out_restart;
6815 break;
6816 case -NFS4ERR_BAD_STATEID:
6817 case -NFS4ERR_OLD_STATEID:
6818 case -NFS4ERR_STALE_STATEID:
6819 case -NFS4ERR_EXPIRED:
6820 if (data->arg.new_lock_owner != 0) {
6821 if (!nfs4_stateid_match(&data->arg.open_stateid,
6822 &lsp->ls_state->open_stateid))
6823 goto out_restart;
6824 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6825 &lsp->ls_stateid))
6826 goto out_restart;
6827 }
6828 out_done:
6829 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6830 return;
6831 out_restart:
6832 if (!data->cancelled)
6833 rpc_restart_call_prepare(task);
6834 goto out_done;
6835 }
6836
6837 static void nfs4_lock_release(void *calldata)
6838 {
6839 struct nfs4_lockdata *data = calldata;
6840
6841 dprintk("%s: begin!\n", __func__);
6842 nfs_free_seqid(data->arg.open_seqid);
6843 if (data->cancelled && data->rpc_status == 0) {
6844 struct rpc_task *task;
6845 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6846 data->arg.lock_seqid);
6847 if (!IS_ERR(task))
6848 rpc_put_task_async(task);
6849 dprintk("%s: cancelling lock!\n", __func__);
6850 } else
6851 nfs_free_seqid(data->arg.lock_seqid);
6852 nfs4_put_lock_state(data->lsp);
6853 put_nfs_open_context(data->ctx);
6854 kfree(data);
6855 dprintk("%s: done!\n", __func__);
6856 }
6857
6858 static const struct rpc_call_ops nfs4_lock_ops = {
6859 .rpc_call_prepare = nfs4_lock_prepare,
6860 .rpc_call_done = nfs4_lock_done,
6861 .rpc_release = nfs4_lock_release,
6862 };
6863
6864 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6865 {
6866 switch (error) {
6867 case -NFS4ERR_ADMIN_REVOKED:
6868 case -NFS4ERR_EXPIRED:
6869 case -NFS4ERR_BAD_STATEID:
6870 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6871 if (new_lock_owner != 0 ||
6872 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6873 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6874 break;
6875 case -NFS4ERR_STALE_STATEID:
6876 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6877 nfs4_schedule_lease_recovery(server->nfs_client);
6878 };
6879 }
6880
6881 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6882 {
6883 struct nfs4_lockdata *data;
6884 struct rpc_task *task;
6885 struct rpc_message msg = {
6886 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6887 .rpc_cred = state->owner->so_cred,
6888 };
6889 struct rpc_task_setup task_setup_data = {
6890 .rpc_client = NFS_CLIENT(state->inode),
6891 .rpc_message = &msg,
6892 .callback_ops = &nfs4_lock_ops,
6893 .workqueue = nfsiod_workqueue,
6894 .flags = RPC_TASK_ASYNC,
6895 };
6896 int ret;
6897
6898 dprintk("%s: begin!\n", __func__);
6899 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6900 fl->fl_u.nfs4_fl.owner,
6901 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6902 if (data == NULL)
6903 return -ENOMEM;
6904 if (IS_SETLKW(cmd))
6905 data->arg.block = 1;
6906 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6907 recovery_type > NFS_LOCK_NEW);
6908 msg.rpc_argp = &data->arg;
6909 msg.rpc_resp = &data->res;
6910 task_setup_data.callback_data = data;
6911 if (recovery_type > NFS_LOCK_NEW) {
6912 if (recovery_type == NFS_LOCK_RECLAIM)
6913 data->arg.reclaim = NFS_LOCK_RECLAIM;
6914 } else
6915 data->arg.new_lock = 1;
6916 task = rpc_run_task(&task_setup_data);
6917 if (IS_ERR(task))
6918 return PTR_ERR(task);
6919 ret = rpc_wait_for_completion_task(task);
6920 if (ret == 0) {
6921 ret = data->rpc_status;
6922 if (ret)
6923 nfs4_handle_setlk_error(data->server, data->lsp,
6924 data->arg.new_lock_owner, ret);
6925 } else
6926 data->cancelled = true;
6927 rpc_put_task(task);
6928 dprintk("%s: done, ret = %d!\n", __func__, ret);
6929 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6930 return ret;
6931 }
6932
6933 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6934 {
6935 struct nfs_server *server = NFS_SERVER(state->inode);
6936 struct nfs4_exception exception = {
6937 .inode = state->inode,
6938 };
6939 int err;
6940
6941 do {
6942
6943 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6944 return 0;
6945 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6946 if (err != -NFS4ERR_DELAY)
6947 break;
6948 nfs4_handle_exception(server, err, &exception);
6949 } while (exception.retry);
6950 return err;
6951 }
6952
6953 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6954 {
6955 struct nfs_server *server = NFS_SERVER(state->inode);
6956 struct nfs4_exception exception = {
6957 .inode = state->inode,
6958 };
6959 int err;
6960
6961 err = nfs4_set_lock_state(state, request);
6962 if (err != 0)
6963 return err;
6964 if (!recover_lost_locks) {
6965 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6966 return 0;
6967 }
6968 do {
6969 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6970 return 0;
6971 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6972 switch (err) {
6973 default:
6974 goto out;
6975 case -NFS4ERR_GRACE:
6976 case -NFS4ERR_DELAY:
6977 nfs4_handle_exception(server, err, &exception);
6978 err = 0;
6979 }
6980 } while (exception.retry);
6981 out:
6982 return err;
6983 }
6984
6985 #if defined(CONFIG_NFS_V4_1)
6986 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6987 {
6988 struct nfs4_lock_state *lsp;
6989 int status;
6990
6991 status = nfs4_set_lock_state(state, request);
6992 if (status != 0)
6993 return status;
6994 lsp = request->fl_u.nfs4_fl.owner;
6995 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6996 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6997 return 0;
6998 return nfs4_lock_expired(state, request);
6999 }
7000 #endif
7001
7002 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7003 {
7004 struct nfs_inode *nfsi = NFS_I(state->inode);
7005 struct nfs4_state_owner *sp = state->owner;
7006 unsigned char fl_flags = request->fl_flags;
7007 int status;
7008
7009 request->fl_flags |= FL_ACCESS;
7010 status = locks_lock_inode_wait(state->inode, request);
7011 if (status < 0)
7012 goto out;
7013 mutex_lock(&sp->so_delegreturn_mutex);
7014 down_read(&nfsi->rwsem);
7015 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7016
7017
7018 request->fl_flags = fl_flags & ~FL_SLEEP;
7019 status = locks_lock_inode_wait(state->inode, request);
7020 up_read(&nfsi->rwsem);
7021 mutex_unlock(&sp->so_delegreturn_mutex);
7022 goto out;
7023 }
7024 up_read(&nfsi->rwsem);
7025 mutex_unlock(&sp->so_delegreturn_mutex);
7026 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7027 out:
7028 request->fl_flags = fl_flags;
7029 return status;
7030 }
7031
7032 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7033 {
7034 struct nfs4_exception exception = {
7035 .state = state,
7036 .inode = state->inode,
7037 .interruptible = true,
7038 };
7039 int err;
7040
7041 do {
7042 err = _nfs4_proc_setlk(state, cmd, request);
7043 if (err == -NFS4ERR_DENIED)
7044 err = -EAGAIN;
7045 err = nfs4_handle_exception(NFS_SERVER(state->inode),
7046 err, &exception);
7047 } while (exception.retry);
7048 return err;
7049 }
7050
7051 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7052 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7053
7054 static int
7055 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7056 struct file_lock *request)
7057 {
7058 int status = -ERESTARTSYS;
7059 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7060
7061 while(!signalled()) {
7062 status = nfs4_proc_setlk(state, cmd, request);
7063 if ((status != -EAGAIN) || IS_SETLK(cmd))
7064 break;
7065 freezable_schedule_timeout_interruptible(timeout);
7066 timeout *= 2;
7067 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7068 status = -ERESTARTSYS;
7069 }
7070 return status;
7071 }
7072
7073 #ifdef CONFIG_NFS_V4_1
7074 struct nfs4_lock_waiter {
7075 struct task_struct *task;
7076 struct inode *inode;
7077 struct nfs_lowner *owner;
7078 };
7079
7080 static int
7081 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7082 {
7083 int ret;
7084 struct nfs4_lock_waiter *waiter = wait->private;
7085
7086
7087 if (key) {
7088 struct cb_notify_lock_args *cbnl = key;
7089 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7090 *wowner = waiter->owner;
7091
7092
7093 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7094 return 0;
7095
7096
7097 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7098 return 0;
7099 }
7100
7101
7102 wait->private = waiter->task;
7103 ret = woken_wake_function(wait, mode, flags, key);
7104 if (ret)
7105 list_del_init(&wait->entry);
7106 wait->private = waiter;
7107 return ret;
7108 }
7109
7110 static int
7111 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7112 {
7113 int status = -ERESTARTSYS;
7114 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7115 struct nfs_server *server = NFS_SERVER(state->inode);
7116 struct nfs_client *clp = server->nfs_client;
7117 wait_queue_head_t *q = &clp->cl_lock_waitq;
7118 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7119 .id = lsp->ls_seqid.owner_id,
7120 .s_dev = server->s_dev };
7121 struct nfs4_lock_waiter waiter = { .task = current,
7122 .inode = state->inode,
7123 .owner = &owner};
7124 wait_queue_entry_t wait;
7125
7126
7127 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7128 return nfs4_retry_setlk_simple(state, cmd, request);
7129
7130 init_wait(&wait);
7131 wait.private = &waiter;
7132 wait.func = nfs4_wake_lock_waiter;
7133
7134 while(!signalled()) {
7135 add_wait_queue(q, &wait);
7136 status = nfs4_proc_setlk(state, cmd, request);
7137 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7138 finish_wait(q, &wait);
7139 break;
7140 }
7141
7142 status = -ERESTARTSYS;
7143 freezer_do_not_count();
7144 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7145 freezer_count();
7146 finish_wait(q, &wait);
7147 }
7148
7149 return status;
7150 }
7151 #else
7152 static inline int
7153 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7154 {
7155 return nfs4_retry_setlk_simple(state, cmd, request);
7156 }
7157 #endif
7158
7159 static int
7160 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7161 {
7162 struct nfs_open_context *ctx;
7163 struct nfs4_state *state;
7164 int status;
7165
7166
7167 ctx = nfs_file_open_context(filp);
7168 state = ctx->state;
7169
7170 if (IS_GETLK(cmd)) {
7171 if (state != NULL)
7172 return nfs4_proc_getlk(state, F_GETLK, request);
7173 return 0;
7174 }
7175
7176 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7177 return -EINVAL;
7178
7179 if (request->fl_type == F_UNLCK) {
7180 if (state != NULL)
7181 return nfs4_proc_unlck(state, cmd, request);
7182 return 0;
7183 }
7184
7185 if (state == NULL)
7186 return -ENOLCK;
7187
7188 if ((request->fl_flags & FL_POSIX) &&
7189 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7190 return -ENOLCK;
7191
7192
7193
7194
7195
7196 switch (request->fl_type) {
7197 case F_RDLCK:
7198 if (!(filp->f_mode & FMODE_READ))
7199 return -EBADF;
7200 break;
7201 case F_WRLCK:
7202 if (!(filp->f_mode & FMODE_WRITE))
7203 return -EBADF;
7204 }
7205
7206 status = nfs4_set_lock_state(state, request);
7207 if (status != 0)
7208 return status;
7209
7210 return nfs4_retry_setlk(state, cmd, request);
7211 }
7212
7213 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7214 {
7215 struct nfs_server *server = NFS_SERVER(state->inode);
7216 int err;
7217
7218 err = nfs4_set_lock_state(state, fl);
7219 if (err != 0)
7220 return err;
7221 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7222 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7223 }
7224
7225 struct nfs_release_lockowner_data {
7226 struct nfs4_lock_state *lsp;
7227 struct nfs_server *server;
7228 struct nfs_release_lockowner_args args;
7229 struct nfs_release_lockowner_res res;
7230 unsigned long timestamp;
7231 };
7232
7233 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7234 {
7235 struct nfs_release_lockowner_data *data = calldata;
7236 struct nfs_server *server = data->server;
7237 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7238 &data->res.seq_res, task);
7239 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7240 data->timestamp = jiffies;
7241 }
7242
7243 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7244 {
7245 struct nfs_release_lockowner_data *data = calldata;
7246 struct nfs_server *server = data->server;
7247
7248 nfs40_sequence_done(task, &data->res.seq_res);
7249
7250 switch (task->tk_status) {
7251 case 0:
7252 renew_lease(server, data->timestamp);
7253 break;
7254 case -NFS4ERR_STALE_CLIENTID:
7255 case -NFS4ERR_EXPIRED:
7256 nfs4_schedule_lease_recovery(server->nfs_client);
7257 break;
7258 case -NFS4ERR_LEASE_MOVED:
7259 case -NFS4ERR_DELAY:
7260 if (nfs4_async_handle_error(task, server,
7261 NULL, NULL) == -EAGAIN)
7262 rpc_restart_call_prepare(task);
7263 }
7264 }
7265
7266 static void nfs4_release_lockowner_release(void *calldata)
7267 {
7268 struct nfs_release_lockowner_data *data = calldata;
7269 nfs4_free_lock_state(data->server, data->lsp);
7270 kfree(calldata);
7271 }
7272
7273 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7274 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7275 .rpc_call_done = nfs4_release_lockowner_done,
7276 .rpc_release = nfs4_release_lockowner_release,
7277 };
7278
7279 static void
7280 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7281 {
7282 struct nfs_release_lockowner_data *data;
7283 struct rpc_message msg = {
7284 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7285 };
7286
7287 if (server->nfs_client->cl_mvops->minor_version != 0)
7288 return;
7289
7290 data = kmalloc(sizeof(*data), GFP_NOFS);
7291 if (!data)
7292 return;
7293 data->lsp = lsp;
7294 data->server = server;
7295 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7296 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7297 data->args.lock_owner.s_dev = server->s_dev;
7298
7299 msg.rpc_argp = &data->args;
7300 msg.rpc_resp = &data->res;
7301 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7302 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7303 }
7304
7305 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7306
7307 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7308 struct dentry *unused, struct inode *inode,
7309 const char *key, const void *buf,
7310 size_t buflen, int flags)
7311 {
7312 return nfs4_proc_set_acl(inode, buf, buflen);
7313 }
7314
7315 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7316 struct dentry *unused, struct inode *inode,
7317 const char *key, void *buf, size_t buflen)
7318 {
7319 return nfs4_proc_get_acl(inode, buf, buflen);
7320 }
7321
7322 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7323 {
7324 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7325 }
7326
7327 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7328
7329 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7330 struct dentry *unused, struct inode *inode,
7331 const char *key, const void *buf,
7332 size_t buflen, int flags)
7333 {
7334 if (security_ismaclabel(key))
7335 return nfs4_set_security_label(inode, buf, buflen);
7336
7337 return -EOPNOTSUPP;
7338 }
7339
7340 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7341 struct dentry *unused, struct inode *inode,
7342 const char *key, void *buf, size_t buflen)
7343 {
7344 if (security_ismaclabel(key))
7345 return nfs4_get_security_label(inode, buf, buflen);
7346 return -EOPNOTSUPP;
7347 }
7348
7349 static ssize_t
7350 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7351 {
7352 int len = 0;
7353
7354 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7355 len = security_inode_listsecurity(inode, list, list_len);
7356 if (list_len && len > list_len)
7357 return -ERANGE;
7358 }
7359 return len;
7360 }
7361
7362 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7363 .prefix = XATTR_SECURITY_PREFIX,
7364 .get = nfs4_xattr_get_nfs4_label,
7365 .set = nfs4_xattr_set_nfs4_label,
7366 };
7367
7368 #else
7369
7370 static ssize_t
7371 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7372 {
7373 return 0;
7374 }
7375
7376 #endif
7377
7378
7379
7380
7381 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7382 {
7383 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7384 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7385 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7386 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7387 return;
7388
7389 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7390 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7391 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7392 fattr->nlink = 2;
7393 }
7394
7395 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7396 const struct qstr *name,
7397 struct nfs4_fs_locations *fs_locations,
7398 struct page *page)
7399 {
7400 struct nfs_server *server = NFS_SERVER(dir);
7401 u32 bitmask[3];
7402 struct nfs4_fs_locations_arg args = {
7403 .dir_fh = NFS_FH(dir),
7404 .name = name,
7405 .page = page,
7406 .bitmask = bitmask,
7407 };
7408 struct nfs4_fs_locations_res res = {
7409 .fs_locations = fs_locations,
7410 };
7411 struct rpc_message msg = {
7412 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7413 .rpc_argp = &args,
7414 .rpc_resp = &res,
7415 };
7416 int status;
7417
7418 dprintk("%s: start\n", __func__);
7419
7420 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7421 bitmask[1] = nfs4_fattr_bitmap[1];
7422
7423
7424
7425 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7426 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7427 else
7428 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7429
7430 nfs_fattr_init(&fs_locations->fattr);
7431 fs_locations->server = server;
7432 fs_locations->nlocations = 0;
7433 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7434 dprintk("%s: returned status = %d\n", __func__, status);
7435 return status;
7436 }
7437
7438 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7439 const struct qstr *name,
7440 struct nfs4_fs_locations *fs_locations,
7441 struct page *page)
7442 {
7443 struct nfs4_exception exception = {
7444 .interruptible = true,
7445 };
7446 int err;
7447 do {
7448 err = _nfs4_proc_fs_locations(client, dir, name,
7449 fs_locations, page);
7450 trace_nfs4_get_fs_locations(dir, name, err);
7451 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7452 &exception);
7453 } while (exception.retry);
7454 return err;
7455 }
7456
7457
7458
7459
7460
7461
7462
7463
7464 static int _nfs40_proc_get_locations(struct inode *inode,
7465 struct nfs4_fs_locations *locations,
7466 struct page *page, const struct cred *cred)
7467 {
7468 struct nfs_server *server = NFS_SERVER(inode);
7469 struct rpc_clnt *clnt = server->client;
7470 u32 bitmask[2] = {
7471 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7472 };
7473 struct nfs4_fs_locations_arg args = {
7474 .clientid = server->nfs_client->cl_clientid,
7475 .fh = NFS_FH(inode),
7476 .page = page,
7477 .bitmask = bitmask,
7478 .migration = 1,
7479 .renew = 1,
7480 };
7481 struct nfs4_fs_locations_res res = {
7482 .fs_locations = locations,
7483 .migration = 1,
7484 .renew = 1,
7485 };
7486 struct rpc_message msg = {
7487 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7488 .rpc_argp = &args,
7489 .rpc_resp = &res,
7490 .rpc_cred = cred,
7491 };
7492 unsigned long now = jiffies;
7493 int status;
7494
7495 nfs_fattr_init(&locations->fattr);
7496 locations->server = server;
7497 locations->nlocations = 0;
7498
7499 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7500 status = nfs4_call_sync_sequence(clnt, server, &msg,
7501 &args.seq_args, &res.seq_res);
7502 if (status)
7503 return status;
7504
7505 renew_lease(server, now);
7506 return 0;
7507 }
7508
7509 #ifdef CONFIG_NFS_V4_1
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521 static int _nfs41_proc_get_locations(struct inode *inode,
7522 struct nfs4_fs_locations *locations,
7523 struct page *page, const struct cred *cred)
7524 {
7525 struct nfs_server *server = NFS_SERVER(inode);
7526 struct rpc_clnt *clnt = server->client;
7527 u32 bitmask[2] = {
7528 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7529 };
7530 struct nfs4_fs_locations_arg args = {
7531 .fh = NFS_FH(inode),
7532 .page = page,
7533 .bitmask = bitmask,
7534 .migration = 1,
7535 };
7536 struct nfs4_fs_locations_res res = {
7537 .fs_locations = locations,
7538 .migration = 1,
7539 };
7540 struct rpc_message msg = {
7541 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7542 .rpc_argp = &args,
7543 .rpc_resp = &res,
7544 .rpc_cred = cred,
7545 };
7546 int status;
7547
7548 nfs_fattr_init(&locations->fattr);
7549 locations->server = server;
7550 locations->nlocations = 0;
7551
7552 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7553 status = nfs4_call_sync_sequence(clnt, server, &msg,
7554 &args.seq_args, &res.seq_res);
7555 if (status == NFS4_OK &&
7556 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7557 status = -NFS4ERR_LEASE_MOVED;
7558 return status;
7559 }
7560
7561 #endif
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580 int nfs4_proc_get_locations(struct inode *inode,
7581 struct nfs4_fs_locations *locations,
7582 struct page *page, const struct cred *cred)
7583 {
7584 struct nfs_server *server = NFS_SERVER(inode);
7585 struct nfs_client *clp = server->nfs_client;
7586 const struct nfs4_mig_recovery_ops *ops =
7587 clp->cl_mvops->mig_recovery_ops;
7588 struct nfs4_exception exception = {
7589 .interruptible = true,
7590 };
7591 int status;
7592
7593 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7594 (unsigned long long)server->fsid.major,
7595 (unsigned long long)server->fsid.minor,
7596 clp->cl_hostname);
7597 nfs_display_fhandle(NFS_FH(inode), __func__);
7598
7599 do {
7600 status = ops->get_locations(inode, locations, page, cred);
7601 if (status != -NFS4ERR_DELAY)
7602 break;
7603 nfs4_handle_exception(server, status, &exception);
7604 } while (exception.retry);
7605 return status;
7606 }
7607
7608
7609
7610
7611
7612
7613
7614
7615 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
7616 {
7617 struct nfs_server *server = NFS_SERVER(inode);
7618 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7619 struct rpc_clnt *clnt = server->client;
7620 struct nfs4_fsid_present_arg args = {
7621 .fh = NFS_FH(inode),
7622 .clientid = clp->cl_clientid,
7623 .renew = 1,
7624 };
7625 struct nfs4_fsid_present_res res = {
7626 .renew = 1,
7627 };
7628 struct rpc_message msg = {
7629 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7630 .rpc_argp = &args,
7631 .rpc_resp = &res,
7632 .rpc_cred = cred,
7633 };
7634 unsigned long now = jiffies;
7635 int status;
7636
7637 res.fh = nfs_alloc_fhandle();
7638 if (res.fh == NULL)
7639 return -ENOMEM;
7640
7641 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7642 status = nfs4_call_sync_sequence(clnt, server, &msg,
7643 &args.seq_args, &res.seq_res);
7644 nfs_free_fhandle(res.fh);
7645 if (status)
7646 return status;
7647
7648 do_renew_lease(clp, now);
7649 return 0;
7650 }
7651
7652 #ifdef CONFIG_NFS_V4_1
7653
7654
7655
7656
7657
7658
7659
7660
7661 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
7662 {
7663 struct nfs_server *server = NFS_SERVER(inode);
7664 struct rpc_clnt *clnt = server->client;
7665 struct nfs4_fsid_present_arg args = {
7666 .fh = NFS_FH(inode),
7667 };
7668 struct nfs4_fsid_present_res res = {
7669 };
7670 struct rpc_message msg = {
7671 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7672 .rpc_argp = &args,
7673 .rpc_resp = &res,
7674 .rpc_cred = cred,
7675 };
7676 int status;
7677
7678 res.fh = nfs_alloc_fhandle();
7679 if (res.fh == NULL)
7680 return -ENOMEM;
7681
7682 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7683 status = nfs4_call_sync_sequence(clnt, server, &msg,
7684 &args.seq_args, &res.seq_res);
7685 nfs_free_fhandle(res.fh);
7686 if (status == NFS4_OK &&
7687 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7688 status = -NFS4ERR_LEASE_MOVED;
7689 return status;
7690 }
7691
7692 #endif
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
7709 {
7710 struct nfs_server *server = NFS_SERVER(inode);
7711 struct nfs_client *clp = server->nfs_client;
7712 const struct nfs4_mig_recovery_ops *ops =
7713 clp->cl_mvops->mig_recovery_ops;
7714 struct nfs4_exception exception = {
7715 .interruptible = true,
7716 };
7717 int status;
7718
7719 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7720 (unsigned long long)server->fsid.major,
7721 (unsigned long long)server->fsid.minor,
7722 clp->cl_hostname);
7723 nfs_display_fhandle(NFS_FH(inode), __func__);
7724
7725 do {
7726 status = ops->fsid_present(inode, cred);
7727 if (status != -NFS4ERR_DELAY)
7728 break;
7729 nfs4_handle_exception(server, status, &exception);
7730 } while (exception.retry);
7731 return status;
7732 }
7733
7734
7735
7736
7737
7738
7739
7740
7741 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7742 {
7743 int status;
7744 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7745 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
7746 struct nfs4_secinfo_arg args = {
7747 .dir_fh = NFS_FH(dir),
7748 .name = name,
7749 };
7750 struct nfs4_secinfo_res res = {
7751 .flavors = flavors,
7752 };
7753 struct rpc_message msg = {
7754 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7755 .rpc_argp = &args,
7756 .rpc_resp = &res,
7757 };
7758 struct nfs4_call_sync_data data = {
7759 .seq_server = NFS_SERVER(dir),
7760 .seq_args = &args.seq_args,
7761 .seq_res = &res.seq_res,
7762 };
7763 struct rpc_task_setup task_setup = {
7764 .rpc_client = clnt,
7765 .rpc_message = &msg,
7766 .callback_ops = clp->cl_mvops->call_sync_ops,
7767 .callback_data = &data,
7768 .flags = RPC_TASK_NO_ROUND_ROBIN,
7769 };
7770 const struct cred *cred = NULL;
7771
7772 if (use_integrity) {
7773 clnt = clp->cl_rpcclient;
7774 task_setup.rpc_client = clnt;
7775
7776 cred = nfs4_get_clid_cred(clp);
7777 msg.rpc_cred = cred;
7778 }
7779
7780 dprintk("NFS call secinfo %s\n", name->name);
7781
7782 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7783 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
7784 status = nfs4_call_sync_custom(&task_setup);
7785
7786 dprintk("NFS reply secinfo: %d\n", status);
7787
7788 put_cred(cred);
7789 return status;
7790 }
7791
7792 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7793 struct nfs4_secinfo_flavors *flavors)
7794 {
7795 struct nfs4_exception exception = {
7796 .interruptible = true,
7797 };
7798 int err;
7799 do {
7800 err = -NFS4ERR_WRONGSEC;
7801
7802
7803 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7804 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7805
7806
7807
7808
7809
7810
7811
7812 if (err == -NFS4ERR_WRONGSEC)
7813 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7814
7815 trace_nfs4_secinfo(dir, name, err);
7816 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7817 &exception);
7818 } while (exception.retry);
7819 return err;
7820 }
7821
7822 #ifdef CONFIG_NFS_V4_1
7823
7824
7825
7826
7827
7828 static int nfs4_check_cl_exchange_flags(u32 flags)
7829 {
7830 if (flags & ~EXCHGID4_FLAG_MASK_R)
7831 goto out_inval;
7832 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7833 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7834 goto out_inval;
7835 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7836 goto out_inval;
7837 return NFS_OK;
7838 out_inval:
7839 return -NFS4ERR_INVAL;
7840 }
7841
7842 static bool
7843 nfs41_same_server_scope(struct nfs41_server_scope *a,
7844 struct nfs41_server_scope *b)
7845 {
7846 if (a->server_scope_sz != b->server_scope_sz)
7847 return false;
7848 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
7849 }
7850
7851 static void
7852 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7853 {
7854 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
7855 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
7856 struct nfs_client *clp = args->client;
7857
7858 switch (task->tk_status) {
7859 case -NFS4ERR_BADSESSION:
7860 case -NFS4ERR_DEADSESSION:
7861 nfs4_schedule_session_recovery(clp->cl_session,
7862 task->tk_status);
7863 }
7864 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
7865 res->dir != NFS4_CDFS4_BOTH) {
7866 rpc_task_close_connection(task);
7867 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
7868 rpc_restart_call(task);
7869 }
7870 }
7871
7872 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7873 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7874 };
7875
7876
7877
7878
7879
7880
7881
7882 static
7883 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7884 struct rpc_xprt *xprt,
7885 struct nfs_client *clp,
7886 const struct cred *cred)
7887 {
7888 int status;
7889 struct nfs41_bind_conn_to_session_args args = {
7890 .client = clp,
7891 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7892 .retries = 0,
7893 };
7894 struct nfs41_bind_conn_to_session_res res;
7895 struct rpc_message msg = {
7896 .rpc_proc =
7897 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7898 .rpc_argp = &args,
7899 .rpc_resp = &res,
7900 .rpc_cred = cred,
7901 };
7902 struct rpc_task_setup task_setup_data = {
7903 .rpc_client = clnt,
7904 .rpc_xprt = xprt,
7905 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7906 .rpc_message = &msg,
7907 .flags = RPC_TASK_TIMEOUT,
7908 };
7909 struct rpc_task *task;
7910
7911 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7912 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7913 args.dir = NFS4_CDFC4_FORE;
7914
7915
7916 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7917 args.dir = NFS4_CDFC4_FORE;
7918
7919 task = rpc_run_task(&task_setup_data);
7920 if (!IS_ERR(task)) {
7921 status = task->tk_status;
7922 rpc_put_task(task);
7923 } else
7924 status = PTR_ERR(task);
7925 trace_nfs4_bind_conn_to_session(clp, status);
7926 if (status == 0) {
7927 if (memcmp(res.sessionid.data,
7928 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7929 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7930 return -EIO;
7931 }
7932 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7933 dprintk("NFS: %s: Unexpected direction from server\n",
7934 __func__);
7935 return -EIO;
7936 }
7937 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7938 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7939 __func__);
7940 return -EIO;
7941 }
7942 }
7943
7944 return status;
7945 }
7946
7947 struct rpc_bind_conn_calldata {
7948 struct nfs_client *clp;
7949 const struct cred *cred;
7950 };
7951
7952 static int
7953 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7954 struct rpc_xprt *xprt,
7955 void *calldata)
7956 {
7957 struct rpc_bind_conn_calldata *p = calldata;
7958
7959 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7960 }
7961
7962 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
7963 {
7964 struct rpc_bind_conn_calldata data = {
7965 .clp = clp,
7966 .cred = cred,
7967 };
7968 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7969 nfs4_proc_bind_conn_to_session_callback, &data);
7970 }
7971
7972
7973
7974
7975
7976 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7977 .how = SP4_MACH_CRED,
7978 .enforce.u.words = {
7979 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7980 1 << (OP_EXCHANGE_ID - 32) |
7981 1 << (OP_CREATE_SESSION - 32) |
7982 1 << (OP_DESTROY_SESSION - 32) |
7983 1 << (OP_DESTROY_CLIENTID - 32)
7984 },
7985 .allow.u.words = {
7986 [0] = 1 << (OP_CLOSE) |
7987 1 << (OP_OPEN_DOWNGRADE) |
7988 1 << (OP_LOCKU) |
7989 1 << (OP_DELEGRETURN) |
7990 1 << (OP_COMMIT),
7991 [1] = 1 << (OP_SECINFO - 32) |
7992 1 << (OP_SECINFO_NO_NAME - 32) |
7993 1 << (OP_LAYOUTRETURN - 32) |
7994 1 << (OP_TEST_STATEID - 32) |
7995 1 << (OP_FREE_STATEID - 32) |
7996 1 << (OP_WRITE - 32)
7997 }
7998 };
7999
8000
8001
8002
8003
8004
8005
8006 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8007 struct nfs41_state_protection *sp)
8008 {
8009 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8010 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8011 1 << (OP_EXCHANGE_ID - 32) |
8012 1 << (OP_CREATE_SESSION - 32) |
8013 1 << (OP_DESTROY_SESSION - 32) |
8014 1 << (OP_DESTROY_CLIENTID - 32)
8015 };
8016 unsigned long flags = 0;
8017 unsigned int i;
8018 int ret = 0;
8019
8020 if (sp->how == SP4_MACH_CRED) {
8021
8022 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8023 for (i = 0; i <= LAST_NFS4_OP; i++) {
8024 if (test_bit(i, sp->enforce.u.longs))
8025 dfprintk(MOUNT, " enforce op %d\n", i);
8026 if (test_bit(i, sp->allow.u.longs))
8027 dfprintk(MOUNT, " allow op %d\n", i);
8028 }
8029
8030
8031 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8032 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8033 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8034 ret = -EINVAL;
8035 goto out;
8036 }
8037 }
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8049 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8050 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8051 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8052 dfprintk(MOUNT, "sp4_mach_cred:\n");
8053 dfprintk(MOUNT, " minimal mode enabled\n");
8054 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8055 } else {
8056 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8057 ret = -EINVAL;
8058 goto out;
8059 }
8060
8061 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8062 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8063 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8064 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8065 dfprintk(MOUNT, " cleanup mode enabled\n");
8066 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8067 }
8068
8069 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8070 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
8071 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8072 }
8073
8074 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8075 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8076 dfprintk(MOUNT, " secinfo mode enabled\n");
8077 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8078 }
8079
8080 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8081 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8082 dfprintk(MOUNT, " stateid mode enabled\n");
8083 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8084 }
8085
8086 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8087 dfprintk(MOUNT, " write mode enabled\n");
8088 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8089 }
8090
8091 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8092 dfprintk(MOUNT, " commit mode enabled\n");
8093 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8094 }
8095 }
8096 out:
8097 clp->cl_sp4_flags = flags;
8098 return ret;
8099 }
8100
8101 struct nfs41_exchange_id_data {
8102 struct nfs41_exchange_id_res res;
8103 struct nfs41_exchange_id_args args;
8104 };
8105
8106 static void nfs4_exchange_id_release(void *data)
8107 {
8108 struct nfs41_exchange_id_data *cdata =
8109 (struct nfs41_exchange_id_data *)data;
8110
8111 nfs_put_client(cdata->args.client);
8112 kfree(cdata->res.impl_id);
8113 kfree(cdata->res.server_scope);
8114 kfree(cdata->res.server_owner);
8115 kfree(cdata);
8116 }
8117
8118 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8119 .rpc_release = nfs4_exchange_id_release,
8120 };
8121
8122
8123
8124
8125
8126
8127 static struct rpc_task *
8128 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8129 u32 sp4_how, struct rpc_xprt *xprt)
8130 {
8131 struct rpc_message msg = {
8132 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8133 .rpc_cred = cred,
8134 };
8135 struct rpc_task_setup task_setup_data = {
8136 .rpc_client = clp->cl_rpcclient,
8137 .callback_ops = &nfs4_exchange_id_call_ops,
8138 .rpc_message = &msg,
8139 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8140 };
8141 struct nfs41_exchange_id_data *calldata;
8142 int status;
8143
8144 if (!refcount_inc_not_zero(&clp->cl_count))
8145 return ERR_PTR(-EIO);
8146
8147 status = -ENOMEM;
8148 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8149 if (!calldata)
8150 goto out;
8151
8152 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8153
8154 status = nfs4_init_uniform_client_string(clp);
8155 if (status)
8156 goto out_calldata;
8157
8158 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8159 GFP_NOFS);
8160 status = -ENOMEM;
8161 if (unlikely(calldata->res.server_owner == NULL))
8162 goto out_calldata;
8163
8164 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8165 GFP_NOFS);
8166 if (unlikely(calldata->res.server_scope == NULL))
8167 goto out_server_owner;
8168
8169 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8170 if (unlikely(calldata->res.impl_id == NULL))
8171 goto out_server_scope;
8172
8173 switch (sp4_how) {
8174 case SP4_NONE:
8175 calldata->args.state_protect.how = SP4_NONE;
8176 break;
8177
8178 case SP4_MACH_CRED:
8179 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8180 break;
8181
8182 default:
8183
8184 WARN_ON_ONCE(1);
8185 status = -EINVAL;
8186 goto out_impl_id;
8187 }
8188 if (xprt) {
8189 task_setup_data.rpc_xprt = xprt;
8190 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8191 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8192 sizeof(calldata->args.verifier.data));
8193 }
8194 calldata->args.client = clp;
8195 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8196 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8197 #ifdef CONFIG_NFS_V4_1_MIGRATION
8198 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8199 #endif
8200 msg.rpc_argp = &calldata->args;
8201 msg.rpc_resp = &calldata->res;
8202 task_setup_data.callback_data = calldata;
8203
8204 return rpc_run_task(&task_setup_data);
8205
8206 out_impl_id:
8207 kfree(calldata->res.impl_id);
8208 out_server_scope:
8209 kfree(calldata->res.server_scope);
8210 out_server_owner:
8211 kfree(calldata->res.server_owner);
8212 out_calldata:
8213 kfree(calldata);
8214 out:
8215 nfs_put_client(clp);
8216 return ERR_PTR(status);
8217 }
8218
8219
8220
8221
8222
8223
8224 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8225 u32 sp4_how)
8226 {
8227 struct rpc_task *task;
8228 struct nfs41_exchange_id_args *argp;
8229 struct nfs41_exchange_id_res *resp;
8230 unsigned long now = jiffies;
8231 int status;
8232
8233 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8234 if (IS_ERR(task))
8235 return PTR_ERR(task);
8236
8237 argp = task->tk_msg.rpc_argp;
8238 resp = task->tk_msg.rpc_resp;
8239 status = task->tk_status;
8240 if (status != 0)
8241 goto out;
8242
8243 status = nfs4_check_cl_exchange_flags(resp->flags);
8244 if (status != 0)
8245 goto out;
8246
8247 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8248 if (status != 0)
8249 goto out;
8250
8251 do_renew_lease(clp, now);
8252
8253 clp->cl_clientid = resp->clientid;
8254 clp->cl_exchange_flags = resp->flags;
8255 clp->cl_seqid = resp->seqid;
8256
8257 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8258 clear_bit(NFS4_SESSION_ESTABLISHED,
8259 &clp->cl_session->session_state);
8260
8261 if (clp->cl_serverscope != NULL &&
8262 !nfs41_same_server_scope(clp->cl_serverscope,
8263 resp->server_scope)) {
8264 dprintk("%s: server_scope mismatch detected\n",
8265 __func__);
8266 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8267 }
8268
8269 swap(clp->cl_serverowner, resp->server_owner);
8270 swap(clp->cl_serverscope, resp->server_scope);
8271 swap(clp->cl_implid, resp->impl_id);
8272
8273
8274 memcpy(clp->cl_confirm.data, argp->verifier.data,
8275 sizeof(clp->cl_confirm.data));
8276 out:
8277 trace_nfs4_exchange_id(clp, status);
8278 rpc_put_task(task);
8279 return status;
8280 }
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8295 {
8296 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8297 int status;
8298
8299
8300 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8301 authflavor == RPC_AUTH_GSS_KRB5P) {
8302 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8303 if (!status)
8304 return 0;
8305 }
8306
8307
8308 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8309 }
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8327 void *data)
8328 {
8329 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8330 struct rpc_task *task;
8331 int status;
8332
8333 u32 sp4_how;
8334
8335 dprintk("--> %s try %s\n", __func__,
8336 xprt->address_strings[RPC_DISPLAY_ADDR]);
8337
8338 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8339
8340
8341 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8342 if (IS_ERR(task))
8343 return;
8344
8345 status = task->tk_status;
8346 if (status == 0)
8347 status = nfs4_detect_session_trunking(adata->clp,
8348 task->tk_msg.rpc_resp, xprt);
8349
8350 if (status == 0)
8351 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8352
8353 rpc_put_task(task);
8354 }
8355 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8356
8357 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8358 const struct cred *cred)
8359 {
8360 struct rpc_message msg = {
8361 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8362 .rpc_argp = clp,
8363 .rpc_cred = cred,
8364 };
8365 int status;
8366
8367 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8368 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8369 trace_nfs4_destroy_clientid(clp, status);
8370 if (status)
8371 dprintk("NFS: Got error %d from the server %s on "
8372 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8373 return status;
8374 }
8375
8376 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8377 const struct cred *cred)
8378 {
8379 unsigned int loop;
8380 int ret;
8381
8382 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8383 ret = _nfs4_proc_destroy_clientid(clp, cred);
8384 switch (ret) {
8385 case -NFS4ERR_DELAY:
8386 case -NFS4ERR_CLIENTID_BUSY:
8387 ssleep(1);
8388 break;
8389 default:
8390 return ret;
8391 }
8392 }
8393 return 0;
8394 }
8395
8396 int nfs4_destroy_clientid(struct nfs_client *clp)
8397 {
8398 const struct cred *cred;
8399 int ret = 0;
8400
8401 if (clp->cl_mvops->minor_version < 1)
8402 goto out;
8403 if (clp->cl_exchange_flags == 0)
8404 goto out;
8405 if (clp->cl_preserve_clid)
8406 goto out;
8407 cred = nfs4_get_clid_cred(clp);
8408 ret = nfs4_proc_destroy_clientid(clp, cred);
8409 put_cred(cred);
8410 switch (ret) {
8411 case 0:
8412 case -NFS4ERR_STALE_CLIENTID:
8413 clp->cl_exchange_flags = 0;
8414 }
8415 out:
8416 return ret;
8417 }
8418
8419 #endif
8420
8421 struct nfs4_get_lease_time_data {
8422 struct nfs4_get_lease_time_args *args;
8423 struct nfs4_get_lease_time_res *res;
8424 struct nfs_client *clp;
8425 };
8426
8427 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8428 void *calldata)
8429 {
8430 struct nfs4_get_lease_time_data *data =
8431 (struct nfs4_get_lease_time_data *)calldata;
8432
8433 dprintk("--> %s\n", __func__);
8434
8435
8436 nfs4_setup_sequence(data->clp,
8437 &data->args->la_seq_args,
8438 &data->res->lr_seq_res,
8439 task);
8440 dprintk("<-- %s\n", __func__);
8441 }
8442
8443
8444
8445
8446
8447 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8448 {
8449 struct nfs4_get_lease_time_data *data =
8450 (struct nfs4_get_lease_time_data *)calldata;
8451
8452 dprintk("--> %s\n", __func__);
8453 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
8454 return;
8455 switch (task->tk_status) {
8456 case -NFS4ERR_DELAY:
8457 case -NFS4ERR_GRACE:
8458 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8459 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8460 task->tk_status = 0;
8461
8462 case -NFS4ERR_RETRY_UNCACHED_REP:
8463 rpc_restart_call_prepare(task);
8464 return;
8465 }
8466 dprintk("<-- %s\n", __func__);
8467 }
8468
8469 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8470 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8471 .rpc_call_done = nfs4_get_lease_time_done,
8472 };
8473
8474 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8475 {
8476 struct nfs4_get_lease_time_args args;
8477 struct nfs4_get_lease_time_res res = {
8478 .lr_fsinfo = fsinfo,
8479 };
8480 struct nfs4_get_lease_time_data data = {
8481 .args = &args,
8482 .res = &res,
8483 .clp = clp,
8484 };
8485 struct rpc_message msg = {
8486 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8487 .rpc_argp = &args,
8488 .rpc_resp = &res,
8489 };
8490 struct rpc_task_setup task_setup = {
8491 .rpc_client = clp->cl_rpcclient,
8492 .rpc_message = &msg,
8493 .callback_ops = &nfs4_get_lease_time_ops,
8494 .callback_data = &data,
8495 .flags = RPC_TASK_TIMEOUT,
8496 };
8497
8498 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8499 return nfs4_call_sync_custom(&task_setup);
8500 }
8501
8502 #ifdef CONFIG_NFS_V4_1
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8514 struct rpc_clnt *clnt)
8515 {
8516 unsigned int max_rqst_sz, max_resp_sz;
8517 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8518 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
8519
8520 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8521 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8522
8523
8524 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8525 args->fc_attrs.max_resp_sz = max_resp_sz;
8526 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8527 args->fc_attrs.max_reqs = max_session_slots;
8528
8529 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8530 "max_ops=%u max_reqs=%u\n",
8531 __func__,
8532 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8533 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8534
8535
8536 args->bc_attrs.max_rqst_sz = max_bc_payload;
8537 args->bc_attrs.max_resp_sz = max_bc_payload;
8538 args->bc_attrs.max_resp_sz_cached = 0;
8539 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8540 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
8541 if (args->bc_attrs.max_reqs > max_bc_slots)
8542 args->bc_attrs.max_reqs = max_bc_slots;
8543
8544 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8545 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8546 __func__,
8547 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8548 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8549 args->bc_attrs.max_reqs);
8550 }
8551
8552 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8553 struct nfs41_create_session_res *res)
8554 {
8555 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8556 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8557
8558 if (rcvd->max_resp_sz > sent->max_resp_sz)
8559 return -EINVAL;
8560
8561
8562
8563
8564
8565
8566 if (rcvd->max_ops < sent->max_ops)
8567 return -EINVAL;
8568 if (rcvd->max_reqs == 0)
8569 return -EINVAL;
8570 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8571 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8572 return 0;
8573 }
8574
8575 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8576 struct nfs41_create_session_res *res)
8577 {
8578 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8579 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8580
8581 if (!(res->flags & SESSION4_BACK_CHAN))
8582 goto out;
8583 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8584 return -EINVAL;
8585 if (rcvd->max_resp_sz < sent->max_resp_sz)
8586 return -EINVAL;
8587 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8588 return -EINVAL;
8589 if (rcvd->max_ops > sent->max_ops)
8590 return -EINVAL;
8591 if (rcvd->max_reqs > sent->max_reqs)
8592 return -EINVAL;
8593 out:
8594 return 0;
8595 }
8596
8597 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8598 struct nfs41_create_session_res *res)
8599 {
8600 int ret;
8601
8602 ret = nfs4_verify_fore_channel_attrs(args, res);
8603 if (ret)
8604 return ret;
8605 return nfs4_verify_back_channel_attrs(args, res);
8606 }
8607
8608 static void nfs4_update_session(struct nfs4_session *session,
8609 struct nfs41_create_session_res *res)
8610 {
8611 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8612
8613 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8614 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8615 session->flags = res->flags;
8616 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8617 if (res->flags & SESSION4_BACK_CHAN)
8618 memcpy(&session->bc_attrs, &res->bc_attrs,
8619 sizeof(session->bc_attrs));
8620 }
8621
8622 static int _nfs4_proc_create_session(struct nfs_client *clp,
8623 const struct cred *cred)
8624 {
8625 struct nfs4_session *session = clp->cl_session;
8626 struct nfs41_create_session_args args = {
8627 .client = clp,
8628 .clientid = clp->cl_clientid,
8629 .seqid = clp->cl_seqid,
8630 .cb_program = NFS4_CALLBACK,
8631 };
8632 struct nfs41_create_session_res res;
8633
8634 struct rpc_message msg = {
8635 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8636 .rpc_argp = &args,
8637 .rpc_resp = &res,
8638 .rpc_cred = cred,
8639 };
8640 int status;
8641
8642 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8643 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8644
8645 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8646 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8647 trace_nfs4_create_session(clp, status);
8648
8649 switch (status) {
8650 case -NFS4ERR_STALE_CLIENTID:
8651 case -NFS4ERR_DELAY:
8652 case -ETIMEDOUT:
8653 case -EACCES:
8654 case -EAGAIN:
8655 goto out;
8656 };
8657
8658 clp->cl_seqid++;
8659 if (!status) {
8660
8661 status = nfs4_verify_channel_attrs(&args, &res);
8662
8663 if (status)
8664 goto out;
8665 nfs4_update_session(session, &res);
8666 }
8667 out:
8668 return status;
8669 }
8670
8671
8672
8673
8674
8675
8676 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
8677 {
8678 int status;
8679 unsigned *ptr;
8680 struct nfs4_session *session = clp->cl_session;
8681
8682 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8683
8684 status = _nfs4_proc_create_session(clp, cred);
8685 if (status)
8686 goto out;
8687
8688
8689 status = nfs4_setup_session_slot_tables(session);
8690 dprintk("slot table setup returned %d\n", status);
8691 if (status)
8692 goto out;
8693
8694 ptr = (unsigned *)&session->sess_id.data[0];
8695 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8696 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8697 out:
8698 dprintk("<-- %s\n", __func__);
8699 return status;
8700 }
8701
8702
8703
8704
8705
8706 int nfs4_proc_destroy_session(struct nfs4_session *session,
8707 const struct cred *cred)
8708 {
8709 struct rpc_message msg = {
8710 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8711 .rpc_argp = session,
8712 .rpc_cred = cred,
8713 };
8714 int status = 0;
8715
8716 dprintk("--> nfs4_proc_destroy_session\n");
8717
8718
8719 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8720 return 0;
8721
8722 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8723 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8724 trace_nfs4_destroy_session(session->clp, status);
8725
8726 if (status)
8727 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8728 "Session has been destroyed regardless...\n", status);
8729
8730 dprintk("<-- nfs4_proc_destroy_session\n");
8731 return status;
8732 }
8733
8734
8735
8736
8737 struct nfs4_sequence_data {
8738 struct nfs_client *clp;
8739 struct nfs4_sequence_args args;
8740 struct nfs4_sequence_res res;
8741 };
8742
8743 static void nfs41_sequence_release(void *data)
8744 {
8745 struct nfs4_sequence_data *calldata = data;
8746 struct nfs_client *clp = calldata->clp;
8747
8748 if (refcount_read(&clp->cl_count) > 1)
8749 nfs4_schedule_state_renewal(clp);
8750 nfs_put_client(clp);
8751 kfree(calldata);
8752 }
8753
8754 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8755 {
8756 switch(task->tk_status) {
8757 case -NFS4ERR_DELAY:
8758 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8759 return -EAGAIN;
8760 default:
8761 nfs4_schedule_lease_recovery(clp);
8762 }
8763 return 0;
8764 }
8765
8766 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8767 {
8768 struct nfs4_sequence_data *calldata = data;
8769 struct nfs_client *clp = calldata->clp;
8770
8771 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8772 return;
8773
8774 trace_nfs4_sequence(clp, task->tk_status);
8775 if (task->tk_status < 0) {
8776 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8777 if (refcount_read(&clp->cl_count) == 1)
8778 goto out;
8779
8780 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8781 rpc_restart_call_prepare(task);
8782 return;
8783 }
8784 }
8785 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8786 out:
8787 dprintk("<-- %s\n", __func__);
8788 }
8789
8790 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8791 {
8792 struct nfs4_sequence_data *calldata = data;
8793 struct nfs_client *clp = calldata->clp;
8794 struct nfs4_sequence_args *args;
8795 struct nfs4_sequence_res *res;
8796
8797 args = task->tk_msg.rpc_argp;
8798 res = task->tk_msg.rpc_resp;
8799
8800 nfs4_setup_sequence(clp, args, res, task);
8801 }
8802
8803 static const struct rpc_call_ops nfs41_sequence_ops = {
8804 .rpc_call_done = nfs41_sequence_call_done,
8805 .rpc_call_prepare = nfs41_sequence_prepare,
8806 .rpc_release = nfs41_sequence_release,
8807 };
8808
8809 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8810 const struct cred *cred,
8811 struct nfs4_slot *slot,
8812 bool is_privileged)
8813 {
8814 struct nfs4_sequence_data *calldata;
8815 struct rpc_message msg = {
8816 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8817 .rpc_cred = cred,
8818 };
8819 struct rpc_task_setup task_setup_data = {
8820 .rpc_client = clp->cl_rpcclient,
8821 .rpc_message = &msg,
8822 .callback_ops = &nfs41_sequence_ops,
8823 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8824 };
8825 struct rpc_task *ret;
8826
8827 ret = ERR_PTR(-EIO);
8828 if (!refcount_inc_not_zero(&clp->cl_count))
8829 goto out_err;
8830
8831 ret = ERR_PTR(-ENOMEM);
8832 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8833 if (calldata == NULL)
8834 goto out_put_clp;
8835 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
8836 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
8837 msg.rpc_argp = &calldata->args;
8838 msg.rpc_resp = &calldata->res;
8839 calldata->clp = clp;
8840 task_setup_data.callback_data = calldata;
8841
8842 ret = rpc_run_task(&task_setup_data);
8843 if (IS_ERR(ret))
8844 goto out_err;
8845 return ret;
8846 out_put_clp:
8847 nfs_put_client(clp);
8848 out_err:
8849 nfs41_release_slot(slot);
8850 return ret;
8851 }
8852
8853 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
8854 {
8855 struct rpc_task *task;
8856 int ret = 0;
8857
8858 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8859 return -EAGAIN;
8860 task = _nfs41_proc_sequence(clp, cred, NULL, false);
8861 if (IS_ERR(task))
8862 ret = PTR_ERR(task);
8863 else
8864 rpc_put_task_async(task);
8865 dprintk("<-- %s status=%d\n", __func__, ret);
8866 return ret;
8867 }
8868
8869 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
8870 {
8871 struct rpc_task *task;
8872 int ret;
8873
8874 task = _nfs41_proc_sequence(clp, cred, NULL, true);
8875 if (IS_ERR(task)) {
8876 ret = PTR_ERR(task);
8877 goto out;
8878 }
8879 ret = rpc_wait_for_completion_task(task);
8880 if (!ret)
8881 ret = task->tk_status;
8882 rpc_put_task(task);
8883 out:
8884 dprintk("<-- %s status=%d\n", __func__, ret);
8885 return ret;
8886 }
8887
8888 struct nfs4_reclaim_complete_data {
8889 struct nfs_client *clp;
8890 struct nfs41_reclaim_complete_args arg;
8891 struct nfs41_reclaim_complete_res res;
8892 };
8893
8894 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8895 {
8896 struct nfs4_reclaim_complete_data *calldata = data;
8897
8898 nfs4_setup_sequence(calldata->clp,
8899 &calldata->arg.seq_args,
8900 &calldata->res.seq_res,
8901 task);
8902 }
8903
8904 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8905 {
8906 switch(task->tk_status) {
8907 case 0:
8908 wake_up_all(&clp->cl_lock_waitq);
8909
8910 case -NFS4ERR_COMPLETE_ALREADY:
8911 case -NFS4ERR_WRONG_CRED:
8912 break;
8913 case -NFS4ERR_DELAY:
8914 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8915
8916 case -NFS4ERR_RETRY_UNCACHED_REP:
8917 return -EAGAIN;
8918 case -NFS4ERR_BADSESSION:
8919 case -NFS4ERR_DEADSESSION:
8920 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8921 break;
8922 default:
8923 nfs4_schedule_lease_recovery(clp);
8924 }
8925 return 0;
8926 }
8927
8928 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8929 {
8930 struct nfs4_reclaim_complete_data *calldata = data;
8931 struct nfs_client *clp = calldata->clp;
8932 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8933
8934 dprintk("--> %s\n", __func__);
8935 if (!nfs41_sequence_done(task, res))
8936 return;
8937
8938 trace_nfs4_reclaim_complete(clp, task->tk_status);
8939 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8940 rpc_restart_call_prepare(task);
8941 return;
8942 }
8943 dprintk("<-- %s\n", __func__);
8944 }
8945
8946 static void nfs4_free_reclaim_complete_data(void *data)
8947 {
8948 struct nfs4_reclaim_complete_data *calldata = data;
8949
8950 kfree(calldata);
8951 }
8952
8953 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8954 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8955 .rpc_call_done = nfs4_reclaim_complete_done,
8956 .rpc_release = nfs4_free_reclaim_complete_data,
8957 };
8958
8959
8960
8961
8962 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8963 const struct cred *cred)
8964 {
8965 struct nfs4_reclaim_complete_data *calldata;
8966 struct rpc_message msg = {
8967 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8968 .rpc_cred = cred,
8969 };
8970 struct rpc_task_setup task_setup_data = {
8971 .rpc_client = clp->cl_rpcclient,
8972 .rpc_message = &msg,
8973 .callback_ops = &nfs4_reclaim_complete_call_ops,
8974 .flags = RPC_TASK_NO_ROUND_ROBIN,
8975 };
8976 int status = -ENOMEM;
8977
8978 dprintk("--> %s\n", __func__);
8979 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8980 if (calldata == NULL)
8981 goto out;
8982 calldata->clp = clp;
8983 calldata->arg.one_fs = 0;
8984
8985 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
8986 msg.rpc_argp = &calldata->arg;
8987 msg.rpc_resp = &calldata->res;
8988 task_setup_data.callback_data = calldata;
8989 status = nfs4_call_sync_custom(&task_setup_data);
8990 out:
8991 dprintk("<-- %s status=%d\n", __func__, status);
8992 return status;
8993 }
8994
8995 static void
8996 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8997 {
8998 struct nfs4_layoutget *lgp = calldata;
8999 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9000
9001 dprintk("--> %s\n", __func__);
9002 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9003 &lgp->res.seq_res, task);
9004 dprintk("<-- %s\n", __func__);
9005 }
9006
9007 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9008 {
9009 struct nfs4_layoutget *lgp = calldata;
9010
9011 dprintk("--> %s\n", __func__);
9012 nfs41_sequence_process(task, &lgp->res.seq_res);
9013 dprintk("<-- %s\n", __func__);
9014 }
9015
9016 static int
9017 nfs4_layoutget_handle_exception(struct rpc_task *task,
9018 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9019 {
9020 struct inode *inode = lgp->args.inode;
9021 struct nfs_server *server = NFS_SERVER(inode);
9022 struct pnfs_layout_hdr *lo;
9023 int nfs4err = task->tk_status;
9024 int err, status = 0;
9025 LIST_HEAD(head);
9026
9027 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9028
9029 nfs4_sequence_free_slot(&lgp->res.seq_res);
9030
9031 switch (nfs4err) {
9032 case 0:
9033 goto out;
9034
9035
9036
9037
9038
9039
9040 case -NFS4ERR_LAYOUTUNAVAILABLE:
9041 status = -ENODATA;
9042 goto out;
9043
9044
9045
9046
9047 case -NFS4ERR_BADLAYOUT:
9048 status = -EOVERFLOW;
9049 goto out;
9050
9051
9052
9053
9054
9055
9056
9057
9058 case -NFS4ERR_LAYOUTTRYLATER:
9059 if (lgp->args.minlength == 0) {
9060 status = -EOVERFLOW;
9061 goto out;
9062 }
9063 status = -EBUSY;
9064 break;
9065 case -NFS4ERR_RECALLCONFLICT:
9066 status = -ERECALLCONFLICT;
9067 break;
9068 case -NFS4ERR_DELEG_REVOKED:
9069 case -NFS4ERR_ADMIN_REVOKED:
9070 case -NFS4ERR_EXPIRED:
9071 case -NFS4ERR_BAD_STATEID:
9072 exception->timeout = 0;
9073 spin_lock(&inode->i_lock);
9074 lo = NFS_I(inode)->layout;
9075
9076 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9077 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9078 spin_unlock(&inode->i_lock);
9079 exception->state = lgp->args.ctx->state;
9080 exception->stateid = &lgp->args.stateid;
9081 break;
9082 }
9083
9084
9085
9086
9087 pnfs_mark_layout_stateid_invalid(lo, &head);
9088 spin_unlock(&inode->i_lock);
9089 nfs_commit_inode(inode, 0);
9090 pnfs_free_lseg_list(&head);
9091 status = -EAGAIN;
9092 goto out;
9093 }
9094
9095 err = nfs4_handle_exception(server, nfs4err, exception);
9096 if (!status) {
9097 if (exception->retry)
9098 status = -EAGAIN;
9099 else
9100 status = err;
9101 }
9102 out:
9103 dprintk("<-- %s\n", __func__);
9104 return status;
9105 }
9106
9107 size_t max_response_pages(struct nfs_server *server)
9108 {
9109 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9110 return nfs_page_array_len(0, max_resp_sz);
9111 }
9112
9113 static void nfs4_layoutget_release(void *calldata)
9114 {
9115 struct nfs4_layoutget *lgp = calldata;
9116
9117 dprintk("--> %s\n", __func__);
9118 nfs4_sequence_free_slot(&lgp->res.seq_res);
9119 pnfs_layoutget_free(lgp);
9120 dprintk("<-- %s\n", __func__);
9121 }
9122
9123 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9124 .rpc_call_prepare = nfs4_layoutget_prepare,
9125 .rpc_call_done = nfs4_layoutget_done,
9126 .rpc_release = nfs4_layoutget_release,
9127 };
9128
9129 struct pnfs_layout_segment *
9130 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9131 {
9132 struct inode *inode = lgp->args.inode;
9133 struct nfs_server *server = NFS_SERVER(inode);
9134 struct rpc_task *task;
9135 struct rpc_message msg = {
9136 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9137 .rpc_argp = &lgp->args,
9138 .rpc_resp = &lgp->res,
9139 .rpc_cred = lgp->cred,
9140 };
9141 struct rpc_task_setup task_setup_data = {
9142 .rpc_client = server->client,
9143 .rpc_message = &msg,
9144 .callback_ops = &nfs4_layoutget_call_ops,
9145 .callback_data = lgp,
9146 .flags = RPC_TASK_ASYNC,
9147 };
9148 struct pnfs_layout_segment *lseg = NULL;
9149 struct nfs4_exception exception = {
9150 .inode = inode,
9151 .timeout = *timeout,
9152 };
9153 int status = 0;
9154
9155 dprintk("--> %s\n", __func__);
9156
9157
9158 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9159
9160 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9161
9162 task = rpc_run_task(&task_setup_data);
9163 if (IS_ERR(task))
9164 return ERR_CAST(task);
9165 status = rpc_wait_for_completion_task(task);
9166 if (status != 0)
9167 goto out;
9168
9169 if (task->tk_status < 0) {
9170 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9171 *timeout = exception.timeout;
9172 } else if (lgp->res.layoutp->len == 0) {
9173 status = -EAGAIN;
9174 *timeout = nfs4_update_delay(&exception.timeout);
9175 } else
9176 lseg = pnfs_layout_process(lgp);
9177 out:
9178 trace_nfs4_layoutget(lgp->args.ctx,
9179 &lgp->args.range,
9180 &lgp->res.range,
9181 &lgp->res.stateid,
9182 status);
9183
9184 rpc_put_task(task);
9185 dprintk("<-- %s status=%d\n", __func__, status);
9186 if (status)
9187 return ERR_PTR(status);
9188 return lseg;
9189 }
9190
9191 static void
9192 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9193 {
9194 struct nfs4_layoutreturn *lrp = calldata;
9195
9196 dprintk("--> %s\n", __func__);
9197 nfs4_setup_sequence(lrp->clp,
9198 &lrp->args.seq_args,
9199 &lrp->res.seq_res,
9200 task);
9201 if (!pnfs_layout_is_valid(lrp->args.layout))
9202 rpc_exit(task, 0);
9203 }
9204
9205 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9206 {
9207 struct nfs4_layoutreturn *lrp = calldata;
9208 struct nfs_server *server;
9209
9210 dprintk("--> %s\n", __func__);
9211
9212 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9213 return;
9214
9215
9216
9217
9218
9219 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9220 lrp->res.lrs_present = 0;
9221 return;
9222 }
9223
9224 server = NFS_SERVER(lrp->args.inode);
9225 switch (task->tk_status) {
9226 case -NFS4ERR_OLD_STATEID:
9227 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9228 &lrp->args.range,
9229 lrp->args.inode))
9230 goto out_restart;
9231
9232 default:
9233 task->tk_status = 0;
9234
9235 case 0:
9236 break;
9237 case -NFS4ERR_DELAY:
9238 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9239 break;
9240 goto out_restart;
9241 }
9242 dprintk("<-- %s\n", __func__);
9243 return;
9244 out_restart:
9245 task->tk_status = 0;
9246 nfs4_sequence_free_slot(&lrp->res.seq_res);
9247 rpc_restart_call_prepare(task);
9248 }
9249
9250 static void nfs4_layoutreturn_release(void *calldata)
9251 {
9252 struct nfs4_layoutreturn *lrp = calldata;
9253 struct pnfs_layout_hdr *lo = lrp->args.layout;
9254
9255 dprintk("--> %s\n", __func__);
9256 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9257 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9258 nfs4_sequence_free_slot(&lrp->res.seq_res);
9259 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9260 lrp->ld_private.ops->free(&lrp->ld_private);
9261 pnfs_put_layout_hdr(lrp->args.layout);
9262 nfs_iput_and_deactive(lrp->inode);
9263 kfree(calldata);
9264 dprintk("<-- %s\n", __func__);
9265 }
9266
9267 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9268 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9269 .rpc_call_done = nfs4_layoutreturn_done,
9270 .rpc_release = nfs4_layoutreturn_release,
9271 };
9272
9273 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9274 {
9275 struct rpc_task *task;
9276 struct rpc_message msg = {
9277 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9278 .rpc_argp = &lrp->args,
9279 .rpc_resp = &lrp->res,
9280 .rpc_cred = lrp->cred,
9281 };
9282 struct rpc_task_setup task_setup_data = {
9283 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9284 .rpc_message = &msg,
9285 .callback_ops = &nfs4_layoutreturn_call_ops,
9286 .callback_data = lrp,
9287 };
9288 int status = 0;
9289
9290 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9291 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9292 &task_setup_data.rpc_client, &msg);
9293
9294 dprintk("--> %s\n", __func__);
9295 if (!sync) {
9296 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9297 if (!lrp->inode) {
9298 nfs4_layoutreturn_release(lrp);
9299 return -EAGAIN;
9300 }
9301 task_setup_data.flags |= RPC_TASK_ASYNC;
9302 }
9303 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
9304 task = rpc_run_task(&task_setup_data);
9305 if (IS_ERR(task))
9306 return PTR_ERR(task);
9307 if (sync)
9308 status = task->tk_status;
9309 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9310 dprintk("<-- %s status=%d\n", __func__, status);
9311 rpc_put_task(task);
9312 return status;
9313 }
9314
9315 static int
9316 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9317 struct pnfs_device *pdev,
9318 const struct cred *cred)
9319 {
9320 struct nfs4_getdeviceinfo_args args = {
9321 .pdev = pdev,
9322 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9323 NOTIFY_DEVICEID4_DELETE,
9324 };
9325 struct nfs4_getdeviceinfo_res res = {
9326 .pdev = pdev,
9327 };
9328 struct rpc_message msg = {
9329 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9330 .rpc_argp = &args,
9331 .rpc_resp = &res,
9332 .rpc_cred = cred,
9333 };
9334 int status;
9335
9336 dprintk("--> %s\n", __func__);
9337 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9338 if (res.notification & ~args.notify_types)
9339 dprintk("%s: unsupported notification\n", __func__);
9340 if (res.notification != args.notify_types)
9341 pdev->nocache = 1;
9342
9343 dprintk("<-- %s status=%d\n", __func__, status);
9344
9345 return status;
9346 }
9347
9348 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9349 struct pnfs_device *pdev,
9350 const struct cred *cred)
9351 {
9352 struct nfs4_exception exception = { };
9353 int err;
9354
9355 do {
9356 err = nfs4_handle_exception(server,
9357 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9358 &exception);
9359 } while (exception.retry);
9360 return err;
9361 }
9362 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9363
9364 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9365 {
9366 struct nfs4_layoutcommit_data *data = calldata;
9367 struct nfs_server *server = NFS_SERVER(data->args.inode);
9368
9369 nfs4_setup_sequence(server->nfs_client,
9370 &data->args.seq_args,
9371 &data->res.seq_res,
9372 task);
9373 }
9374
9375 static void
9376 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9377 {
9378 struct nfs4_layoutcommit_data *data = calldata;
9379 struct nfs_server *server = NFS_SERVER(data->args.inode);
9380
9381 if (!nfs41_sequence_done(task, &data->res.seq_res))
9382 return;
9383
9384 switch (task->tk_status) {
9385 case -NFS4ERR_DELEG_REVOKED:
9386 case -NFS4ERR_BADIOMODE:
9387 case -NFS4ERR_BADLAYOUT:
9388 case -NFS4ERR_GRACE:
9389 task->tk_status = 0;
9390 case 0:
9391 break;
9392 default:
9393 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9394 rpc_restart_call_prepare(task);
9395 return;
9396 }
9397 }
9398 }
9399
9400 static void nfs4_layoutcommit_release(void *calldata)
9401 {
9402 struct nfs4_layoutcommit_data *data = calldata;
9403
9404 pnfs_cleanup_layoutcommit(data);
9405 nfs_post_op_update_inode_force_wcc(data->args.inode,
9406 data->res.fattr);
9407 put_cred(data->cred);
9408 nfs_iput_and_deactive(data->inode);
9409 kfree(data);
9410 }
9411
9412 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9413 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9414 .rpc_call_done = nfs4_layoutcommit_done,
9415 .rpc_release = nfs4_layoutcommit_release,
9416 };
9417
9418 int
9419 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9420 {
9421 struct rpc_message msg = {
9422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9423 .rpc_argp = &data->args,
9424 .rpc_resp = &data->res,
9425 .rpc_cred = data->cred,
9426 };
9427 struct rpc_task_setup task_setup_data = {
9428 .task = &data->task,
9429 .rpc_client = NFS_CLIENT(data->args.inode),
9430 .rpc_message = &msg,
9431 .callback_ops = &nfs4_layoutcommit_ops,
9432 .callback_data = data,
9433 };
9434 struct rpc_task *task;
9435 int status = 0;
9436
9437 dprintk("NFS: initiating layoutcommit call. sync %d "
9438 "lbw: %llu inode %lu\n", sync,
9439 data->args.lastbytewritten,
9440 data->args.inode->i_ino);
9441
9442 if (!sync) {
9443 data->inode = nfs_igrab_and_active(data->args.inode);
9444 if (data->inode == NULL) {
9445 nfs4_layoutcommit_release(data);
9446 return -EAGAIN;
9447 }
9448 task_setup_data.flags = RPC_TASK_ASYNC;
9449 }
9450 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9451 task = rpc_run_task(&task_setup_data);
9452 if (IS_ERR(task))
9453 return PTR_ERR(task);
9454 if (sync)
9455 status = task->tk_status;
9456 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9457 dprintk("%s: status %d\n", __func__, status);
9458 rpc_put_task(task);
9459 return status;
9460 }
9461
9462
9463
9464
9465
9466 static int
9467 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9468 struct nfs_fsinfo *info,
9469 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9470 {
9471 struct nfs41_secinfo_no_name_args args = {
9472 .style = SECINFO_STYLE_CURRENT_FH,
9473 };
9474 struct nfs4_secinfo_res res = {
9475 .flavors = flavors,
9476 };
9477 struct rpc_message msg = {
9478 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9479 .rpc_argp = &args,
9480 .rpc_resp = &res,
9481 };
9482 struct rpc_clnt *clnt = server->client;
9483 struct nfs4_call_sync_data data = {
9484 .seq_server = server,
9485 .seq_args = &args.seq_args,
9486 .seq_res = &res.seq_res,
9487 };
9488 struct rpc_task_setup task_setup = {
9489 .rpc_client = server->client,
9490 .rpc_message = &msg,
9491 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9492 .callback_data = &data,
9493 .flags = RPC_TASK_NO_ROUND_ROBIN,
9494 };
9495 const struct cred *cred = NULL;
9496 int status;
9497
9498 if (use_integrity) {
9499 clnt = server->nfs_client->cl_rpcclient;
9500 task_setup.rpc_client = clnt;
9501
9502 cred = nfs4_get_clid_cred(server->nfs_client);
9503 msg.rpc_cred = cred;
9504 }
9505
9506 dprintk("--> %s\n", __func__);
9507 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9508 status = nfs4_call_sync_custom(&task_setup);
9509 dprintk("<-- %s status=%d\n", __func__, status);
9510
9511 put_cred(cred);
9512
9513 return status;
9514 }
9515
9516 static int
9517 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9518 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9519 {
9520 struct nfs4_exception exception = {
9521 .interruptible = true,
9522 };
9523 int err;
9524 do {
9525
9526 err = -NFS4ERR_WRONGSEC;
9527
9528
9529 if (_nfs4_is_integrity_protected(server->nfs_client))
9530 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9531 flavors, true);
9532
9533
9534
9535
9536
9537
9538
9539 if (err == -NFS4ERR_WRONGSEC)
9540 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9541 flavors, false);
9542
9543 switch (err) {
9544 case 0:
9545 case -NFS4ERR_WRONGSEC:
9546 case -ENOTSUPP:
9547 goto out;
9548 default:
9549 err = nfs4_handle_exception(server, err, &exception);
9550 }
9551 } while (exception.retry);
9552 out:
9553 return err;
9554 }
9555
9556 static int
9557 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9558 struct nfs_fsinfo *info)
9559 {
9560 int err;
9561 struct page *page;
9562 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9563 struct nfs4_secinfo_flavors *flavors;
9564 struct nfs4_secinfo4 *secinfo;
9565 int i;
9566
9567 page = alloc_page(GFP_KERNEL);
9568 if (!page) {
9569 err = -ENOMEM;
9570 goto out;
9571 }
9572
9573 flavors = page_address(page);
9574 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9575
9576
9577
9578
9579
9580 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9581 err = nfs4_find_root_sec(server, fhandle, info);
9582 goto out_freepage;
9583 }
9584 if (err)
9585 goto out_freepage;
9586
9587 for (i = 0; i < flavors->num_flavors; i++) {
9588 secinfo = &flavors->flavors[i];
9589
9590 switch (secinfo->flavor) {
9591 case RPC_AUTH_NULL:
9592 case RPC_AUTH_UNIX:
9593 case RPC_AUTH_GSS:
9594 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9595 &secinfo->flavor_info);
9596 break;
9597 default:
9598 flavor = RPC_AUTH_MAXFLAVOR;
9599 break;
9600 }
9601
9602 if (!nfs_auth_info_match(&server->auth_info, flavor))
9603 flavor = RPC_AUTH_MAXFLAVOR;
9604
9605 if (flavor != RPC_AUTH_MAXFLAVOR) {
9606 err = nfs4_lookup_root_sec(server, fhandle,
9607 info, flavor);
9608 if (!err)
9609 break;
9610 }
9611 }
9612
9613 if (flavor == RPC_AUTH_MAXFLAVOR)
9614 err = -EPERM;
9615
9616 out_freepage:
9617 put_page(page);
9618 if (err == -EACCES)
9619 return -EPERM;
9620 out:
9621 return err;
9622 }
9623
9624 static int _nfs41_test_stateid(struct nfs_server *server,
9625 nfs4_stateid *stateid,
9626 const struct cred *cred)
9627 {
9628 int status;
9629 struct nfs41_test_stateid_args args = {
9630 .stateid = stateid,
9631 };
9632 struct nfs41_test_stateid_res res;
9633 struct rpc_message msg = {
9634 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9635 .rpc_argp = &args,
9636 .rpc_resp = &res,
9637 .rpc_cred = cred,
9638 };
9639 struct rpc_clnt *rpc_client = server->client;
9640
9641 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9642 &rpc_client, &msg);
9643
9644 dprintk("NFS call test_stateid %p\n", stateid);
9645 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
9646 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9647 &args.seq_args, &res.seq_res);
9648 if (status != NFS_OK) {
9649 dprintk("NFS reply test_stateid: failed, %d\n", status);
9650 return status;
9651 }
9652 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9653 return -res.status;
9654 }
9655
9656 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9657 int err, struct nfs4_exception *exception)
9658 {
9659 exception->retry = 0;
9660 switch(err) {
9661 case -NFS4ERR_DELAY:
9662 case -NFS4ERR_RETRY_UNCACHED_REP:
9663 nfs4_handle_exception(server, err, exception);
9664 break;
9665 case -NFS4ERR_BADSESSION:
9666 case -NFS4ERR_BADSLOT:
9667 case -NFS4ERR_BAD_HIGH_SLOT:
9668 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9669 case -NFS4ERR_DEADSESSION:
9670 nfs4_do_handle_exception(server, err, exception);
9671 }
9672 }
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685 static int nfs41_test_stateid(struct nfs_server *server,
9686 nfs4_stateid *stateid,
9687 const struct cred *cred)
9688 {
9689 struct nfs4_exception exception = {
9690 .interruptible = true,
9691 };
9692 int err;
9693 do {
9694 err = _nfs41_test_stateid(server, stateid, cred);
9695 nfs4_handle_delay_or_session_error(server, err, &exception);
9696 } while (exception.retry);
9697 return err;
9698 }
9699
9700 struct nfs_free_stateid_data {
9701 struct nfs_server *server;
9702 struct nfs41_free_stateid_args args;
9703 struct nfs41_free_stateid_res res;
9704 };
9705
9706 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9707 {
9708 struct nfs_free_stateid_data *data = calldata;
9709 nfs4_setup_sequence(data->server->nfs_client,
9710 &data->args.seq_args,
9711 &data->res.seq_res,
9712 task);
9713 }
9714
9715 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9716 {
9717 struct nfs_free_stateid_data *data = calldata;
9718
9719 nfs41_sequence_done(task, &data->res.seq_res);
9720
9721 switch (task->tk_status) {
9722 case -NFS4ERR_DELAY:
9723 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9724 rpc_restart_call_prepare(task);
9725 }
9726 }
9727
9728 static void nfs41_free_stateid_release(void *calldata)
9729 {
9730 kfree(calldata);
9731 }
9732
9733 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9734 .rpc_call_prepare = nfs41_free_stateid_prepare,
9735 .rpc_call_done = nfs41_free_stateid_done,
9736 .rpc_release = nfs41_free_stateid_release,
9737 };
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749 static int nfs41_free_stateid(struct nfs_server *server,
9750 const nfs4_stateid *stateid,
9751 const struct cred *cred,
9752 bool privileged)
9753 {
9754 struct rpc_message msg = {
9755 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9756 .rpc_cred = cred,
9757 };
9758 struct rpc_task_setup task_setup = {
9759 .rpc_client = server->client,
9760 .rpc_message = &msg,
9761 .callback_ops = &nfs41_free_stateid_ops,
9762 .flags = RPC_TASK_ASYNC,
9763 };
9764 struct nfs_free_stateid_data *data;
9765 struct rpc_task *task;
9766
9767 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9768 &task_setup.rpc_client, &msg);
9769
9770 dprintk("NFS call free_stateid %p\n", stateid);
9771 data = kmalloc(sizeof(*data), GFP_NOFS);
9772 if (!data)
9773 return -ENOMEM;
9774 data->server = server;
9775 nfs4_stateid_copy(&data->args.stateid, stateid);
9776
9777 task_setup.callback_data = data;
9778
9779 msg.rpc_argp = &data->args;
9780 msg.rpc_resp = &data->res;
9781 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
9782 task = rpc_run_task(&task_setup);
9783 if (IS_ERR(task))
9784 return PTR_ERR(task);
9785 rpc_put_task(task);
9786 return 0;
9787 }
9788
9789 static void
9790 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9791 {
9792 const struct cred *cred = lsp->ls_state->owner->so_cred;
9793
9794 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9795 nfs4_free_lock_state(server, lsp);
9796 }
9797
9798 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9799 const nfs4_stateid *s2)
9800 {
9801 if (s1->type != s2->type)
9802 return false;
9803
9804 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9805 return false;
9806
9807 if (s1->seqid == s2->seqid)
9808 return true;
9809
9810 return s1->seqid == 0 || s2->seqid == 0;
9811 }
9812
9813 #endif
9814
9815 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9816 const nfs4_stateid *s2)
9817 {
9818 return nfs4_stateid_match(s1, s2);
9819 }
9820
9821
9822 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9823 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9824 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9825 .recover_open = nfs4_open_reclaim,
9826 .recover_lock = nfs4_lock_reclaim,
9827 .establish_clid = nfs4_init_clientid,
9828 .detect_trunking = nfs40_discover_server_trunking,
9829 };
9830
9831 #if defined(CONFIG_NFS_V4_1)
9832 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9833 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9834 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9835 .recover_open = nfs4_open_reclaim,
9836 .recover_lock = nfs4_lock_reclaim,
9837 .establish_clid = nfs41_init_clientid,
9838 .reclaim_complete = nfs41_proc_reclaim_complete,
9839 .detect_trunking = nfs41_discover_server_trunking,
9840 };
9841 #endif
9842
9843 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9844 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9845 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9846 .recover_open = nfs40_open_expired,
9847 .recover_lock = nfs4_lock_expired,
9848 .establish_clid = nfs4_init_clientid,
9849 };
9850
9851 #if defined(CONFIG_NFS_V4_1)
9852 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9853 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9854 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9855 .recover_open = nfs41_open_expired,
9856 .recover_lock = nfs41_lock_expired,
9857 .establish_clid = nfs41_init_clientid,
9858 };
9859 #endif
9860
9861 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9862 .sched_state_renewal = nfs4_proc_async_renew,
9863 .get_state_renewal_cred = nfs4_get_renew_cred,
9864 .renew_lease = nfs4_proc_renew,
9865 };
9866
9867 #if defined(CONFIG_NFS_V4_1)
9868 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9869 .sched_state_renewal = nfs41_proc_async_sequence,
9870 .get_state_renewal_cred = nfs4_get_machine_cred,
9871 .renew_lease = nfs4_proc_sequence,
9872 };
9873 #endif
9874
9875 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9876 .get_locations = _nfs40_proc_get_locations,
9877 .fsid_present = _nfs40_proc_fsid_present,
9878 };
9879
9880 #if defined(CONFIG_NFS_V4_1)
9881 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9882 .get_locations = _nfs41_proc_get_locations,
9883 .fsid_present = _nfs41_proc_fsid_present,
9884 };
9885 #endif
9886
9887 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9888 .minor_version = 0,
9889 .init_caps = NFS_CAP_READDIRPLUS
9890 | NFS_CAP_ATOMIC_OPEN
9891 | NFS_CAP_POSIX_LOCK,
9892 .init_client = nfs40_init_client,
9893 .shutdown_client = nfs40_shutdown_client,
9894 .match_stateid = nfs4_match_stateid,
9895 .find_root_sec = nfs4_find_root_sec,
9896 .free_lock_state = nfs4_release_lockowner,
9897 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9898 .alloc_seqid = nfs_alloc_seqid,
9899 .call_sync_ops = &nfs40_call_sync_ops,
9900 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9901 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9902 .state_renewal_ops = &nfs40_state_renewal_ops,
9903 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9904 };
9905
9906 #if defined(CONFIG_NFS_V4_1)
9907 static struct nfs_seqid *
9908 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9909 {
9910 return NULL;
9911 }
9912
9913 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9914 .minor_version = 1,
9915 .init_caps = NFS_CAP_READDIRPLUS
9916 | NFS_CAP_ATOMIC_OPEN
9917 | NFS_CAP_POSIX_LOCK
9918 | NFS_CAP_STATEID_NFSV41
9919 | NFS_CAP_ATOMIC_OPEN_V1
9920 | NFS_CAP_LGOPEN,
9921 .init_client = nfs41_init_client,
9922 .shutdown_client = nfs41_shutdown_client,
9923 .match_stateid = nfs41_match_stateid,
9924 .find_root_sec = nfs41_find_root_sec,
9925 .free_lock_state = nfs41_free_lock_state,
9926 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9927 .alloc_seqid = nfs_alloc_no_seqid,
9928 .session_trunk = nfs4_test_session_trunk,
9929 .call_sync_ops = &nfs41_call_sync_ops,
9930 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9931 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9932 .state_renewal_ops = &nfs41_state_renewal_ops,
9933 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9934 };
9935 #endif
9936
9937 #if defined(CONFIG_NFS_V4_2)
9938 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9939 .minor_version = 2,
9940 .init_caps = NFS_CAP_READDIRPLUS
9941 | NFS_CAP_ATOMIC_OPEN
9942 | NFS_CAP_POSIX_LOCK
9943 | NFS_CAP_STATEID_NFSV41
9944 | NFS_CAP_ATOMIC_OPEN_V1
9945 | NFS_CAP_LGOPEN
9946 | NFS_CAP_ALLOCATE
9947 | NFS_CAP_COPY
9948 | NFS_CAP_OFFLOAD_CANCEL
9949 | NFS_CAP_DEALLOCATE
9950 | NFS_CAP_SEEK
9951 | NFS_CAP_LAYOUTSTATS
9952 | NFS_CAP_CLONE
9953 | NFS_CAP_LAYOUTERROR,
9954 .init_client = nfs41_init_client,
9955 .shutdown_client = nfs41_shutdown_client,
9956 .match_stateid = nfs41_match_stateid,
9957 .find_root_sec = nfs41_find_root_sec,
9958 .free_lock_state = nfs41_free_lock_state,
9959 .call_sync_ops = &nfs41_call_sync_ops,
9960 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9961 .alloc_seqid = nfs_alloc_no_seqid,
9962 .session_trunk = nfs4_test_session_trunk,
9963 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9964 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9965 .state_renewal_ops = &nfs41_state_renewal_ops,
9966 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9967 };
9968 #endif
9969
9970 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9971 [0] = &nfs_v4_0_minor_ops,
9972 #if defined(CONFIG_NFS_V4_1)
9973 [1] = &nfs_v4_1_minor_ops,
9974 #endif
9975 #if defined(CONFIG_NFS_V4_2)
9976 [2] = &nfs_v4_2_minor_ops,
9977 #endif
9978 };
9979
9980 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9981 {
9982 ssize_t error, error2;
9983
9984 error = generic_listxattr(dentry, list, size);
9985 if (error < 0)
9986 return error;
9987 if (list) {
9988 list += error;
9989 size -= error;
9990 }
9991
9992 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9993 if (error2 < 0)
9994 return error2;
9995 return error + error2;
9996 }
9997
9998 static const struct inode_operations nfs4_dir_inode_operations = {
9999 .create = nfs_create,
10000 .lookup = nfs_lookup,
10001 .atomic_open = nfs_atomic_open,
10002 .link = nfs_link,
10003 .unlink = nfs_unlink,
10004 .symlink = nfs_symlink,
10005 .mkdir = nfs_mkdir,
10006 .rmdir = nfs_rmdir,
10007 .mknod = nfs_mknod,
10008 .rename = nfs_rename,
10009 .permission = nfs_permission,
10010 .getattr = nfs_getattr,
10011 .setattr = nfs_setattr,
10012 .listxattr = nfs4_listxattr,
10013 };
10014
10015 static const struct inode_operations nfs4_file_inode_operations = {
10016 .permission = nfs_permission,
10017 .getattr = nfs_getattr,
10018 .setattr = nfs_setattr,
10019 .listxattr = nfs4_listxattr,
10020 };
10021
10022 const struct nfs_rpc_ops nfs_v4_clientops = {
10023 .version = 4,
10024 .dentry_ops = &nfs4_dentry_operations,
10025 .dir_inode_ops = &nfs4_dir_inode_operations,
10026 .file_inode_ops = &nfs4_file_inode_operations,
10027 .file_ops = &nfs4_file_operations,
10028 .getroot = nfs4_proc_get_root,
10029 .submount = nfs4_submount,
10030 .try_mount = nfs4_try_mount,
10031 .getattr = nfs4_proc_getattr,
10032 .setattr = nfs4_proc_setattr,
10033 .lookup = nfs4_proc_lookup,
10034 .lookupp = nfs4_proc_lookupp,
10035 .access = nfs4_proc_access,
10036 .readlink = nfs4_proc_readlink,
10037 .create = nfs4_proc_create,
10038 .remove = nfs4_proc_remove,
10039 .unlink_setup = nfs4_proc_unlink_setup,
10040 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10041 .unlink_done = nfs4_proc_unlink_done,
10042 .rename_setup = nfs4_proc_rename_setup,
10043 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10044 .rename_done = nfs4_proc_rename_done,
10045 .link = nfs4_proc_link,
10046 .symlink = nfs4_proc_symlink,
10047 .mkdir = nfs4_proc_mkdir,
10048 .rmdir = nfs4_proc_rmdir,
10049 .readdir = nfs4_proc_readdir,
10050 .mknod = nfs4_proc_mknod,
10051 .statfs = nfs4_proc_statfs,
10052 .fsinfo = nfs4_proc_fsinfo,
10053 .pathconf = nfs4_proc_pathconf,
10054 .set_capabilities = nfs4_server_capabilities,
10055 .decode_dirent = nfs4_decode_dirent,
10056 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10057 .read_setup = nfs4_proc_read_setup,
10058 .read_done = nfs4_read_done,
10059 .write_setup = nfs4_proc_write_setup,
10060 .write_done = nfs4_write_done,
10061 .commit_setup = nfs4_proc_commit_setup,
10062 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10063 .commit_done = nfs4_commit_done,
10064 .lock = nfs4_proc_lock,
10065 .clear_acl_cache = nfs4_zap_acl_attr,
10066 .close_context = nfs4_close_context,
10067 .open_context = nfs4_atomic_open,
10068 .have_delegation = nfs4_have_delegation,
10069 .alloc_client = nfs4_alloc_client,
10070 .init_client = nfs4_init_client,
10071 .free_client = nfs4_free_client,
10072 .create_server = nfs4_create_server,
10073 .clone_server = nfs_clone_server,
10074 };
10075
10076 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10077 .name = XATTR_NAME_NFSV4_ACL,
10078 .list = nfs4_xattr_list_nfs4_acl,
10079 .get = nfs4_xattr_get_nfs4_acl,
10080 .set = nfs4_xattr_set_nfs4_acl,
10081 };
10082
10083 const struct xattr_handler *nfs4_xattr_handlers[] = {
10084 &nfs4_xattr_nfs4_acl_handler,
10085 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10086 &nfs4_xattr_nfs4_label_handler,
10087 #endif
10088 NULL
10089 };
10090
10091
10092
10093
10094
10095