Whamcloud - gitweb
LU-10030 clio: don't use spare bits in iattr.ia_valid
[fs/lustre-release.git] / lustre / llite / file.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/llite/file.c
33  *
34  * Author: Peter Braam <braam@clusterfs.com>
35  * Author: Phil Schwan <phil@clusterfs.com>
36  * Author: Andreas Dilger <adilger@clusterfs.com>
37  */
38
39 #define DEBUG_SUBSYSTEM S_LLITE
40 #include <lustre_dlm.h>
41 #include <linux/pagemap.h>
42 #include <linux/file.h>
43 #include <linux/sched.h>
44 #include <linux/user_namespace.h>
45 #ifdef HAVE_UIDGID_HEADER
46 # include <linux/uidgid.h>
47 #endif
48
49 #include <uapi/linux/lustre/lustre_ioctl.h>
50 #include <lustre_swab.h>
51
52 #include "cl_object.h"
53 #include "llite_internal.h"
54 #include "vvp_internal.h"
55
56 struct split_param {
57         struct inode    *sp_inode;
58         __u16           sp_mirror_id;
59 };
60
61 static int
62 ll_put_grouplock(struct inode *inode, struct file *file, unsigned long arg);
63
64 static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
65                           bool *lease_broken);
66
67 static struct ll_file_data *ll_file_data_get(void)
68 {
69         struct ll_file_data *fd;
70
71         OBD_SLAB_ALLOC_PTR_GFP(fd, ll_file_data_slab, GFP_NOFS);
72         if (fd == NULL)
73                 return NULL;
74
75         fd->fd_write_failed = false;
76
77         return fd;
78 }
79
80 static void ll_file_data_put(struct ll_file_data *fd)
81 {
82         if (fd != NULL)
83                 OBD_SLAB_FREE_PTR(fd, ll_file_data_slab);
84 }
85
86 /**
87  * Packs all the attributes into @op_data for the CLOSE rpc.
88  */
89 static void ll_prepare_close(struct inode *inode, struct md_op_data *op_data,
90                              struct obd_client_handle *och)
91 {
92         ENTRY;
93
94         ll_prep_md_op_data(op_data, inode, NULL, NULL,
95                            0, 0, LUSTRE_OPC_ANY, NULL);
96
97         op_data->op_attr.ia_mode = inode->i_mode;
98         op_data->op_attr.ia_atime = inode->i_atime;
99         op_data->op_attr.ia_mtime = inode->i_mtime;
100         op_data->op_attr.ia_ctime = inode->i_ctime;
101         op_data->op_attr.ia_size = i_size_read(inode);
102         op_data->op_attr.ia_valid |= (ATTR_MODE | ATTR_ATIME | ATTR_ATIME_SET |
103                                       ATTR_MTIME | ATTR_MTIME_SET |
104                                       ATTR_CTIME);
105         op_data->op_xvalid |= OP_XVALID_CTIME_SET;
106         op_data->op_attr_blocks = inode->i_blocks;
107         op_data->op_attr_flags = ll_inode_to_ext_flags(inode->i_flags);
108         if (ll_file_test_flag(ll_i2info(inode), LLIF_PROJECT_INHERIT))
109                 op_data->op_attr_flags |= LUSTRE_PROJINHERIT_FL;
110         op_data->op_open_handle = och->och_open_handle;
111
112         if (och->och_flags & FMODE_WRITE &&
113             ll_file_test_and_clear_flag(ll_i2info(inode), LLIF_DATA_MODIFIED))
114                 /* For HSM: if inode data has been modified, pack it so that
115                  * MDT can set data dirty flag in the archive. */
116                 op_data->op_bias |= MDS_DATA_MODIFIED;
117
118         EXIT;
119 }
120
121 /**
122  * Perform a close, possibly with a bias.
123  * The meaning of "data" depends on the value of "bias".
124  *
125  * If \a bias is MDS_HSM_RELEASE then \a data is a pointer to the data version.
126  * If \a bias is MDS_CLOSE_LAYOUT_SWAP then \a data is a pointer to the inode to
127  * swap layouts with.
128  */
129 static int ll_close_inode_openhandle(struct inode *inode,
130                                      struct obd_client_handle *och,
131                                      enum mds_op_bias bias, void *data)
132 {
133         struct obd_export *md_exp = ll_i2mdexp(inode);
134         const struct ll_inode_info *lli = ll_i2info(inode);
135         struct md_op_data *op_data;
136         struct ptlrpc_request *req = NULL;
137         int rc;
138         ENTRY;
139
140         if (class_exp2obd(md_exp) == NULL) {
141                 CERROR("%s: invalid MDC connection handle closing "DFID"\n",
142                        ll_get_fsname(inode->i_sb, NULL, 0),
143                        PFID(&lli->lli_fid));
144                 GOTO(out, rc = 0);
145         }
146
147         OBD_ALLOC_PTR(op_data);
148         /* We leak openhandle and request here on error, but not much to be
149          * done in OOM case since app won't retry close on error either. */
150         if (op_data == NULL)
151                 GOTO(out, rc = -ENOMEM);
152
153         ll_prepare_close(inode, op_data, och);
154         switch (bias) {
155         case MDS_CLOSE_LAYOUT_MERGE:
156                 /* merge blocks from the victim inode */
157                 op_data->op_attr_blocks += ((struct inode *)data)->i_blocks;
158                 op_data->op_attr.ia_valid |= ATTR_SIZE;
159                 op_data->op_xvalid |= OP_XVALID_BLOCKS;
160         case MDS_CLOSE_LAYOUT_SPLIT:
161         case MDS_CLOSE_LAYOUT_SWAP: {
162                 struct split_param *sp = data;
163
164                 LASSERT(data != NULL);
165                 op_data->op_bias |= bias;
166                 op_data->op_data_version = 0;
167                 op_data->op_lease_handle = och->och_lease_handle;
168                 if (bias == MDS_CLOSE_LAYOUT_SPLIT) {
169                         op_data->op_fid2 = *ll_inode2fid(sp->sp_inode);
170                         op_data->op_mirror_id = sp->sp_mirror_id;
171                 } else {
172                         op_data->op_fid2 = *ll_inode2fid(data);
173                 }
174                 break;
175         }
176
177         case MDS_CLOSE_RESYNC_DONE: {
178                 struct ll_ioc_lease *ioc = data;
179
180                 LASSERT(data != NULL);
181                 op_data->op_attr_blocks +=
182                         ioc->lil_count * op_data->op_attr_blocks;
183                 op_data->op_attr.ia_valid |= ATTR_SIZE;
184                 op_data->op_xvalid |= OP_XVALID_BLOCKS;
185                 op_data->op_bias |= MDS_CLOSE_RESYNC_DONE;
186
187                 op_data->op_lease_handle = och->och_lease_handle;
188                 op_data->op_data = &ioc->lil_ids[0];
189                 op_data->op_data_size =
190                         ioc->lil_count * sizeof(ioc->lil_ids[0]);
191                 break;
192         }
193
194         case MDS_HSM_RELEASE:
195                 LASSERT(data != NULL);
196                 op_data->op_bias |= MDS_HSM_RELEASE;
197                 op_data->op_data_version = *(__u64 *)data;
198                 op_data->op_lease_handle = och->och_lease_handle;
199                 op_data->op_attr.ia_valid |= ATTR_SIZE;
200                 op_data->op_xvalid |= OP_XVALID_BLOCKS;
201                 break;
202
203         default:
204                 LASSERT(data == NULL);
205                 break;
206         }
207
208         if (!(op_data->op_attr.ia_valid & ATTR_SIZE))
209                 op_data->op_xvalid |= OP_XVALID_LAZYSIZE;
210         if (!(op_data->op_xvalid & OP_XVALID_BLOCKS))
211                 op_data->op_xvalid |= OP_XVALID_LAZYBLOCKS;
212
213         rc = md_close(md_exp, op_data, och->och_mod, &req);
214         if (rc != 0 && rc != -EINTR)
215                 CERROR("%s: inode "DFID" mdc close failed: rc = %d\n",
216                        md_exp->exp_obd->obd_name, PFID(&lli->lli_fid), rc);
217
218         if (rc == 0 && op_data->op_bias & bias) {
219                 struct mdt_body *body;
220
221                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
222                 if (!(body->mbo_valid & OBD_MD_CLOSE_INTENT_EXECED))
223                         rc = -EBUSY;
224         }
225
226         ll_finish_md_op_data(op_data);
227         EXIT;
228 out:
229
230         md_clear_open_replay_data(md_exp, och);
231         och->och_open_handle.cookie = DEAD_HANDLE_MAGIC;
232         OBD_FREE_PTR(och);
233
234         ptlrpc_req_finished(req);       /* This is close request */
235         return rc;
236 }
237
238 int ll_md_real_close(struct inode *inode, fmode_t fmode)
239 {
240         struct ll_inode_info *lli = ll_i2info(inode);
241         struct obd_client_handle **och_p;
242         struct obd_client_handle *och;
243         __u64 *och_usecount;
244         int rc = 0;
245         ENTRY;
246
247         if (fmode & FMODE_WRITE) {
248                 och_p = &lli->lli_mds_write_och;
249                 och_usecount = &lli->lli_open_fd_write_count;
250         } else if (fmode & FMODE_EXEC) {
251                 och_p = &lli->lli_mds_exec_och;
252                 och_usecount = &lli->lli_open_fd_exec_count;
253         } else {
254                 LASSERT(fmode & FMODE_READ);
255                 och_p = &lli->lli_mds_read_och;
256                 och_usecount = &lli->lli_open_fd_read_count;
257         }
258
259         mutex_lock(&lli->lli_och_mutex);
260         if (*och_usecount > 0) {
261                 /* There are still users of this handle, so skip
262                  * freeing it. */
263                 mutex_unlock(&lli->lli_och_mutex);
264                 RETURN(0);
265         }
266
267         och = *och_p;
268         *och_p = NULL;
269         mutex_unlock(&lli->lli_och_mutex);
270
271         if (och != NULL) {
272                 /* There might be a race and this handle may already
273                  * be closed. */
274                 rc = ll_close_inode_openhandle(inode, och, 0, NULL);
275         }
276
277         RETURN(rc);
278 }
279
280 static int ll_md_close(struct inode *inode, struct file *file)
281 {
282         union ldlm_policy_data policy = {
283                 .l_inodebits    = { MDS_INODELOCK_OPEN },
284         };
285         __u64 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_TEST_LOCK;
286         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
287         struct ll_inode_info *lli = ll_i2info(inode);
288         struct lustre_handle lockh;
289         enum ldlm_mode lockmode;
290         int rc = 0;
291         ENTRY;
292
293         /* clear group lock, if present */
294         if (unlikely(fd->fd_flags & LL_FILE_GROUP_LOCKED))
295                 ll_put_grouplock(inode, file, fd->fd_grouplock.lg_gid);
296
297         if (fd->fd_lease_och != NULL) {
298                 bool lease_broken;
299
300                 /* Usually the lease is not released when the
301                  * application crashed, we need to release here. */
302                 rc = ll_lease_close(fd->fd_lease_och, inode, &lease_broken);
303                 CDEBUG(rc ? D_ERROR : D_INODE, "Clean up lease "DFID" %d/%d\n",
304                         PFID(&lli->lli_fid), rc, lease_broken);
305
306                 fd->fd_lease_och = NULL;
307         }
308
309         if (fd->fd_och != NULL) {
310                 rc = ll_close_inode_openhandle(inode, fd->fd_och, 0, NULL);
311                 fd->fd_och = NULL;
312                 GOTO(out, rc);
313         }
314
315         /* Let's see if we have good enough OPEN lock on the file and if
316            we can skip talking to MDS */
317         mutex_lock(&lli->lli_och_mutex);
318         if (fd->fd_omode & FMODE_WRITE) {
319                 lockmode = LCK_CW;
320                 LASSERT(lli->lli_open_fd_write_count);
321                 lli->lli_open_fd_write_count--;
322         } else if (fd->fd_omode & FMODE_EXEC) {
323                 lockmode = LCK_PR;
324                 LASSERT(lli->lli_open_fd_exec_count);
325                 lli->lli_open_fd_exec_count--;
326         } else {
327                 lockmode = LCK_CR;
328                 LASSERT(lli->lli_open_fd_read_count);
329                 lli->lli_open_fd_read_count--;
330         }
331         mutex_unlock(&lli->lli_och_mutex);
332
333         if (!md_lock_match(ll_i2mdexp(inode), flags, ll_inode2fid(inode),
334                            LDLM_IBITS, &policy, lockmode, &lockh))
335                 rc = ll_md_real_close(inode, fd->fd_omode);
336
337 out:
338         LUSTRE_FPRIVATE(file) = NULL;
339         ll_file_data_put(fd);
340
341         RETURN(rc);
342 }
343
344 /* While this returns an error code, fput() the caller does not, so we need
345  * to make every effort to clean up all of our state here.  Also, applications
346  * rarely check close errors and even if an error is returned they will not
347  * re-try the close call.
348  */
349 int ll_file_release(struct inode *inode, struct file *file)
350 {
351         struct ll_file_data *fd;
352         struct ll_sb_info *sbi = ll_i2sbi(inode);
353         struct ll_inode_info *lli = ll_i2info(inode);
354         int rc;
355         ENTRY;
356
357         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
358                PFID(ll_inode2fid(inode)), inode);
359
360         if (inode->i_sb->s_root != file_dentry(file))
361                 ll_stats_ops_tally(sbi, LPROC_LL_RELEASE, 1);
362         fd = LUSTRE_FPRIVATE(file);
363         LASSERT(fd != NULL);
364
365         /* The last ref on @file, maybe not the the owner pid of statahead,
366          * because parent and child process can share the same file handle. */
367         if (S_ISDIR(inode->i_mode) && lli->lli_opendir_key == fd)
368                 ll_deauthorize_statahead(inode, fd);
369
370         if (inode->i_sb->s_root == file_dentry(file)) {
371                 LUSTRE_FPRIVATE(file) = NULL;
372                 ll_file_data_put(fd);
373                 RETURN(0);
374         }
375
376         if (!S_ISDIR(inode->i_mode)) {
377                 if (lli->lli_clob != NULL)
378                         lov_read_and_clear_async_rc(lli->lli_clob);
379                 lli->lli_async_rc = 0;
380         }
381
382         rc = ll_md_close(inode, file);
383
384         if (CFS_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_DUMP_LOG, cfs_fail_val))
385                 libcfs_debug_dumplog();
386
387         RETURN(rc);
388 }
389
390 static inline int ll_dom_readpage(void *data, struct page *page)
391 {
392         struct niobuf_local *lnb = data;
393         void *kaddr;
394
395         kaddr = ll_kmap_atomic(page, KM_USER0);
396         memcpy(kaddr, lnb->lnb_data, lnb->lnb_len);
397         if (lnb->lnb_len < PAGE_SIZE)
398                 memset(kaddr + lnb->lnb_len, 0,
399                        PAGE_SIZE - lnb->lnb_len);
400         flush_dcache_page(page);
401         SetPageUptodate(page);
402         ll_kunmap_atomic(kaddr, KM_USER0);
403         unlock_page(page);
404
405         return 0;
406 }
407
408 void ll_dom_finish_open(struct inode *inode, struct ptlrpc_request *req,
409                         struct lookup_intent *it)
410 {
411         struct ll_inode_info *lli = ll_i2info(inode);
412         struct cl_object *obj = lli->lli_clob;
413         struct address_space *mapping = inode->i_mapping;
414         struct page *vmpage;
415         struct niobuf_remote *rnb;
416         char *data;
417         struct lu_env *env;
418         struct cl_io *io;
419         __u16 refcheck;
420         struct lustre_handle lockh;
421         struct ldlm_lock *lock;
422         unsigned long index, start;
423         struct niobuf_local lnb;
424         int rc;
425         bool dom_lock = false;
426
427         ENTRY;
428
429         if (obj == NULL)
430                 RETURN_EXIT;
431
432         if (it->it_lock_mode != 0) {
433                 lockh.cookie = it->it_lock_handle;
434                 lock = ldlm_handle2lock(&lockh);
435                 if (lock != NULL)
436                         dom_lock = ldlm_has_dom(lock);
437                 LDLM_LOCK_PUT(lock);
438         }
439
440         if (!dom_lock)
441                 RETURN_EXIT;
442
443         env = cl_env_get(&refcheck);
444         if (IS_ERR(env))
445                 RETURN_EXIT;
446
447         if (!req_capsule_has_field(&req->rq_pill, &RMF_NIOBUF_INLINE,
448                                    RCL_SERVER))
449                 GOTO(out_env, rc = -ENODATA);
450
451         rnb = req_capsule_server_get(&req->rq_pill, &RMF_NIOBUF_INLINE);
452         data = (char *)rnb + sizeof(*rnb);
453
454         if (rnb == NULL || rnb->rnb_len == 0)
455                 GOTO(out_env, rc = 0);
456
457         CDEBUG(D_INFO, "Get data buffer along with open, len %i, i_size %llu\n",
458                rnb->rnb_len, i_size_read(inode));
459
460         io = vvp_env_thread_io(env);
461         io->ci_obj = obj;
462         io->ci_ignore_layout = 1;
463         rc = cl_io_init(env, io, CIT_MISC, obj);
464         if (rc)
465                 GOTO(out_io, rc);
466
467         lnb.lnb_file_offset = rnb->rnb_offset;
468         start = lnb.lnb_file_offset / PAGE_SIZE;
469         index = 0;
470         LASSERT(lnb.lnb_file_offset % PAGE_SIZE == 0);
471         lnb.lnb_page_offset = 0;
472         do {
473                 struct cl_page *clp;
474
475                 lnb.lnb_data = data + (index << PAGE_SHIFT);
476                 lnb.lnb_len = rnb->rnb_len - (index << PAGE_SHIFT);
477                 if (lnb.lnb_len > PAGE_SIZE)
478                         lnb.lnb_len = PAGE_SIZE;
479
480                 vmpage = read_cache_page(mapping, index + start,
481                                          ll_dom_readpage, &lnb);
482                 if (IS_ERR(vmpage)) {
483                         CWARN("%s: cannot fill page %lu for "DFID
484                               " with data: rc = %li\n",
485                               ll_get_fsname(inode->i_sb, NULL, 0),
486                               index + start, PFID(lu_object_fid(&obj->co_lu)),
487                               PTR_ERR(vmpage));
488                         break;
489                 }
490                 lock_page(vmpage);
491                 if (vmpage->mapping == NULL) {
492                         unlock_page(vmpage);
493                         put_page(vmpage);
494                         /* page was truncated */
495                         GOTO(out_io, rc = -ENODATA);
496                 }
497                 clp = cl_page_find(env, obj, vmpage->index, vmpage,
498                                    CPT_CACHEABLE);
499                 if (IS_ERR(clp)) {
500                         unlock_page(vmpage);
501                         put_page(vmpage);
502                         GOTO(out_io, rc = PTR_ERR(clp));
503                 }
504
505                 /* export page */
506                 cl_page_export(env, clp, 1);
507                 cl_page_put(env, clp);
508                 unlock_page(vmpage);
509                 put_page(vmpage);
510                 index++;
511         } while (rnb->rnb_len > (index << PAGE_SHIFT));
512         rc = 0;
513         EXIT;
514 out_io:
515         cl_io_fini(env, io);
516 out_env:
517         cl_env_put(env, &refcheck);
518 }
519
520 static int ll_intent_file_open(struct dentry *de, void *lmm, int lmmsize,
521                                 struct lookup_intent *itp)
522 {
523         struct ll_sb_info *sbi = ll_i2sbi(de->d_inode);
524         struct dentry *parent = de->d_parent;
525         const char *name = NULL;
526         int len = 0;
527         struct md_op_data *op_data;
528         struct ptlrpc_request *req = NULL;
529         int rc;
530         ENTRY;
531
532         LASSERT(parent != NULL);
533         LASSERT(itp->it_flags & MDS_OPEN_BY_FID);
534
535         /* if server supports open-by-fid, or file name is invalid, don't pack
536          * name in open request */
537         if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_OPEN_BY_FID) &&
538             lu_name_is_valid_2(de->d_name.name, de->d_name.len)) {
539                 name = de->d_name.name;
540                 len = de->d_name.len;
541         }
542
543         op_data = ll_prep_md_op_data(NULL, parent->d_inode, de->d_inode,
544                                      name, len, 0, LUSTRE_OPC_ANY, NULL);
545         if (IS_ERR(op_data))
546                 RETURN(PTR_ERR(op_data));
547         op_data->op_data = lmm;
548         op_data->op_data_size = lmmsize;
549
550         rc = md_intent_lock(sbi->ll_md_exp, op_data, itp, &req,
551                             &ll_md_blocking_ast, 0);
552         ll_finish_md_op_data(op_data);
553         if (rc == -ESTALE) {
554                 /* reason for keep own exit path - don`t flood log
555                  * with messages with -ESTALE errors.
556                  */
557                 if (!it_disposition(itp, DISP_OPEN_OPEN) ||
558                      it_open_error(DISP_OPEN_OPEN, itp))
559                         GOTO(out, rc);
560                 ll_release_openhandle(de, itp);
561                 GOTO(out, rc);
562         }
563
564         if (it_disposition(itp, DISP_LOOKUP_NEG))
565                 GOTO(out, rc = -ENOENT);
566
567         if (rc != 0 || it_open_error(DISP_OPEN_OPEN, itp)) {
568                 rc = rc ? rc : it_open_error(DISP_OPEN_OPEN, itp);
569                 CDEBUG(D_VFSTRACE, "lock enqueue: err: %d\n", rc);
570                 GOTO(out, rc);
571         }
572
573         rc = ll_prep_inode(&de->d_inode, req, NULL, itp);
574
575         if (!rc && itp->it_lock_mode) {
576                 ll_dom_finish_open(de->d_inode, req, itp);
577                 ll_set_lock_data(sbi->ll_md_exp, de->d_inode, itp, NULL);
578         }
579
580 out:
581         ptlrpc_req_finished(req);
582         ll_intent_drop_lock(itp);
583
584         /* We did open by fid, but by the time we got to the server,
585          * the object disappeared. If this is a create, we cannot really
586          * tell the userspace that the file it was trying to create
587          * does not exist. Instead let's return -ESTALE, and the VFS will
588          * retry the create with LOOKUP_REVAL that we are going to catch
589          * in ll_revalidate_dentry() and use lookup then.
590          */
591         if (rc == -ENOENT && itp->it_op & IT_CREAT)
592                 rc = -ESTALE;
593
594         RETURN(rc);
595 }
596
597 static int ll_och_fill(struct obd_export *md_exp, struct lookup_intent *it,
598                        struct obd_client_handle *och)
599 {
600         struct mdt_body *body;
601
602         body = req_capsule_server_get(&it->it_request->rq_pill, &RMF_MDT_BODY);
603         och->och_open_handle = body->mbo_open_handle;
604         och->och_fid = body->mbo_fid1;
605         och->och_lease_handle.cookie = it->it_lock_handle;
606         och->och_magic = OBD_CLIENT_HANDLE_MAGIC;
607         och->och_flags = it->it_flags;
608
609         return md_set_open_replay_data(md_exp, och, it);
610 }
611
612 static int ll_local_open(struct file *file, struct lookup_intent *it,
613                          struct ll_file_data *fd, struct obd_client_handle *och)
614 {
615         struct inode *inode = file_inode(file);
616         ENTRY;
617
618         LASSERT(!LUSTRE_FPRIVATE(file));
619
620         LASSERT(fd != NULL);
621
622         if (och) {
623                 int rc;
624
625                 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
626                 if (rc != 0)
627                         RETURN(rc);
628         }
629
630         LUSTRE_FPRIVATE(file) = fd;
631         ll_readahead_init(inode, &fd->fd_ras);
632         fd->fd_omode = it->it_flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
633
634         /* ll_cl_context initialize */
635         rwlock_init(&fd->fd_lock);
636         INIT_LIST_HEAD(&fd->fd_lccs);
637
638         RETURN(0);
639 }
640
641 /* Open a file, and (for the very first open) create objects on the OSTs at
642  * this time.  If opened with O_LOV_DELAY_CREATE, then we don't do the object
643  * creation or open until ll_lov_setstripe() ioctl is called.
644  *
645  * If we already have the stripe MD locally then we don't request it in
646  * md_open(), by passing a lmm_size = 0.
647  *
648  * It is up to the application to ensure no other processes open this file
649  * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
650  * used.  We might be able to avoid races of that sort by getting lli_open_sem
651  * before returning in the O_LOV_DELAY_CREATE case and dropping it here
652  * or in ll_file_release(), but I'm not sure that is desirable/necessary.
653  */
654 int ll_file_open(struct inode *inode, struct file *file)
655 {
656         struct ll_inode_info *lli = ll_i2info(inode);
657         struct lookup_intent *it, oit = { .it_op = IT_OPEN,
658                                           .it_flags = file->f_flags };
659         struct obd_client_handle **och_p = NULL;
660         __u64 *och_usecount = NULL;
661         struct ll_file_data *fd;
662         int rc = 0;
663         ENTRY;
664
665         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), flags %o\n",
666                PFID(ll_inode2fid(inode)), inode, file->f_flags);
667
668         it = file->private_data; /* XXX: compat macro */
669         file->private_data = NULL; /* prevent ll_local_open assertion */
670
671         fd = ll_file_data_get();
672         if (fd == NULL)
673                 GOTO(out_nofiledata, rc = -ENOMEM);
674
675         fd->fd_file = file;
676         if (S_ISDIR(inode->i_mode))
677                 ll_authorize_statahead(inode, fd);
678
679         if (inode->i_sb->s_root == file_dentry(file)) {
680                 LUSTRE_FPRIVATE(file) = fd;
681                 RETURN(0);
682         }
683
684         if (!it || !it->it_disposition) {
685                 /* Convert f_flags into access mode. We cannot use file->f_mode,
686                  * because everything but O_ACCMODE mask was stripped from
687                  * there */
688                 if ((oit.it_flags + 1) & O_ACCMODE)
689                         oit.it_flags++;
690                 if (file->f_flags & O_TRUNC)
691                         oit.it_flags |= FMODE_WRITE;
692
693                 /* kernel only call f_op->open in dentry_open.  filp_open calls
694                  * dentry_open after call to open_namei that checks permissions.
695                  * Only nfsd_open call dentry_open directly without checking
696                  * permissions and because of that this code below is safe. */
697                 if (oit.it_flags & (FMODE_WRITE | FMODE_READ))
698                         oit.it_flags |= MDS_OPEN_OWNEROVERRIDE;
699
700                 /* We do not want O_EXCL here, presumably we opened the file
701                  * already? XXX - NFS implications? */
702                 oit.it_flags &= ~O_EXCL;
703
704                 /* bug20584, if "it_flags" contains O_CREAT, the file will be
705                  * created if necessary, then "IT_CREAT" should be set to keep
706                  * consistent with it */
707                 if (oit.it_flags & O_CREAT)
708                         oit.it_op |= IT_CREAT;
709
710                 it = &oit;
711         }
712
713 restart:
714         /* Let's see if we have file open on MDS already. */
715         if (it->it_flags & FMODE_WRITE) {
716                 och_p = &lli->lli_mds_write_och;
717                 och_usecount = &lli->lli_open_fd_write_count;
718         } else if (it->it_flags & FMODE_EXEC) {
719                 och_p = &lli->lli_mds_exec_och;
720                 och_usecount = &lli->lli_open_fd_exec_count;
721          } else {
722                 och_p = &lli->lli_mds_read_och;
723                 och_usecount = &lli->lli_open_fd_read_count;
724         }
725
726         mutex_lock(&lli->lli_och_mutex);
727         if (*och_p) { /* Open handle is present */
728                 if (it_disposition(it, DISP_OPEN_OPEN)) {
729                         /* Well, there's extra open request that we do not need,
730                            let's close it somehow. This will decref request. */
731                         rc = it_open_error(DISP_OPEN_OPEN, it);
732                         if (rc) {
733                                 mutex_unlock(&lli->lli_och_mutex);
734                                 GOTO(out_openerr, rc);
735                         }
736
737                         ll_release_openhandle(file_dentry(file), it);
738                 }
739                 (*och_usecount)++;
740
741                 rc = ll_local_open(file, it, fd, NULL);
742                 if (rc) {
743                         (*och_usecount)--;
744                         mutex_unlock(&lli->lli_och_mutex);
745                         GOTO(out_openerr, rc);
746                 }
747         } else {
748                 LASSERT(*och_usecount == 0);
749                 if (!it->it_disposition) {
750                         struct ll_dentry_data *ldd = ll_d2d(file->f_path.dentry);
751                         /* We cannot just request lock handle now, new ELC code
752                            means that one of other OPEN locks for this file
753                            could be cancelled, and since blocking ast handler
754                            would attempt to grab och_mutex as well, that would
755                            result in a deadlock */
756                         mutex_unlock(&lli->lli_och_mutex);
757                         /*
758                          * Normally called under two situations:
759                          * 1. NFS export.
760                          * 2. A race/condition on MDS resulting in no open
761                          *    handle to be returned from LOOKUP|OPEN request,
762                          *    for example if the target entry was a symlink.
763                          *
764                          *  Only fetch MDS_OPEN_LOCK if this is in NFS path,
765                          *  marked by a bit set in ll_iget_for_nfs. Clear the
766                          *  bit so that it's not confusing later callers.
767                          *
768                          *  NB; when ldd is NULL, it must have come via normal
769                          *  lookup path only, since ll_iget_for_nfs always calls
770                          *  ll_d_init().
771                          */
772                         if (ldd && ldd->lld_nfs_dentry) {
773                                 ldd->lld_nfs_dentry = 0;
774                                 it->it_flags |= MDS_OPEN_LOCK;
775                         }
776
777                          /*
778                          * Always specify MDS_OPEN_BY_FID because we don't want
779                          * to get file with different fid.
780                          */
781                         it->it_flags |= MDS_OPEN_BY_FID;
782                         rc = ll_intent_file_open(file_dentry(file), NULL, 0,
783                                                  it);
784                         if (rc)
785                                 GOTO(out_openerr, rc);
786
787                         goto restart;
788                 }
789                 OBD_ALLOC(*och_p, sizeof (struct obd_client_handle));
790                 if (!*och_p)
791                         GOTO(out_och_free, rc = -ENOMEM);
792
793                 (*och_usecount)++;
794
795                 /* md_intent_lock() didn't get a request ref if there was an
796                  * open error, so don't do cleanup on the request here
797                  * (bug 3430) */
798                 /* XXX (green): Should not we bail out on any error here, not
799                  * just open error? */
800                 rc = it_open_error(DISP_OPEN_OPEN, it);
801                 if (rc != 0)
802                         GOTO(out_och_free, rc);
803
804                 LASSERTF(it_disposition(it, DISP_ENQ_OPEN_REF),
805                          "inode %p: disposition %x, status %d\n", inode,
806                          it_disposition(it, ~0), it->it_status);
807
808                 rc = ll_local_open(file, it, fd, *och_p);
809                 if (rc)
810                         GOTO(out_och_free, rc);
811         }
812         mutex_unlock(&lli->lli_och_mutex);
813         fd = NULL;
814
815         /* Must do this outside lli_och_mutex lock to prevent deadlock where
816            different kind of OPEN lock for this same inode gets cancelled
817            by ldlm_cancel_lru */
818         if (!S_ISREG(inode->i_mode))
819                 GOTO(out_och_free, rc);
820
821         cl_lov_delay_create_clear(&file->f_flags);
822         GOTO(out_och_free, rc);
823
824 out_och_free:
825         if (rc) {
826                 if (och_p && *och_p) {
827                         OBD_FREE(*och_p, sizeof (struct obd_client_handle));
828                         *och_p = NULL; /* OBD_FREE writes some magic there */
829                         (*och_usecount)--;
830                 }
831                 mutex_unlock(&lli->lli_och_mutex);
832
833 out_openerr:
834                 if (lli->lli_opendir_key == fd)
835                         ll_deauthorize_statahead(inode, fd);
836                 if (fd != NULL)
837                         ll_file_data_put(fd);
838         } else {
839                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_OPEN, 1);
840         }
841
842 out_nofiledata:
843         if (it && it_disposition(it, DISP_ENQ_OPEN_REF)) {
844                 ptlrpc_req_finished(it->it_request);
845                 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
846         }
847
848         return rc;
849 }
850
851 static int ll_md_blocking_lease_ast(struct ldlm_lock *lock,
852                         struct ldlm_lock_desc *desc, void *data, int flag)
853 {
854         int rc;
855         struct lustre_handle lockh;
856         ENTRY;
857
858         switch (flag) {
859         case LDLM_CB_BLOCKING:
860                 ldlm_lock2handle(lock, &lockh);
861                 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
862                 if (rc < 0) {
863                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
864                         RETURN(rc);
865                 }
866                 break;
867         case LDLM_CB_CANCELING:
868                 /* do nothing */
869                 break;
870         }
871         RETURN(0);
872 }
873
874 /**
875  * When setting a lease on a file, we take ownership of the lli_mds_*_och
876  * and save it as fd->fd_och so as to force client to reopen the file even
877  * if it has an open lock in cache already.
878  */
879 static int ll_lease_och_acquire(struct inode *inode, struct file *file,
880                                 struct lustre_handle *old_open_handle)
881 {
882         struct ll_inode_info *lli = ll_i2info(inode);
883         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
884         struct obd_client_handle **och_p;
885         __u64 *och_usecount;
886         int rc = 0;
887         ENTRY;
888
889         /* Get the openhandle of the file */
890         mutex_lock(&lli->lli_och_mutex);
891         if (fd->fd_lease_och != NULL)
892                 GOTO(out_unlock, rc = -EBUSY);
893
894         if (fd->fd_och == NULL) {
895                 if (file->f_mode & FMODE_WRITE) {
896                         LASSERT(lli->lli_mds_write_och != NULL);
897                         och_p = &lli->lli_mds_write_och;
898                         och_usecount = &lli->lli_open_fd_write_count;
899                 } else {
900                         LASSERT(lli->lli_mds_read_och != NULL);
901                         och_p = &lli->lli_mds_read_och;
902                         och_usecount = &lli->lli_open_fd_read_count;
903                 }
904
905                 if (*och_usecount > 1)
906                         GOTO(out_unlock, rc = -EBUSY);
907
908                 fd->fd_och = *och_p;
909                 *och_usecount = 0;
910                 *och_p = NULL;
911         }
912
913         *old_open_handle = fd->fd_och->och_open_handle;
914
915         EXIT;
916 out_unlock:
917         mutex_unlock(&lli->lli_och_mutex);
918         return rc;
919 }
920
921 /**
922  * Release ownership on lli_mds_*_och when putting back a file lease.
923  */
924 static int ll_lease_och_release(struct inode *inode, struct file *file)
925 {
926         struct ll_inode_info *lli = ll_i2info(inode);
927         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
928         struct obd_client_handle **och_p;
929         struct obd_client_handle *old_och = NULL;
930         __u64 *och_usecount;
931         int rc = 0;
932         ENTRY;
933
934         mutex_lock(&lli->lli_och_mutex);
935         if (file->f_mode & FMODE_WRITE) {
936                 och_p = &lli->lli_mds_write_och;
937                 och_usecount = &lli->lli_open_fd_write_count;
938         } else {
939                 och_p = &lli->lli_mds_read_och;
940                 och_usecount = &lli->lli_open_fd_read_count;
941         }
942
943         /* The file may have been open by another process (broken lease) so
944          * *och_p is not NULL. In this case we should simply increase usecount
945          * and close fd_och.
946          */
947         if (*och_p != NULL) {
948                 old_och = fd->fd_och;
949                 (*och_usecount)++;
950         } else {
951                 *och_p = fd->fd_och;
952                 *och_usecount = 1;
953         }
954         fd->fd_och = NULL;
955         mutex_unlock(&lli->lli_och_mutex);
956
957         if (old_och != NULL)
958                 rc = ll_close_inode_openhandle(inode, old_och, 0, NULL);
959
960         RETURN(rc);
961 }
962
963 /**
964  * Acquire a lease and open the file.
965  */
966 static struct obd_client_handle *
967 ll_lease_open(struct inode *inode, struct file *file, fmode_t fmode,
968               __u64 open_flags)
969 {
970         struct lookup_intent it = { .it_op = IT_OPEN };
971         struct ll_sb_info *sbi = ll_i2sbi(inode);
972         struct md_op_data *op_data;
973         struct ptlrpc_request *req = NULL;
974         struct lustre_handle old_open_handle = { 0 };
975         struct obd_client_handle *och = NULL;
976         int rc;
977         int rc2;
978         ENTRY;
979
980         if (fmode != FMODE_WRITE && fmode != FMODE_READ)
981                 RETURN(ERR_PTR(-EINVAL));
982
983         if (file != NULL) {
984                 if (!(fmode & file->f_mode) || (file->f_mode & FMODE_EXEC))
985                         RETURN(ERR_PTR(-EPERM));
986
987                 rc = ll_lease_och_acquire(inode, file, &old_open_handle);
988                 if (rc)
989                         RETURN(ERR_PTR(rc));
990         }
991
992         OBD_ALLOC_PTR(och);
993         if (och == NULL)
994                 RETURN(ERR_PTR(-ENOMEM));
995
996         op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
997                                         LUSTRE_OPC_ANY, NULL);
998         if (IS_ERR(op_data))
999                 GOTO(out, rc = PTR_ERR(op_data));
1000
1001         /* To tell the MDT this openhandle is from the same owner */
1002         op_data->op_open_handle = old_open_handle;
1003
1004         it.it_flags = fmode | open_flags;
1005         it.it_flags |= MDS_OPEN_LOCK | MDS_OPEN_BY_FID | MDS_OPEN_LEASE;
1006         rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
1007                             &ll_md_blocking_lease_ast,
1008         /* LDLM_FL_NO_LRU: To not put the lease lock into LRU list, otherwise
1009          * it can be cancelled which may mislead applications that the lease is
1010          * broken;
1011          * LDLM_FL_EXCL: Set this flag so that it won't be matched by normal
1012          * open in ll_md_blocking_ast(). Otherwise as ll_md_blocking_lease_ast
1013          * doesn't deal with openhandle, so normal openhandle will be leaked. */
1014                             LDLM_FL_NO_LRU | LDLM_FL_EXCL);
1015         ll_finish_md_op_data(op_data);
1016         ptlrpc_req_finished(req);
1017         if (rc < 0)
1018                 GOTO(out_release_it, rc);
1019
1020         if (it_disposition(&it, DISP_LOOKUP_NEG))
1021                 GOTO(out_release_it, rc = -ENOENT);
1022
1023         rc = it_open_error(DISP_OPEN_OPEN, &it);
1024         if (rc)
1025                 GOTO(out_release_it, rc);
1026
1027         LASSERT(it_disposition(&it, DISP_ENQ_OPEN_REF));
1028         ll_och_fill(sbi->ll_md_exp, &it, och);
1029
1030         if (!it_disposition(&it, DISP_OPEN_LEASE)) /* old server? */
1031                 GOTO(out_close, rc = -EOPNOTSUPP);
1032
1033         /* already get lease, handle lease lock */
1034         ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
1035         if (it.it_lock_mode == 0 ||
1036             it.it_lock_bits != MDS_INODELOCK_OPEN) {
1037                 /* open lock must return for lease */
1038                 CERROR(DFID "lease granted but no open lock, %d/%llu.\n",
1039                         PFID(ll_inode2fid(inode)), it.it_lock_mode,
1040                         it.it_lock_bits);
1041                 GOTO(out_close, rc = -EPROTO);
1042         }
1043
1044         ll_intent_release(&it);
1045         RETURN(och);
1046
1047 out_close:
1048         /* Cancel open lock */
1049         if (it.it_lock_mode != 0) {
1050                 ldlm_lock_decref_and_cancel(&och->och_lease_handle,
1051                                             it.it_lock_mode);
1052                 it.it_lock_mode = 0;
1053                 och->och_lease_handle.cookie = 0ULL;
1054         }
1055         rc2 = ll_close_inode_openhandle(inode, och, 0, NULL);
1056         if (rc2 < 0)
1057                 CERROR("%s: error closing file "DFID": %d\n",
1058                        ll_get_fsname(inode->i_sb, NULL, 0),
1059                        PFID(&ll_i2info(inode)->lli_fid), rc2);
1060         och = NULL; /* och has been freed in ll_close_inode_openhandle() */
1061 out_release_it:
1062         ll_intent_release(&it);
1063 out:
1064         if (och != NULL)
1065                 OBD_FREE_PTR(och);
1066         RETURN(ERR_PTR(rc));
1067 }
1068
1069 /**
1070  * Check whether a layout swap can be done between two inodes.
1071  *
1072  * \param[in] inode1  First inode to check
1073  * \param[in] inode2  Second inode to check
1074  *
1075  * \retval 0 on success, layout swap can be performed between both inodes
1076  * \retval negative error code if requirements are not met
1077  */
1078 static int ll_check_swap_layouts_validity(struct inode *inode1,
1079                                           struct inode *inode2)
1080 {
1081         if (!S_ISREG(inode1->i_mode) || !S_ISREG(inode2->i_mode))
1082                 return -EINVAL;
1083
1084         if (inode_permission(inode1, MAY_WRITE) ||
1085             inode_permission(inode2, MAY_WRITE))
1086                 return -EPERM;
1087
1088         if (inode1->i_sb != inode2->i_sb)
1089                 return -EXDEV;
1090
1091         return 0;
1092 }
1093
1094 static int ll_swap_layouts_close(struct obd_client_handle *och,
1095                                  struct inode *inode, struct inode *inode2)
1096 {
1097         const struct lu_fid     *fid1 = ll_inode2fid(inode);
1098         const struct lu_fid     *fid2;
1099         int                      rc;
1100         ENTRY;
1101
1102         CDEBUG(D_INODE, "%s: biased close of file "DFID"\n",
1103                ll_get_fsname(inode->i_sb, NULL, 0), PFID(fid1));
1104
1105         rc = ll_check_swap_layouts_validity(inode, inode2);
1106         if (rc < 0)
1107                 GOTO(out_free_och, rc);
1108
1109         /* We now know that inode2 is a lustre inode */
1110         fid2 = ll_inode2fid(inode2);
1111
1112         rc = lu_fid_cmp(fid1, fid2);
1113         if (rc == 0)
1114                 GOTO(out_free_och, rc = -EINVAL);
1115
1116         /* Close the file and {swap,merge} layouts between inode & inode2.
1117          * NB: lease lock handle is released in mdc_close_layout_swap_pack()
1118          * because we still need it to pack l_remote_handle to MDT. */
1119         rc = ll_close_inode_openhandle(inode, och, MDS_CLOSE_LAYOUT_SWAP,
1120                                        inode2);
1121
1122         och = NULL; /* freed in ll_close_inode_openhandle() */
1123
1124 out_free_och:
1125         if (och != NULL)
1126                 OBD_FREE_PTR(och);
1127
1128         RETURN(rc);
1129 }
1130
1131 /**
1132  * Release lease and close the file.
1133  * It will check if the lease has ever broken.
1134  */
1135 static int ll_lease_close_intent(struct obd_client_handle *och,
1136                                  struct inode *inode,
1137                                  bool *lease_broken, enum mds_op_bias bias,
1138                                  void *data)
1139 {
1140         struct ldlm_lock *lock;
1141         bool cancelled = true;
1142         int rc;
1143         ENTRY;
1144
1145         lock = ldlm_handle2lock(&och->och_lease_handle);
1146         if (lock != NULL) {
1147                 lock_res_and_lock(lock);
1148                 cancelled = ldlm_is_cancel(lock);
1149                 unlock_res_and_lock(lock);
1150                 LDLM_LOCK_PUT(lock);
1151         }
1152
1153         CDEBUG(D_INODE, "lease for "DFID" broken? %d, bias: %x\n",
1154                PFID(&ll_i2info(inode)->lli_fid), cancelled, bias);
1155
1156         if (lease_broken != NULL)
1157                 *lease_broken = cancelled;
1158
1159         if (!cancelled && !bias)
1160                 ldlm_cli_cancel(&och->och_lease_handle, 0);
1161
1162         if (cancelled) { /* no need to excute intent */
1163                 bias = 0;
1164                 data = NULL;
1165         }
1166
1167         rc = ll_close_inode_openhandle(inode, och, bias, data);
1168         RETURN(rc);
1169 }
1170
1171 static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
1172                           bool *lease_broken)
1173 {
1174         return ll_lease_close_intent(och, inode, lease_broken, 0, NULL);
1175 }
1176
1177 /**
1178  * After lease is taken, send the RPC MDS_REINT_RESYNC to the MDT
1179  */
1180 static int ll_lease_file_resync(struct obd_client_handle *och,
1181                                 struct inode *inode)
1182 {
1183         struct ll_sb_info *sbi = ll_i2sbi(inode);
1184         struct md_op_data *op_data;
1185         __u64 data_version_unused;
1186         int rc;
1187         ENTRY;
1188
1189         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
1190                                      LUSTRE_OPC_ANY, NULL);
1191         if (IS_ERR(op_data))
1192                 RETURN(PTR_ERR(op_data));
1193
1194         /* before starting file resync, it's necessary to clean up page cache
1195          * in client memory, otherwise once the layout version is increased,
1196          * writing back cached data will be denied the OSTs. */
1197         rc = ll_data_version(inode, &data_version_unused, LL_DV_WR_FLUSH);
1198         if (rc)
1199                 GOTO(out, rc);
1200
1201         op_data->op_lease_handle = och->och_lease_handle;
1202         rc = md_file_resync(sbi->ll_md_exp, op_data);
1203         if (rc)
1204                 GOTO(out, rc);
1205
1206         EXIT;
1207 out:
1208         ll_finish_md_op_data(op_data);
1209         return rc;
1210 }
1211
1212 int ll_merge_attr(const struct lu_env *env, struct inode *inode)
1213 {
1214         struct ll_inode_info *lli = ll_i2info(inode);
1215         struct cl_object *obj = lli->lli_clob;
1216         struct cl_attr *attr = vvp_env_thread_attr(env);
1217         s64 atime;
1218         s64 mtime;
1219         s64 ctime;
1220         int rc = 0;
1221
1222         ENTRY;
1223
1224         ll_inode_size_lock(inode);
1225
1226         /* Merge timestamps the most recently obtained from MDS with
1227          * timestamps obtained from OSTs.
1228          *
1229          * Do not overwrite atime of inode because it may be refreshed
1230          * by file_accessed() function. If the read was served by cache
1231          * data, there is no RPC to be sent so that atime may not be
1232          * transferred to OSTs at all. MDT only updates atime at close time
1233          * if it's at least 'mdd.*.atime_diff' older.
1234          * All in all, the atime in Lustre does not strictly comply with
1235          * POSIX. Solving this problem needs to send an RPC to MDT for each
1236          * read, this will hurt performance. */
1237         if (LTIME_S(inode->i_atime) < lli->lli_atime || lli->lli_update_atime) {
1238                 LTIME_S(inode->i_atime) = lli->lli_atime;
1239                 lli->lli_update_atime = 0;
1240         }
1241         LTIME_S(inode->i_mtime) = lli->lli_mtime;
1242         LTIME_S(inode->i_ctime) = lli->lli_ctime;
1243
1244         atime = LTIME_S(inode->i_atime);
1245         mtime = LTIME_S(inode->i_mtime);
1246         ctime = LTIME_S(inode->i_ctime);
1247
1248         cl_object_attr_lock(obj);
1249         if (OBD_FAIL_CHECK(OBD_FAIL_MDC_MERGE))
1250                 rc = -EINVAL;
1251         else
1252                 rc = cl_object_attr_get(env, obj, attr);
1253         cl_object_attr_unlock(obj);
1254
1255         if (rc != 0)
1256                 GOTO(out_size_unlock, rc = (rc == -ENODATA ? 0 : rc));
1257
1258         if (atime < attr->cat_atime)
1259                 atime = attr->cat_atime;
1260
1261         if (ctime < attr->cat_ctime)
1262                 ctime = attr->cat_ctime;
1263
1264         if (mtime < attr->cat_mtime)
1265                 mtime = attr->cat_mtime;
1266
1267         CDEBUG(D_VFSTRACE, DFID" updating i_size %llu\n",
1268                PFID(&lli->lli_fid), attr->cat_size);
1269
1270         i_size_write(inode, attr->cat_size);
1271         inode->i_blocks = attr->cat_blocks;
1272
1273         LTIME_S(inode->i_atime) = atime;
1274         LTIME_S(inode->i_mtime) = mtime;
1275         LTIME_S(inode->i_ctime) = ctime;
1276
1277 out_size_unlock:
1278         ll_inode_size_unlock(inode);
1279
1280         RETURN(rc);
1281 }
1282
1283 /**
1284  * Set designated mirror for I/O.
1285  *
1286  * So far only read, write, and truncated can support to issue I/O to
1287  * designated mirror.
1288  */
1289 void ll_io_set_mirror(struct cl_io *io, const struct file *file)
1290 {
1291         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1292
1293         /* clear layout version for generic(non-resync) I/O in case it carries
1294          * stale layout version due to I/O restart */
1295         io->ci_layout_version = 0;
1296
1297         /* FLR: disable non-delay for designated mirror I/O because obviously
1298          * only one mirror is available */
1299         if (fd->fd_designated_mirror > 0) {
1300                 io->ci_ndelay = 0;
1301                 io->ci_designated_mirror = fd->fd_designated_mirror;
1302                 io->ci_layout_version = fd->fd_layout_version;
1303                 io->ci_pio = 0; /* doesn't have a mechanism to pass mirror
1304                                  * io to ptasks */
1305         }
1306
1307         CDEBUG(D_VFSTRACE, "%s: desiginated mirror: %d\n",
1308                file->f_path.dentry->d_name.name, io->ci_designated_mirror);
1309 }
1310
1311 static bool file_is_noatime(const struct file *file)
1312 {
1313         const struct vfsmount *mnt = file->f_path.mnt;
1314         const struct inode *inode = file_inode((struct file *)file);
1315
1316         /* Adapted from file_accessed() and touch_atime().*/
1317         if (file->f_flags & O_NOATIME)
1318                 return true;
1319
1320         if (inode->i_flags & S_NOATIME)
1321                 return true;
1322
1323         if (IS_NOATIME(inode))
1324                 return true;
1325
1326         if (mnt->mnt_flags & (MNT_NOATIME | MNT_READONLY))
1327                 return true;
1328
1329         if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1330                 return true;
1331
1332         if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
1333                 return true;
1334
1335         return false;
1336 }
1337
1338 static int ll_file_io_ptask(struct cfs_ptask *ptask);
1339
1340 static void ll_io_init(struct cl_io *io, struct file *file, enum cl_io_type iot)
1341 {
1342         struct inode *inode = file_inode(file);
1343         struct ll_file_data *fd  = LUSTRE_FPRIVATE(file);
1344
1345         memset(&io->u.ci_rw.rw_iter, 0, sizeof(io->u.ci_rw.rw_iter));
1346         init_sync_kiocb(&io->u.ci_rw.rw_iocb, file);
1347         io->u.ci_rw.rw_file = file;
1348         io->u.ci_rw.rw_ptask = ll_file_io_ptask;
1349         io->u.ci_rw.rw_nonblock = !!(file->f_flags & O_NONBLOCK);
1350         io->ci_lock_no_expand = fd->ll_lock_no_expand;
1351
1352         if (iot == CIT_WRITE) {
1353                 io->u.ci_rw.rw_append = !!(file->f_flags & O_APPEND);
1354                 io->u.ci_rw.rw_sync   = !!(file->f_flags & O_SYNC ||
1355                                            file->f_flags & O_DIRECT ||
1356                                            IS_SYNC(inode));
1357         }
1358         io->ci_obj = ll_i2info(inode)->lli_clob;
1359         io->ci_lockreq = CILR_MAYBE;
1360         if (ll_file_nolock(file)) {
1361                 io->ci_lockreq = CILR_NEVER;
1362                 io->ci_no_srvlock = 1;
1363         } else if (file->f_flags & O_APPEND) {
1364                 io->ci_lockreq = CILR_MANDATORY;
1365         }
1366         io->ci_noatime = file_is_noatime(file);
1367         if (ll_i2sbi(inode)->ll_flags & LL_SBI_PIO)
1368                 io->ci_pio = !io->u.ci_rw.rw_append;
1369         else
1370                 io->ci_pio = 0;
1371
1372         /* FLR: only use non-delay I/O for read as there is only one
1373          * avaliable mirror for write. */
1374         io->ci_ndelay = !(iot == CIT_WRITE);
1375
1376         ll_io_set_mirror(io, file);
1377 }
1378
1379 static int ll_file_io_ptask(struct cfs_ptask *ptask)
1380 {
1381         struct cl_io_pt *pt = ptask->pt_cbdata;
1382         struct file *file = pt->cip_file;
1383         struct lu_env *env;
1384         struct cl_io *io;
1385         loff_t pos = pt->cip_pos;
1386         int rc;
1387         __u16 refcheck;
1388         ENTRY;
1389
1390         CDEBUG(D_VFSTRACE, "%s: %s range: [%llu, %llu)\n",
1391                 file_dentry(file)->d_name.name,
1392                 pt->cip_iot == CIT_READ ? "read" : "write",
1393                 pos, pos + pt->cip_count);
1394
1395         env = cl_env_get(&refcheck);
1396         if (IS_ERR(env))
1397                 RETURN(PTR_ERR(env));
1398
1399         io = vvp_env_thread_io(env);
1400         ll_io_init(io, file, pt->cip_iot);
1401         io->u.ci_rw.rw_iter = pt->cip_iter;
1402         io->u.ci_rw.rw_iocb = pt->cip_iocb;
1403         io->ci_pio = 0; /* It's already in parallel task */
1404
1405         rc = cl_io_rw_init(env, io, pt->cip_iot, pos,
1406                            pt->cip_count - pt->cip_result);
1407         if (!rc) {
1408                 struct vvp_io *vio = vvp_env_io(env);
1409
1410                 vio->vui_io_subtype = IO_NORMAL;
1411                 vio->vui_fd = LUSTRE_FPRIVATE(file);
1412
1413                 ll_cl_add(file, env, io, LCC_RW);
1414                 rc = cl_io_loop(env, io);
1415                 ll_cl_remove(file, env);
1416         } else {
1417                 /* cl_io_rw_init() handled IO */
1418                 rc = io->ci_result;
1419         }
1420
1421         if (OBD_FAIL_CHECK_RESET(OBD_FAIL_LLITE_PTASK_IO_FAIL, 0)) {
1422                 if (io->ci_nob > 0)
1423                         io->ci_nob /= 2;
1424                 rc = -EIO;
1425         }
1426
1427         if (io->ci_nob > 0) {
1428                 pt->cip_result += io->ci_nob;
1429                 iov_iter_advance(&pt->cip_iter, io->ci_nob);
1430                 pos += io->ci_nob;
1431                 pt->cip_iocb.ki_pos = pos;
1432 #ifdef HAVE_KIOCB_KI_LEFT
1433                 pt->cip_iocb.ki_left = pt->cip_count - pt->cip_result;
1434 #elif defined(HAVE_KI_NBYTES)
1435                 pt->cip_iocb.ki_nbytes = pt->cip_count - pt->cip_result;
1436 #endif
1437         }
1438
1439         cl_io_fini(env, io);
1440         cl_env_put(env, &refcheck);
1441
1442         pt->cip_need_restart = io->ci_need_restart;
1443
1444         CDEBUG(D_VFSTRACE, "%s: %s ret: %zd, rc: %d\n",
1445                 file_dentry(file)->d_name.name,
1446                 pt->cip_iot == CIT_READ ? "read" : "write",
1447                 pt->cip_result, rc);
1448
1449         RETURN(pt->cip_result > 0 ? 0 : rc);
1450 }
1451
1452 static ssize_t
1453 ll_file_io_generic(const struct lu_env *env, struct vvp_io_args *args,
1454                    struct file *file, enum cl_io_type iot,
1455                    loff_t *ppos, size_t count)
1456 {
1457         struct range_lock       range;
1458         struct vvp_io           *vio = vvp_env_io(env);
1459         struct inode            *inode = file_inode(file);
1460         struct ll_inode_info    *lli = ll_i2info(inode);
1461         struct ll_file_data     *fd  = LUSTRE_FPRIVATE(file);
1462         struct cl_io            *io;
1463         loff_t                  pos = *ppos;
1464         ssize_t                 result = 0;
1465         int                     rc = 0;
1466         unsigned                retried = 0;
1467         bool                    restarted = false;
1468
1469         ENTRY;
1470
1471         CDEBUG(D_VFSTRACE, "%s: %s range: [%llu, %llu)\n",
1472                 file_dentry(file)->d_name.name,
1473                 iot == CIT_READ ? "read" : "write", pos, pos + count);
1474
1475 restart:
1476         io = vvp_env_thread_io(env);
1477         ll_io_init(io, file, iot);
1478         if (args->via_io_subtype == IO_NORMAL) {
1479                 io->u.ci_rw.rw_iter = *args->u.normal.via_iter;
1480                 io->u.ci_rw.rw_iocb = *args->u.normal.via_iocb;
1481         }
1482         if (args->via_io_subtype != IO_NORMAL || restarted)
1483                 io->ci_pio = 0;
1484         io->ci_ndelay_tried = retried;
1485
1486         if (cl_io_rw_init(env, io, iot, pos, count) == 0) {
1487                 bool range_locked = false;
1488
1489                 if (file->f_flags & O_APPEND)
1490                         range_lock_init(&range, 0, LUSTRE_EOF);
1491                 else
1492                         range_lock_init(&range, pos, pos + count - 1);
1493
1494                 vio->vui_fd  = LUSTRE_FPRIVATE(file);
1495                 vio->vui_io_subtype = args->via_io_subtype;
1496
1497                 switch (vio->vui_io_subtype) {
1498                 case IO_NORMAL:
1499                         /* Direct IO reads must also take range lock,
1500                          * or multiple reads will try to work on the same pages
1501                          * See LU-6227 for details. */
1502                         if (((iot == CIT_WRITE) ||
1503                             (iot == CIT_READ && (file->f_flags & O_DIRECT))) &&
1504                             !(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1505                                 CDEBUG(D_VFSTRACE, "Range lock "RL_FMT"\n",
1506                                        RL_PARA(&range));
1507                                 rc = range_lock(&lli->lli_write_tree, &range);
1508                                 if (rc < 0)
1509                                         GOTO(out, rc);
1510
1511                                 range_locked = true;
1512                         }
1513                         break;
1514                 case IO_SPLICE:
1515                         vio->u.splice.vui_pipe = args->u.splice.via_pipe;
1516                         vio->u.splice.vui_flags = args->u.splice.via_flags;
1517                         break;
1518                 default:
1519                         CERROR("unknown IO subtype %u\n", vio->vui_io_subtype);
1520                         LBUG();
1521                 }
1522
1523                 ll_cl_add(file, env, io, LCC_RW);
1524                 if (io->ci_pio && iot == CIT_WRITE && !IS_NOSEC(inode) &&
1525                     !lli->lli_inode_locked) {
1526                         inode_lock(inode);
1527                         lli->lli_inode_locked = 1;
1528                 }
1529                 rc = cl_io_loop(env, io);
1530                 if (lli->lli_inode_locked) {
1531                         lli->lli_inode_locked = 0;
1532                         inode_unlock(inode);
1533                 }
1534                 ll_cl_remove(file, env);
1535
1536                 if (range_locked) {
1537                         CDEBUG(D_VFSTRACE, "Range unlock "RL_FMT"\n",
1538                                RL_PARA(&range));
1539                         range_unlock(&lli->lli_write_tree, &range);
1540                 }
1541         } else {
1542                 /* cl_io_rw_init() handled IO */
1543                 rc = io->ci_result;
1544         }
1545
1546         if (io->ci_nob > 0) {
1547                 result += io->ci_nob;
1548                 count  -= io->ci_nob;
1549
1550                 if (args->via_io_subtype == IO_NORMAL) {
1551                         iov_iter_advance(args->u.normal.via_iter, io->ci_nob);
1552
1553                         /* CLIO is too complicated. See LU-11069. */
1554                         if (cl_io_is_append(io))
1555                                 pos = io->u.ci_rw.rw_iocb.ki_pos;
1556                         else
1557                                 pos += io->ci_nob;
1558
1559                         args->u.normal.via_iocb->ki_pos = pos;
1560 #ifdef HAVE_KIOCB_KI_LEFT
1561                         args->u.normal.via_iocb->ki_left = count;
1562 #elif defined(HAVE_KI_NBYTES)
1563                         args->u.normal.via_iocb->ki_nbytes = count;
1564 #endif
1565                 } else {
1566                         /* for splice */
1567                         pos = io->u.ci_rw.rw_range.cir_pos;
1568                 }
1569         }
1570 out:
1571         cl_io_fini(env, io);
1572
1573         CDEBUG(D_VFSTRACE,
1574                "%s: %d io complete with rc: %d, result: %zd, restart: %d\n",
1575                file->f_path.dentry->d_name.name,
1576                iot, rc, result, io->ci_need_restart);
1577
1578         if ((rc == 0 || rc == -ENODATA) && count > 0 && io->ci_need_restart) {
1579                 CDEBUG(D_VFSTRACE,
1580                         "%s: restart %s range: [%llu, %llu) ret: %zd, rc: %d\n",
1581                         file_dentry(file)->d_name.name,
1582                         iot == CIT_READ ? "read" : "write",
1583                         pos, pos + count, result, rc);
1584                 /* preserve the tried count for FLR */
1585                 retried = io->ci_ndelay_tried;
1586                 restarted = true;
1587                 goto restart;
1588         }
1589
1590         if (iot == CIT_READ) {
1591                 if (result > 0)
1592                         ll_stats_ops_tally(ll_i2sbi(inode),
1593                                            LPROC_LL_READ_BYTES, result);
1594         } else if (iot == CIT_WRITE) {
1595                 if (result > 0) {
1596                         ll_stats_ops_tally(ll_i2sbi(inode),
1597                                            LPROC_LL_WRITE_BYTES, result);
1598                         fd->fd_write_failed = false;
1599                 } else if (result == 0 && rc == 0) {
1600                         rc = io->ci_result;
1601                         if (rc < 0)
1602                                 fd->fd_write_failed = true;
1603                         else
1604                                 fd->fd_write_failed = false;
1605                 } else if (rc != -ERESTARTSYS) {
1606                         fd->fd_write_failed = true;
1607                 }
1608         }
1609
1610         CDEBUG(D_VFSTRACE, "%s: %s *ppos: %llu, pos: %llu, ret: %zd, rc: %d\n",
1611                 file_dentry(file)->d_name.name,
1612                 iot == CIT_READ ? "read" : "write", *ppos, pos, result, rc);
1613
1614         *ppos = pos;
1615
1616         RETURN(result > 0 ? result : rc);
1617 }
1618
1619 /**
1620  * The purpose of fast read is to overcome per I/O overhead and improve IOPS
1621  * especially for small I/O.
1622  *
1623  * To serve a read request, CLIO has to create and initialize a cl_io and
1624  * then request DLM lock. This has turned out to have siginificant overhead
1625  * and affects the performance of small I/O dramatically.
1626  *
1627  * It's not necessary to create a cl_io for each I/O. Under the help of read
1628  * ahead, most of the pages being read are already in memory cache and we can
1629  * read those pages directly because if the pages exist, the corresponding DLM
1630  * lock must exist so that page content must be valid.
1631  *
1632  * In fast read implementation, the llite speculatively finds and reads pages
1633  * in memory cache. There are three scenarios for fast read:
1634  *   - If the page exists and is uptodate, kernel VM will provide the data and
1635  *     CLIO won't be intervened;
1636  *   - If the page was brought into memory by read ahead, it will be exported
1637  *     and read ahead parameters will be updated;
1638  *   - Otherwise the page is not in memory, we can't do fast read. Therefore,
1639  *     it will go back and invoke normal read, i.e., a cl_io will be created
1640  *     and DLM lock will be requested.
1641  *
1642  * POSIX compliance: posix standard states that read is intended to be atomic.
1643  * Lustre read implementation is in line with Linux kernel read implementation
1644  * and neither of them complies with POSIX standard in this matter. Fast read
1645  * doesn't make the situation worse on single node but it may interleave write
1646  * results from multiple nodes due to short read handling in ll_file_aio_read().
1647  *
1648  * \param env - lu_env
1649  * \param iocb - kiocb from kernel
1650  * \param iter - user space buffers where the data will be copied
1651  *
1652  * \retval - number of bytes have been read, or error code if error occurred.
1653  */
1654 static ssize_t
1655 ll_do_fast_read(struct kiocb *iocb, struct iov_iter *iter)
1656 {
1657         ssize_t result;
1658
1659         if (!ll_sbi_has_fast_read(ll_i2sbi(file_inode(iocb->ki_filp))))
1660                 return 0;
1661
1662         /* NB: we can't do direct IO for fast read because it will need a lock
1663          * to make IO engine happy. */
1664         if (iocb->ki_filp->f_flags & O_DIRECT)
1665                 return 0;
1666
1667         result = generic_file_read_iter(iocb, iter);
1668
1669         /* If the first page is not in cache, generic_file_aio_read() will be
1670          * returned with -ENODATA.
1671          * See corresponding code in ll_readpage(). */
1672         if (result == -ENODATA)
1673                 result = 0;
1674
1675         if (result > 0)
1676                 ll_stats_ops_tally(ll_i2sbi(file_inode(iocb->ki_filp)),
1677                                 LPROC_LL_READ_BYTES, result);
1678
1679         return result;
1680 }
1681
1682 /*
1683  * Read from a file (through the page cache).
1684  */
1685 static ssize_t ll_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1686 {
1687         struct lu_env *env;
1688         struct vvp_io_args *args;
1689         ssize_t result;
1690         ssize_t rc2;
1691         __u16 refcheck;
1692
1693         result = ll_do_fast_read(iocb, to);
1694         if (result < 0 || iov_iter_count(to) == 0)
1695                 GOTO(out, result);
1696
1697         env = cl_env_get(&refcheck);
1698         if (IS_ERR(env))
1699                 return PTR_ERR(env);
1700
1701         args = ll_env_args(env, IO_NORMAL);
1702         args->u.normal.via_iter = to;
1703         args->u.normal.via_iocb = iocb;
1704
1705         rc2 = ll_file_io_generic(env, args, iocb->ki_filp, CIT_READ,
1706                                  &iocb->ki_pos, iov_iter_count(to));
1707         if (rc2 > 0)
1708                 result += rc2;
1709         else if (result == 0)
1710                 result = rc2;
1711
1712         cl_env_put(env, &refcheck);
1713 out:
1714         return result;
1715 }
1716
1717 /**
1718  * Similar trick to ll_do_fast_read, this improves write speed for tiny writes.
1719  * If a page is already in the page cache and dirty (and some other things -
1720  * See ll_tiny_write_begin for the instantiation of these rules), then we can
1721  * write to it without doing a full I/O, because Lustre already knows about it
1722  * and will write it out.  This saves a lot of processing time.
1723  *
1724  * All writes here are within one page, so exclusion is handled by the page
1725  * lock on the vm page.  We do not do tiny writes for writes which touch
1726  * multiple pages because it's very unlikely multiple sequential pages are
1727  * are already dirty.
1728  *
1729  * We limit these to < PAGE_SIZE because PAGE_SIZE writes are relatively common
1730  * and are unlikely to be to already dirty pages.
1731  *
1732  * Attribute updates are important here, we do them in ll_tiny_write_end.
1733  */
1734 static ssize_t ll_do_tiny_write(struct kiocb *iocb, struct iov_iter *iter)
1735 {
1736         ssize_t count = iov_iter_count(iter);
1737         struct file *file = iocb->ki_filp;
1738         struct inode *inode = file_inode(file);
1739         ssize_t result = 0;
1740
1741         ENTRY;
1742
1743         /* Restrict writes to single page and < PAGE_SIZE.  See comment at top
1744          * of function for why.
1745          */
1746         if (count >= PAGE_SIZE ||
1747             (iocb->ki_pos & (PAGE_SIZE-1)) + count > PAGE_SIZE)
1748                 RETURN(0);
1749
1750         result = __generic_file_write_iter(iocb, iter);
1751
1752         /* If the page is not already dirty, ll_tiny_write_begin returns
1753          * -ENODATA.  We continue on to normal write.
1754          */
1755         if (result == -ENODATA)
1756                 result = 0;
1757
1758         if (result > 0) {
1759                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_WRITE_BYTES,
1760                                    result);
1761                 ll_file_set_flag(ll_i2info(inode), LLIF_DATA_MODIFIED);
1762         }
1763
1764         CDEBUG(D_VFSTRACE, "result: %zu, original count %zu\n", result, count);
1765
1766         RETURN(result);
1767 }
1768
1769 /*
1770  * Write to a file (through the page cache).
1771  */
1772 static ssize_t ll_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1773 {
1774         struct vvp_io_args *args;
1775         struct lu_env *env;
1776         ssize_t rc_tiny = 0, rc_normal;
1777         __u16 refcheck;
1778
1779         ENTRY;
1780
1781         /* NB: we can't do direct IO for tiny writes because they use the page
1782          * cache, we can't do sync writes because tiny writes can't flush
1783          * pages, and we can't do append writes because we can't guarantee the
1784          * required DLM locks are held to protect file size.
1785          */
1786         if (ll_sbi_has_tiny_write(ll_i2sbi(file_inode(iocb->ki_filp))) &&
1787             !(iocb->ki_filp->f_flags & (O_DIRECT | O_SYNC | O_APPEND)))
1788                 rc_tiny = ll_do_tiny_write(iocb, from);
1789
1790         /* In case of error, go on and try normal write - Only stop if tiny
1791          * write completed I/O.
1792          */
1793         if (iov_iter_count(from) == 0)
1794                 GOTO(out, rc_normal = rc_tiny);
1795
1796         env = cl_env_get(&refcheck);
1797         if (IS_ERR(env))
1798                 return PTR_ERR(env);
1799
1800         args = ll_env_args(env, IO_NORMAL);
1801         args->u.normal.via_iter = from;
1802         args->u.normal.via_iocb = iocb;
1803
1804         rc_normal = ll_file_io_generic(env, args, iocb->ki_filp, CIT_WRITE,
1805                                     &iocb->ki_pos, iov_iter_count(from));
1806
1807         /* On success, combine bytes written. */
1808         if (rc_tiny >= 0 && rc_normal > 0)
1809                 rc_normal += rc_tiny;
1810         /* On error, only return error from normal write if tiny write did not
1811          * write any bytes.  Otherwise return bytes written by tiny write.
1812          */
1813         else if (rc_tiny > 0)
1814                 rc_normal = rc_tiny;
1815
1816         cl_env_put(env, &refcheck);
1817 out:
1818         RETURN(rc_normal);
1819 }
1820
1821 #ifndef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
1822 /*
1823  * XXX: exact copy from kernel code (__generic_file_aio_write_nolock)
1824  */
1825 static int ll_file_get_iov_count(const struct iovec *iov,
1826                                  unsigned long *nr_segs, size_t *count)
1827 {
1828         size_t cnt = 0;
1829         unsigned long seg;
1830
1831         for (seg = 0; seg < *nr_segs; seg++) {
1832                 const struct iovec *iv = &iov[seg];
1833
1834                 /*
1835                  * If any segment has a negative length, or the cumulative
1836                  * length ever wraps negative then return -EINVAL.
1837                  */
1838                 cnt += iv->iov_len;
1839                 if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
1840                         return -EINVAL;
1841                 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
1842                         continue;
1843                 if (seg == 0)
1844                         return -EFAULT;
1845                 *nr_segs = seg;
1846                 cnt -= iv->iov_len;     /* This segment is no good */
1847                 break;
1848         }
1849         *count = cnt;
1850         return 0;
1851 }
1852
1853 static ssize_t ll_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1854                                 unsigned long nr_segs, loff_t pos)
1855 {
1856         struct iov_iter to;
1857         size_t iov_count;
1858         ssize_t result;
1859         ENTRY;
1860
1861         result = ll_file_get_iov_count(iov, &nr_segs, &iov_count);
1862         if (result)
1863                 RETURN(result);
1864
1865 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
1866         iov_iter_init(&to, READ, iov, nr_segs, iov_count);
1867 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
1868         iov_iter_init(&to, iov, nr_segs, iov_count, 0);
1869 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
1870
1871         result = ll_file_read_iter(iocb, &to);
1872
1873         RETURN(result);
1874 }
1875
1876 static ssize_t ll_file_read(struct file *file, char __user *buf, size_t count,
1877                             loff_t *ppos)
1878 {
1879         struct iovec   iov = { .iov_base = buf, .iov_len = count };
1880         struct kiocb   kiocb;
1881         ssize_t        result;
1882         ENTRY;
1883
1884         init_sync_kiocb(&kiocb, file);
1885         kiocb.ki_pos = *ppos;
1886 #ifdef HAVE_KIOCB_KI_LEFT
1887         kiocb.ki_left = count;
1888 #elif defined(HAVE_KI_NBYTES)
1889         kiocb.i_nbytes = count;
1890 #endif
1891
1892         result = ll_file_aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
1893         *ppos = kiocb.ki_pos;
1894
1895         RETURN(result);
1896 }
1897
1898 /*
1899  * Write to a file (through the page cache).
1900  * AIO stuff
1901  */
1902 static ssize_t ll_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
1903                                  unsigned long nr_segs, loff_t pos)
1904 {
1905         struct iov_iter from;
1906         size_t iov_count;
1907         ssize_t result;
1908         ENTRY;
1909
1910         result = ll_file_get_iov_count(iov, &nr_segs, &iov_count);
1911         if (result)
1912                 RETURN(result);
1913
1914 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
1915         iov_iter_init(&from, WRITE, iov, nr_segs, iov_count);
1916 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
1917         iov_iter_init(&from, iov, nr_segs, iov_count, 0);
1918 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
1919
1920         result = ll_file_write_iter(iocb, &from);
1921
1922         RETURN(result);
1923 }
1924
1925 static ssize_t ll_file_write(struct file *file, const char __user *buf,
1926                              size_t count, loff_t *ppos)
1927 {
1928         struct iovec   iov = { .iov_base = (void __user *)buf,
1929                                .iov_len = count };
1930         struct kiocb   kiocb;
1931         ssize_t        result;
1932
1933         ENTRY;
1934
1935         init_sync_kiocb(&kiocb, file);
1936         kiocb.ki_pos = *ppos;
1937 #ifdef HAVE_KIOCB_KI_LEFT
1938         kiocb.ki_left = count;
1939 #elif defined(HAVE_KI_NBYTES)
1940         kiocb.ki_nbytes = count;
1941 #endif
1942
1943         result = ll_file_aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
1944         *ppos = kiocb.ki_pos;
1945
1946         RETURN(result);
1947 }
1948 #endif /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
1949
1950 /*
1951  * Send file content (through pagecache) somewhere with helper
1952  */
1953 static ssize_t ll_file_splice_read(struct file *in_file, loff_t *ppos,
1954                                    struct pipe_inode_info *pipe, size_t count,
1955                                    unsigned int flags)
1956 {
1957         struct lu_env      *env;
1958         struct vvp_io_args *args;
1959         ssize_t             result;
1960         __u16               refcheck;
1961         ENTRY;
1962
1963         env = cl_env_get(&refcheck);
1964         if (IS_ERR(env))
1965                 RETURN(PTR_ERR(env));
1966
1967         args = ll_env_args(env, IO_SPLICE);
1968         args->u.splice.via_pipe = pipe;
1969         args->u.splice.via_flags = flags;
1970
1971         result = ll_file_io_generic(env, args, in_file, CIT_READ, ppos, count);
1972         cl_env_put(env, &refcheck);
1973         RETURN(result);
1974 }
1975
1976 int ll_lov_setstripe_ea_info(struct inode *inode, struct dentry *dentry,
1977                              __u64 flags, struct lov_user_md *lum, int lum_size)
1978 {
1979         struct lookup_intent oit = {
1980                 .it_op = IT_OPEN,
1981                 .it_flags = flags | MDS_OPEN_BY_FID,
1982         };
1983         int rc;
1984         ENTRY;
1985
1986         ll_inode_size_lock(inode);
1987         rc = ll_intent_file_open(dentry, lum, lum_size, &oit);
1988         if (rc < 0)
1989                 GOTO(out_unlock, rc);
1990
1991         ll_release_openhandle(dentry, &oit);
1992
1993 out_unlock:
1994         ll_inode_size_unlock(inode);
1995         ll_intent_release(&oit);
1996
1997         RETURN(rc);
1998 }
1999
2000 int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
2001                              struct lov_mds_md **lmmp, int *lmm_size,
2002                              struct ptlrpc_request **request)
2003 {
2004         struct ll_sb_info *sbi = ll_i2sbi(inode);
2005         struct mdt_body  *body;
2006         struct lov_mds_md *lmm = NULL;
2007         struct ptlrpc_request *req = NULL;
2008         struct md_op_data *op_data;
2009         int rc, lmmsize;
2010
2011         rc = ll_get_default_mdsize(sbi, &lmmsize);
2012         if (rc)
2013                 RETURN(rc);
2014
2015         op_data = ll_prep_md_op_data(NULL, inode, NULL, filename,
2016                                      strlen(filename), lmmsize,
2017                                      LUSTRE_OPC_ANY, NULL);
2018         if (IS_ERR(op_data))
2019                 RETURN(PTR_ERR(op_data));
2020
2021         op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
2022         rc = md_getattr_name(sbi->ll_md_exp, op_data, &req);
2023         ll_finish_md_op_data(op_data);
2024         if (rc < 0) {
2025                 CDEBUG(D_INFO, "md_getattr_name failed "
2026                        "on %s: rc %d\n", filename, rc);
2027                 GOTO(out, rc);
2028         }
2029
2030         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2031         LASSERT(body != NULL); /* checked by mdc_getattr_name */
2032
2033         lmmsize = body->mbo_eadatasize;
2034
2035         if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
2036                         lmmsize == 0) {
2037                 GOTO(out, rc = -ENODATA);
2038         }
2039
2040         lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_MDT_MD, lmmsize);
2041         LASSERT(lmm != NULL);
2042
2043         if (lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V1) &&
2044             lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V3) &&
2045             lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_COMP_V1))
2046                 GOTO(out, rc = -EPROTO);
2047
2048         /*
2049          * This is coming from the MDS, so is probably in
2050          * little endian.  We convert it to host endian before
2051          * passing it to userspace.
2052          */
2053         if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC)) {
2054                 int stripe_count;
2055
2056                 if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1) ||
2057                     lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3)) {
2058                         stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
2059                         if (le32_to_cpu(lmm->lmm_pattern) &
2060                             LOV_PATTERN_F_RELEASED)
2061                                 stripe_count = 0;
2062                 }
2063
2064                 /* if function called for directory - we should
2065                  * avoid swab not existent lsm objects */
2066                 if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1)) {
2067                         lustre_swab_lov_user_md_v1(
2068                                         (struct lov_user_md_v1 *)lmm);
2069                         if (S_ISREG(body->mbo_mode))
2070                                 lustre_swab_lov_user_md_objects(
2071                                     ((struct lov_user_md_v1 *)lmm)->lmm_objects,
2072                                     stripe_count);
2073                 } else if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3)) {
2074                         lustre_swab_lov_user_md_v3(
2075                                         (struct lov_user_md_v3 *)lmm);
2076                         if (S_ISREG(body->mbo_mode))
2077                                 lustre_swab_lov_user_md_objects(
2078                                     ((struct lov_user_md_v3 *)lmm)->lmm_objects,
2079                                     stripe_count);
2080                 } else if (lmm->lmm_magic ==
2081                            cpu_to_le32(LOV_MAGIC_COMP_V1)) {
2082                         lustre_swab_lov_comp_md_v1(
2083                                         (struct lov_comp_md_v1 *)lmm);
2084                 }
2085         }
2086
2087 out:
2088         *lmmp = lmm;
2089         *lmm_size = lmmsize;
2090         *request = req;
2091         return rc;
2092 }
2093
2094 static int ll_lov_setea(struct inode *inode, struct file *file,
2095                         void __user *arg)
2096 {
2097         __u64                    flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
2098         struct lov_user_md      *lump;
2099         int                      lum_size = sizeof(struct lov_user_md) +
2100                                             sizeof(struct lov_user_ost_data);
2101         int                      rc;
2102         ENTRY;
2103
2104         if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2105                 RETURN(-EPERM);
2106
2107         OBD_ALLOC_LARGE(lump, lum_size);
2108         if (lump == NULL)
2109                 RETURN(-ENOMEM);
2110
2111         if (copy_from_user(lump, arg, lum_size))
2112                 GOTO(out_lump, rc = -EFAULT);
2113
2114         rc = ll_lov_setstripe_ea_info(inode, file_dentry(file), flags, lump,
2115                                       lum_size);
2116         cl_lov_delay_create_clear(&file->f_flags);
2117
2118 out_lump:
2119         OBD_FREE_LARGE(lump, lum_size);
2120         RETURN(rc);
2121 }
2122
2123 static int ll_file_getstripe(struct inode *inode, void __user *lum, size_t size)
2124 {
2125         struct lu_env   *env;
2126         __u16           refcheck;
2127         int             rc;
2128         ENTRY;
2129
2130         env = cl_env_get(&refcheck);
2131         if (IS_ERR(env))
2132                 RETURN(PTR_ERR(env));
2133
2134         rc = cl_object_getstripe(env, ll_i2info(inode)->lli_clob, lum, size);
2135         cl_env_put(env, &refcheck);
2136         RETURN(rc);
2137 }
2138
2139 static int ll_lov_setstripe(struct inode *inode, struct file *file,
2140                             void __user *arg)
2141 {
2142         struct lov_user_md __user *lum = (struct lov_user_md __user *)arg;
2143         struct lov_user_md        *klum;
2144         int                        lum_size, rc;
2145         __u64                      flags = FMODE_WRITE;
2146         ENTRY;
2147
2148         rc = ll_copy_user_md(lum, &klum);
2149         if (rc < 0)
2150                 RETURN(rc);
2151
2152         lum_size = rc;
2153         rc = ll_lov_setstripe_ea_info(inode, file_dentry(file), flags, klum,
2154                                       lum_size);
2155         if (!rc) {
2156                 __u32 gen;
2157
2158                 rc = put_user(0, &lum->lmm_stripe_count);
2159                 if (rc)
2160                         GOTO(out, rc);
2161
2162                 rc = ll_layout_refresh(inode, &gen);
2163                 if (rc)
2164                         GOTO(out, rc);
2165
2166                 rc = ll_file_getstripe(inode, arg, lum_size);
2167         }
2168         cl_lov_delay_create_clear(&file->f_flags);
2169
2170 out:
2171         OBD_FREE(klum, lum_size);
2172         RETURN(rc);
2173 }
2174
2175 static int
2176 ll_get_grouplock(struct inode *inode, struct file *file, unsigned long arg)
2177 {
2178         struct ll_inode_info *lli = ll_i2info(inode);
2179         struct cl_object *obj = lli->lli_clob;
2180         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
2181         struct ll_grouplock grouplock;
2182         int rc;
2183         ENTRY;
2184
2185         if (arg == 0) {
2186                 CWARN("group id for group lock must not be 0\n");
2187                 RETURN(-EINVAL);
2188         }
2189
2190         if (ll_file_nolock(file))
2191                 RETURN(-EOPNOTSUPP);
2192
2193         spin_lock(&lli->lli_lock);
2194         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
2195                 CWARN("group lock already existed with gid %lu\n",
2196                       fd->fd_grouplock.lg_gid);
2197                 spin_unlock(&lli->lli_lock);
2198                 RETURN(-EINVAL);
2199         }
2200         LASSERT(fd->fd_grouplock.lg_lock == NULL);
2201         spin_unlock(&lli->lli_lock);
2202
2203         /**
2204          * XXX: group lock needs to protect all OST objects while PFL
2205          * can add new OST objects during the IO, so we'd instantiate
2206          * all OST objects before getting its group lock.
2207          */
2208         if (obj) {
2209                 struct lu_env *env;
2210                 __u16 refcheck;
2211                 struct cl_layout cl = {
2212                         .cl_is_composite = false,
2213                 };
2214                 struct lu_extent ext = {
2215                         .e_start = 0,
2216                         .e_end = OBD_OBJECT_EOF,
2217                 };
2218
2219                 env = cl_env_get(&refcheck);
2220                 if (IS_ERR(env))
2221                         RETURN(PTR_ERR(env));
2222
2223                 rc = cl_object_layout_get(env, obj, &cl);
2224                 if (!rc && cl.cl_is_composite)
2225                         rc = ll_layout_write_intent(inode, LAYOUT_INTENT_WRITE,
2226                                                     &ext);
2227
2228                 cl_env_put(env, &refcheck);
2229                 if (rc)
2230                         RETURN(rc);
2231         }
2232
2233         rc = cl_get_grouplock(ll_i2info(inode)->lli_clob,
2234                               arg, (file->f_flags & O_NONBLOCK), &grouplock);
2235         if (rc)
2236                 RETURN(rc);
2237
2238         spin_lock(&lli->lli_lock);
2239         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
2240                 spin_unlock(&lli->lli_lock);
2241                 CERROR("another thread just won the race\n");
2242                 cl_put_grouplock(&grouplock);
2243                 RETURN(-EINVAL);
2244         }
2245
2246         fd->fd_flags |= LL_FILE_GROUP_LOCKED;
2247         fd->fd_grouplock = grouplock;
2248         spin_unlock(&lli->lli_lock);
2249
2250         CDEBUG(D_INFO, "group lock %lu obtained\n", arg);
2251         RETURN(0);
2252 }
2253
2254 static int ll_put_grouplock(struct inode *inode, struct file *file,
2255                             unsigned long arg)
2256 {
2257         struct ll_inode_info   *lli = ll_i2info(inode);
2258         struct ll_file_data    *fd = LUSTRE_FPRIVATE(file);
2259         struct ll_grouplock     grouplock;
2260         ENTRY;
2261
2262         spin_lock(&lli->lli_lock);
2263         if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
2264                 spin_unlock(&lli->lli_lock);
2265                 CWARN("no group lock held\n");
2266                 RETURN(-EINVAL);
2267         }
2268
2269         LASSERT(fd->fd_grouplock.lg_lock != NULL);
2270
2271         if (fd->fd_grouplock.lg_gid != arg) {
2272                 CWARN("group lock %lu doesn't match current id %lu\n",
2273                       arg, fd->fd_grouplock.lg_gid);
2274                 spin_unlock(&lli->lli_lock);
2275                 RETURN(-EINVAL);
2276         }
2277
2278         grouplock = fd->fd_grouplock;
2279         memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
2280         fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
2281         spin_unlock(&lli->lli_lock);
2282
2283         cl_put_grouplock(&grouplock);
2284         CDEBUG(D_INFO, "group lock %lu released\n", arg);
2285         RETURN(0);
2286 }
2287
2288 /**
2289  * Close inode open handle
2290  *
2291  * \param dentry [in]     dentry which contains the inode
2292  * \param it     [in,out] intent which contains open info and result
2293  *
2294  * \retval 0     success
2295  * \retval <0    failure
2296  */
2297 int ll_release_openhandle(struct dentry *dentry, struct lookup_intent *it)
2298 {
2299         struct inode *inode = dentry->d_inode;
2300         struct obd_client_handle *och;
2301         int rc;
2302         ENTRY;
2303
2304         LASSERT(inode);
2305
2306         /* Root ? Do nothing. */
2307         if (dentry->d_inode->i_sb->s_root == dentry)
2308                 RETURN(0);
2309
2310         /* No open handle to close? Move away */
2311         if (!it_disposition(it, DISP_OPEN_OPEN))
2312                 RETURN(0);
2313
2314         LASSERT(it_open_error(DISP_OPEN_OPEN, it) == 0);
2315
2316         OBD_ALLOC(och, sizeof(*och));
2317         if (!och)
2318                 GOTO(out, rc = -ENOMEM);
2319
2320         ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
2321
2322         rc = ll_close_inode_openhandle(inode, och, 0, NULL);
2323 out:
2324         /* this one is in place of ll_file_open */
2325         if (it_disposition(it, DISP_ENQ_OPEN_REF)) {
2326                 ptlrpc_req_finished(it->it_request);
2327                 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
2328         }
2329         RETURN(rc);
2330 }
2331
2332 /**
2333  * Get size for inode for which FIEMAP mapping is requested.
2334  * Make the FIEMAP get_info call and returns the result.
2335  * \param fiemap        kernel buffer to hold extens
2336  * \param num_bytes     kernel buffer size
2337  */
2338 static int ll_do_fiemap(struct inode *inode, struct fiemap *fiemap,
2339                         size_t num_bytes)
2340 {
2341         struct lu_env                   *env;
2342         __u16                           refcheck;
2343         int                             rc = 0;
2344         struct ll_fiemap_info_key       fmkey = { .lfik_name = KEY_FIEMAP, };
2345         ENTRY;
2346
2347         /* Checks for fiemap flags */
2348         if (fiemap->fm_flags & ~LUSTRE_FIEMAP_FLAGS_COMPAT) {
2349                 fiemap->fm_flags &= ~LUSTRE_FIEMAP_FLAGS_COMPAT;
2350                 return -EBADR;
2351         }
2352
2353         /* Check for FIEMAP_FLAG_SYNC */
2354         if (fiemap->fm_flags & FIEMAP_FLAG_SYNC) {
2355                 rc = filemap_fdatawrite(inode->i_mapping);
2356                 if (rc)
2357                         return rc;
2358         }
2359
2360         env = cl_env_get(&refcheck);
2361         if (IS_ERR(env))
2362                 RETURN(PTR_ERR(env));
2363
2364         if (i_size_read(inode) == 0) {
2365                 rc = ll_glimpse_size(inode);
2366                 if (rc)
2367                         GOTO(out, rc);
2368         }
2369
2370         fmkey.lfik_oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
2371         obdo_from_inode(&fmkey.lfik_oa, inode, OBD_MD_FLSIZE);
2372         obdo_set_parent_fid(&fmkey.lfik_oa, &ll_i2info(inode)->lli_fid);
2373
2374         /* If filesize is 0, then there would be no objects for mapping */
2375         if (fmkey.lfik_oa.o_size == 0) {
2376                 fiemap->fm_mapped_extents = 0;
2377                 GOTO(out, rc = 0);
2378         }
2379
2380         fmkey.lfik_fiemap = *fiemap;
2381
2382         rc = cl_object_fiemap(env, ll_i2info(inode)->lli_clob,
2383                               &fmkey, fiemap, &num_bytes);
2384 out:
2385         cl_env_put(env, &refcheck);
2386         RETURN(rc);
2387 }
2388
2389 int ll_fid2path(struct inode *inode, void __user *arg)
2390 {
2391         struct obd_export       *exp = ll_i2mdexp(inode);
2392         const struct getinfo_fid2path __user *gfin = arg;
2393         __u32                    pathlen;
2394         struct getinfo_fid2path *gfout;
2395         size_t                   outsize;
2396         int                      rc;
2397
2398         ENTRY;
2399
2400         if (!cfs_capable(CFS_CAP_DAC_READ_SEARCH) &&
2401             !(ll_i2sbi(inode)->ll_flags & LL_SBI_USER_FID2PATH))
2402                 RETURN(-EPERM);
2403
2404         /* Only need to get the buflen */
2405         if (get_user(pathlen, &gfin->gf_pathlen))
2406                 RETURN(-EFAULT);
2407
2408         if (pathlen > PATH_MAX)
2409                 RETURN(-EINVAL);
2410
2411         outsize = sizeof(*gfout) + pathlen;
2412         OBD_ALLOC(gfout, outsize);
2413         if (gfout == NULL)
2414                 RETURN(-ENOMEM);
2415
2416         if (copy_from_user(gfout, arg, sizeof(*gfout)))
2417                 GOTO(gf_free, rc = -EFAULT);
2418         /* append root FID after gfout to let MDT know the root FID so that it
2419          * can lookup the correct path, this is mainly for fileset.
2420          * old server without fileset mount support will ignore this. */
2421         *gfout->gf_u.gf_root_fid = *ll_inode2fid(inode);
2422
2423         /* Call mdc_iocontrol */
2424         rc = obd_iocontrol(OBD_IOC_FID2PATH, exp, outsize, gfout, NULL);
2425         if (rc != 0)
2426                 GOTO(gf_free, rc);
2427
2428         if (copy_to_user(arg, gfout, outsize))
2429                 rc = -EFAULT;
2430
2431 gf_free:
2432         OBD_FREE(gfout, outsize);
2433         RETURN(rc);
2434 }
2435
2436 static int
2437 ll_ioc_data_version(struct inode *inode, struct ioc_data_version *ioc)
2438 {
2439         struct cl_object *obj = ll_i2info(inode)->lli_clob;
2440         struct lu_env *env;
2441         struct cl_io *io;
2442         __u16  refcheck;
2443         int result;
2444
2445         ENTRY;
2446
2447         ioc->idv_version = 0;
2448         ioc->idv_layout_version = UINT_MAX;
2449
2450         /* If no file object initialized, we consider its version is 0. */
2451         if (obj == NULL)
2452                 RETURN(0);
2453
2454         env = cl_env_get(&refcheck);
2455         if (IS_ERR(env))
2456                 RETURN(PTR_ERR(env));
2457
2458         io = vvp_env_thread_io(env);
2459         io->ci_obj = obj;
2460         io->u.ci_data_version.dv_data_version = 0;
2461         io->u.ci_data_version.dv_layout_version = UINT_MAX;
2462         io->u.ci_data_version.dv_flags = ioc->idv_flags;
2463
2464 restart:
2465         if (cl_io_init(env, io, CIT_DATA_VERSION, io->ci_obj) == 0)
2466                 result = cl_io_loop(env, io);
2467         else
2468                 result = io->ci_result;
2469
2470         ioc->idv_version = io->u.ci_data_version.dv_data_version;
2471         ioc->idv_layout_version = io->u.ci_data_version.dv_layout_version;
2472
2473         cl_io_fini(env, io);
2474
2475         if (unlikely(io->ci_need_restart))
2476                 goto restart;
2477
2478         cl_env_put(env, &refcheck);
2479
2480         RETURN(result);
2481 }
2482
2483 /*
2484  * Read the data_version for inode.
2485  *
2486  * This value is computed using stripe object version on OST.
2487  * Version is computed using server side locking.
2488  *
2489  * @param flags if do sync on the OST side;
2490  *              0: no sync
2491  *              LL_DV_RD_FLUSH: flush dirty pages, LCK_PR on OSTs
2492  *              LL_DV_WR_FLUSH: drop all caching pages, LCK_PW on OSTs
2493  */
2494 int ll_data_version(struct inode *inode, __u64 *data_version, int flags)
2495 {
2496         struct ioc_data_version ioc = { .idv_flags = flags };
2497         int rc;
2498
2499         rc = ll_ioc_data_version(inode, &ioc);
2500         if (!rc)
2501                 *data_version = ioc.idv_version;
2502
2503         return rc;
2504 }
2505
2506 /*
2507  * Trigger a HSM release request for the provided inode.
2508  */
2509 int ll_hsm_release(struct inode *inode)
2510 {
2511         struct lu_env *env;
2512         struct obd_client_handle *och = NULL;
2513         __u64 data_version = 0;
2514         int rc;
2515         __u16 refcheck;
2516         ENTRY;
2517
2518         CDEBUG(D_INODE, "%s: Releasing file "DFID".\n",
2519                ll_get_fsname(inode->i_sb, NULL, 0),
2520                PFID(&ll_i2info(inode)->lli_fid));
2521
2522         och = ll_lease_open(inode, NULL, FMODE_WRITE, MDS_OPEN_RELEASE);
2523         if (IS_ERR(och))
2524                 GOTO(out, rc = PTR_ERR(och));
2525
2526         /* Grab latest data_version and [am]time values */
2527         rc = ll_data_version(inode, &data_version, LL_DV_WR_FLUSH);
2528         if (rc != 0)
2529                 GOTO(out, rc);
2530
2531         env = cl_env_get(&refcheck);
2532         if (IS_ERR(env))
2533                 GOTO(out, rc = PTR_ERR(env));
2534
2535         rc = ll_merge_attr(env, inode);
2536         cl_env_put(env, &refcheck);
2537
2538         /* If error happen, we have the wrong size for a file.
2539          * Don't release it.
2540          */
2541         if (rc != 0)
2542                 GOTO(out, rc);
2543
2544         /* Release the file.
2545          * NB: lease lock handle is released in mdc_hsm_release_pack() because
2546          * we still need it to pack l_remote_handle to MDT. */
2547         rc = ll_close_inode_openhandle(inode, och, MDS_HSM_RELEASE,
2548                                        &data_version);
2549         och = NULL;
2550
2551         EXIT;
2552 out:
2553         if (och != NULL && !IS_ERR(och)) /* close the file */
2554                 ll_lease_close(och, inode, NULL);
2555
2556         return rc;
2557 }
2558
2559 struct ll_swap_stack {
2560         __u64                    dv1;
2561         __u64                    dv2;
2562         struct inode            *inode1;
2563         struct inode            *inode2;
2564         bool                     check_dv1;
2565         bool                     check_dv2;
2566 };
2567
2568 static int ll_swap_layouts(struct file *file1, struct file *file2,
2569                            struct lustre_swap_layouts *lsl)
2570 {
2571         struct mdc_swap_layouts  msl;
2572         struct md_op_data       *op_data;
2573         __u32                    gid;
2574         __u64                    dv;
2575         struct ll_swap_stack    *llss = NULL;
2576         int                      rc;
2577
2578         OBD_ALLOC_PTR(llss);
2579         if (llss == NULL)
2580                 RETURN(-ENOMEM);
2581
2582         llss->inode1 = file_inode(file1);
2583         llss->inode2 = file_inode(file2);
2584
2585         rc = ll_check_swap_layouts_validity(llss->inode1, llss->inode2);
2586         if (rc < 0)
2587                 GOTO(free, rc);
2588
2589         /* we use 2 bool because it is easier to swap than 2 bits */
2590         if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV1)
2591                 llss->check_dv1 = true;
2592
2593         if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV2)
2594                 llss->check_dv2 = true;
2595
2596         /* we cannot use lsl->sl_dvX directly because we may swap them */
2597         llss->dv1 = lsl->sl_dv1;
2598         llss->dv2 = lsl->sl_dv2;
2599
2600         rc = lu_fid_cmp(ll_inode2fid(llss->inode1), ll_inode2fid(llss->inode2));
2601         if (rc == 0) /* same file, done! */
2602                 GOTO(free, rc);
2603
2604         if (rc < 0) { /* sequentialize it */
2605                 swap(llss->inode1, llss->inode2);
2606                 swap(file1, file2);
2607                 swap(llss->dv1, llss->dv2);
2608                 swap(llss->check_dv1, llss->check_dv2);
2609         }
2610
2611         gid = lsl->sl_gid;
2612         if (gid != 0) { /* application asks to flush dirty cache */
2613                 rc = ll_get_grouplock(llss->inode1, file1, gid);
2614                 if (rc < 0)
2615                         GOTO(free, rc);
2616
2617                 rc = ll_get_grouplock(llss->inode2, file2, gid);
2618                 if (rc < 0) {
2619                         ll_put_grouplock(llss->inode1, file1, gid);
2620                         GOTO(free, rc);
2621                 }
2622         }
2623
2624         /* ultimate check, before swaping the layouts we check if
2625          * dataversion has changed (if requested) */
2626         if (llss->check_dv1) {
2627                 rc = ll_data_version(llss->inode1, &dv, 0);
2628                 if (rc)
2629                         GOTO(putgl, rc);
2630                 if (dv != llss->dv1)
2631                         GOTO(putgl, rc = -EAGAIN);
2632         }
2633
2634         if (llss->check_dv2) {
2635                 rc = ll_data_version(llss->inode2, &dv, 0);
2636                 if (rc)
2637                         GOTO(putgl, rc);
2638                 if (dv != llss->dv2)
2639                         GOTO(putgl, rc = -EAGAIN);
2640         }
2641
2642         /* struct md_op_data is used to send the swap args to the mdt
2643          * only flags is missing, so we use struct mdc_swap_layouts
2644          * through the md_op_data->op_data */
2645         /* flags from user space have to be converted before they are send to
2646          * server, no flag is sent today, they are only used on the client */
2647         msl.msl_flags = 0;
2648         rc = -ENOMEM;
2649         op_data = ll_prep_md_op_data(NULL, llss->inode1, llss->inode2, NULL, 0,
2650                                      0, LUSTRE_OPC_ANY, &msl);
2651         if (IS_ERR(op_data))
2652                 GOTO(free, rc = PTR_ERR(op_data));
2653
2654         rc = obd_iocontrol(LL_IOC_LOV_SWAP_LAYOUTS, ll_i2mdexp(llss->inode1),
2655                            sizeof(*op_data), op_data, NULL);
2656         ll_finish_md_op_data(op_data);
2657
2658         if (rc < 0)
2659                 GOTO(putgl, rc);
2660
2661 putgl:
2662         if (gid != 0) {
2663                 ll_put_grouplock(llss->inode2, file2, gid);
2664                 ll_put_grouplock(llss->inode1, file1, gid);
2665         }
2666
2667 free:
2668         if (llss != NULL)
2669                 OBD_FREE_PTR(llss);
2670
2671         RETURN(rc);
2672 }
2673
2674 int ll_hsm_state_set(struct inode *inode, struct hsm_state_set *hss)
2675 {
2676         struct md_op_data       *op_data;
2677         int                      rc;
2678         ENTRY;
2679
2680         /* Detect out-of range masks */
2681         if ((hss->hss_setmask | hss->hss_clearmask) & ~HSM_FLAGS_MASK)
2682                 RETURN(-EINVAL);
2683
2684         /* Non-root users are forbidden to set or clear flags which are
2685          * NOT defined in HSM_USER_MASK. */
2686         if (((hss->hss_setmask | hss->hss_clearmask) & ~HSM_USER_MASK) &&
2687             !cfs_capable(CFS_CAP_SYS_ADMIN))
2688                 RETURN(-EPERM);
2689
2690         /* Detect out-of range archive id */
2691         if ((hss->hss_valid & HSS_ARCHIVE_ID) &&
2692             (hss->hss_archive_id > LL_HSM_MAX_ARCHIVE))
2693                 RETURN(-EINVAL);
2694
2695         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2696                                      LUSTRE_OPC_ANY, hss);
2697         if (IS_ERR(op_data))
2698                 RETURN(PTR_ERR(op_data));
2699
2700         rc = obd_iocontrol(LL_IOC_HSM_STATE_SET, ll_i2mdexp(inode),
2701                            sizeof(*op_data), op_data, NULL);
2702
2703         ll_finish_md_op_data(op_data);
2704
2705         RETURN(rc);
2706 }
2707
2708 static int ll_hsm_import(struct inode *inode, struct file *file,
2709                          struct hsm_user_import *hui)
2710 {
2711         struct hsm_state_set    *hss = NULL;
2712         struct iattr            *attr = NULL;
2713         int                      rc;
2714         ENTRY;
2715
2716         if (!S_ISREG(inode->i_mode))
2717                 RETURN(-EINVAL);
2718
2719         /* set HSM flags */
2720         OBD_ALLOC_PTR(hss);
2721         if (hss == NULL)
2722                 GOTO(out, rc = -ENOMEM);
2723
2724         hss->hss_valid = HSS_SETMASK | HSS_ARCHIVE_ID;
2725         hss->hss_archive_id = hui->hui_archive_id;
2726         hss->hss_setmask = HS_ARCHIVED | HS_EXISTS | HS_RELEASED;
2727         rc = ll_hsm_state_set(inode, hss);
2728         if (rc != 0)
2729                 GOTO(out, rc);
2730
2731         OBD_ALLOC_PTR(attr);
2732         if (attr == NULL)
2733                 GOTO(out, rc = -ENOMEM);
2734
2735         attr->ia_mode = hui->hui_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
2736         attr->ia_mode |= S_IFREG;
2737         attr->ia_uid = make_kuid(&init_user_ns, hui->hui_uid);
2738         attr->ia_gid = make_kgid(&init_user_ns, hui->hui_gid);
2739         attr->ia_size = hui->hui_size;
2740         attr->ia_mtime.tv_sec = hui->hui_mtime;
2741         attr->ia_mtime.tv_nsec = hui->hui_mtime_ns;
2742         attr->ia_atime.tv_sec = hui->hui_atime;
2743         attr->ia_atime.tv_nsec = hui->hui_atime_ns;
2744
2745         attr->ia_valid = ATTR_SIZE | ATTR_MODE | ATTR_FORCE |
2746                          ATTR_UID | ATTR_GID |
2747                          ATTR_MTIME | ATTR_MTIME_SET |
2748                          ATTR_ATIME | ATTR_ATIME_SET;
2749
2750         inode_lock(inode);
2751
2752         rc = ll_setattr_raw(file_dentry(file), attr, 0, true);
2753         if (rc == -ENODATA)
2754                 rc = 0;
2755
2756         inode_unlock(inode);
2757
2758 out:
2759         if (hss != NULL)
2760                 OBD_FREE_PTR(hss);
2761
2762         if (attr != NULL)
2763                 OBD_FREE_PTR(attr);
2764
2765         RETURN(rc);
2766 }
2767
2768 static inline long ll_lease_type_from_fmode(fmode_t fmode)
2769 {
2770         return ((fmode & FMODE_READ) ? LL_LEASE_RDLCK : 0) |
2771                ((fmode & FMODE_WRITE) ? LL_LEASE_WRLCK : 0);
2772 }
2773
2774 static int ll_file_futimes_3(struct file *file, const struct ll_futimes_3 *lfu)
2775 {
2776         struct inode *inode = file_inode(file);
2777         struct iattr ia = {
2778                 .ia_valid = ATTR_ATIME | ATTR_ATIME_SET |
2779                             ATTR_MTIME | ATTR_MTIME_SET |
2780                             ATTR_CTIME,
2781                 .ia_atime = {
2782                         .tv_sec = lfu->lfu_atime_sec,
2783                         .tv_nsec = lfu->lfu_atime_nsec,
2784                 },
2785                 .ia_mtime = {
2786                         .tv_sec = lfu->lfu_mtime_sec,
2787                         .tv_nsec = lfu->lfu_mtime_nsec,
2788                 },
2789                 .ia_ctime = {
2790                         .tv_sec = lfu->lfu_ctime_sec,
2791                         .tv_nsec = lfu->lfu_ctime_nsec,
2792                 },
2793         };
2794         int rc;
2795         ENTRY;
2796
2797         if (!capable(CAP_SYS_ADMIN))
2798                 RETURN(-EPERM);
2799
2800         if (!S_ISREG(inode->i_mode))
2801                 RETURN(-EINVAL);
2802
2803         inode_lock(inode);
2804         rc = ll_setattr_raw(file_dentry(file), &ia, OP_XVALID_CTIME_SET,
2805                             false);
2806         inode_unlock(inode);
2807
2808         RETURN(rc);
2809 }
2810
2811 static enum cl_lock_mode cl_mode_user_to_kernel(enum lock_mode_user mode)
2812 {
2813         switch (mode) {
2814         case MODE_READ_USER:
2815                 return CLM_READ;
2816         case MODE_WRITE_USER:
2817                 return CLM_WRITE;
2818         default:
2819                 return -EINVAL;
2820         }
2821 }
2822
2823 static const char *const user_lockname[] = LOCK_MODE_NAMES;
2824
2825 /* Used to allow the upper layers of the client to request an LDLM lock
2826  * without doing an actual read or write.
2827  *
2828  * Used for ladvise lockahead to manually request specific locks.
2829  *
2830  * \param[in] file      file this ladvise lock request is on
2831  * \param[in] ladvise   ladvise struct describing this lock request
2832  *
2833  * \retval 0            success, no detailed result available (sync requests
2834  *                      and requests sent to the server [not handled locally]
2835  *                      cannot return detailed results)
2836  * \retval LLA_RESULT_{SAME,DIFFERENT} - detailed result of the lock request,
2837  *                                       see definitions for details.
2838  * \retval negative     negative errno on error
2839  */
2840 int ll_file_lock_ahead(struct file *file, struct llapi_lu_ladvise *ladvise)
2841 {
2842         struct lu_env *env = NULL;
2843         struct cl_io *io  = NULL;
2844         struct cl_lock *lock = NULL;
2845         struct cl_lock_descr *descr = NULL;
2846         struct dentry *dentry = file->f_path.dentry;
2847         struct inode *inode = dentry->d_inode;
2848         enum cl_lock_mode cl_mode;
2849         off_t start = ladvise->lla_start;
2850         off_t end = ladvise->lla_end;
2851         int result;
2852         __u16 refcheck;
2853
2854         ENTRY;
2855
2856         CDEBUG(D_VFSTRACE, "Lock request: file=%.*s, inode=%p, mode=%s "
2857                "start=%llu, end=%llu\n", dentry->d_name.len,
2858                dentry->d_name.name, dentry->d_inode,
2859                user_lockname[ladvise->lla_lockahead_mode], (__u64) start,
2860                (__u64) end);
2861
2862         cl_mode = cl_mode_user_to_kernel(ladvise->lla_lockahead_mode);
2863         if (cl_mode < 0)
2864                 GOTO(out, result = cl_mode);
2865
2866         /* Get IO environment */
2867         result = cl_io_get(inode, &env, &io, &refcheck);
2868         if (result <= 0)
2869                 GOTO(out, result);
2870
2871         result = cl_io_init(env, io, CIT_MISC, io->ci_obj);
2872         if (result > 0) {
2873                 /*
2874                  * nothing to do for this io. This currently happens when
2875                  * stripe sub-object's are not yet created.
2876                  */
2877                 result = io->ci_result;
2878         } else if (result == 0) {
2879                 lock = vvp_env_lock(env);
2880                 descr = &lock->cll_descr;
2881
2882                 descr->cld_obj   = io->ci_obj;
2883                 /* Convert byte offsets to pages */
2884                 descr->cld_start = cl_index(io->ci_obj, start);
2885                 descr->cld_end   = cl_index(io->ci_obj, end);
2886                 descr->cld_mode  = cl_mode;
2887                 /* CEF_MUST is used because we do not want to convert a
2888                  * lockahead request to a lockless lock */
2889                 descr->cld_enq_flags = CEF_MUST | CEF_LOCK_NO_EXPAND |
2890                                        CEF_NONBLOCK;
2891
2892                 if (ladvise->lla_peradvice_flags & LF_ASYNC)
2893                         descr->cld_enq_flags |= CEF_SPECULATIVE;
2894
2895                 result = cl_lock_request(env, io, lock);
2896
2897                 /* On success, we need to release the lock */
2898                 if (result >= 0)
2899                         cl_lock_release(env, lock);
2900         }
2901         cl_io_fini(env, io);
2902         cl_env_put(env, &refcheck);
2903
2904         /* -ECANCELED indicates a matching lock with a different extent
2905          * was already present, and -EEXIST indicates a matching lock
2906          * on exactly the same extent was already present.
2907          * We convert them to positive values for userspace to make
2908          * recognizing true errors easier.
2909          * Note we can only return these detailed results on async requests,
2910          * as sync requests look the same as i/o requests for locking. */
2911         if (result == -ECANCELED)
2912                 result = LLA_RESULT_DIFFERENT;
2913         else if (result == -EEXIST)
2914                 result = LLA_RESULT_SAME;
2915
2916 out:
2917         RETURN(result);
2918 }
2919 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
2920
2921 static int ll_ladvise_sanity(struct inode *inode,
2922                              struct llapi_lu_ladvise *ladvise)
2923 {
2924         enum lu_ladvise_type advice = ladvise->lla_advice;
2925         /* Note the peradvice flags is a 32 bit field, so per advice flags must
2926          * be in the first 32 bits of enum ladvise_flags */
2927         __u32 flags = ladvise->lla_peradvice_flags;
2928         /* 3 lines at 80 characters per line, should be plenty */
2929         int rc = 0;
2930
2931         if (advice > LU_LADVISE_MAX || advice == LU_LADVISE_INVALID) {
2932                 rc = -EINVAL;
2933                 CDEBUG(D_VFSTRACE, "%s: advice with value '%d' not recognized,"
2934                        "last supported advice is %s (value '%d'): rc = %d\n",
2935                        ll_get_fsname(inode->i_sb, NULL, 0), advice,
2936                        ladvise_names[LU_LADVISE_MAX-1], LU_LADVISE_MAX-1, rc);
2937                 GOTO(out, rc);
2938         }
2939
2940         /* Per-advice checks */
2941         switch (advice) {
2942         case LU_LADVISE_LOCKNOEXPAND:
2943                 if (flags & ~LF_LOCKNOEXPAND_MASK) {
2944                         rc = -EINVAL;
2945                         CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
2946                                "rc = %d\n",
2947                                ll_get_fsname(inode->i_sb, NULL, 0), flags,
2948                                ladvise_names[advice], rc);
2949                         GOTO(out, rc);
2950                 }
2951                 break;
2952         case LU_LADVISE_LOCKAHEAD:
2953                 /* Currently only READ and WRITE modes can be requested */
2954                 if (ladvise->lla_lockahead_mode >= MODE_MAX_USER ||
2955                     ladvise->lla_lockahead_mode == 0) {
2956                         rc = -EINVAL;
2957                         CDEBUG(D_VFSTRACE, "%s: Invalid mode (%d) for %s: "
2958                                "rc = %d\n",
2959                                ll_get_fsname(inode->i_sb, NULL, 0),
2960                                ladvise->lla_lockahead_mode,
2961                                ladvise_names[advice], rc);
2962                         GOTO(out, rc);
2963                 }
2964         case LU_LADVISE_WILLREAD:
2965         case LU_LADVISE_DONTNEED:
2966         default:
2967                 /* Note fall through above - These checks apply to all advices
2968                  * except LOCKNOEXPAND */
2969                 if (flags & ~LF_DEFAULT_MASK) {
2970                         rc = -EINVAL;
2971                         CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
2972                                "rc = %d\n",
2973                                ll_get_fsname(inode->i_sb, NULL, 0), flags,
2974                                ladvise_names[advice], rc);
2975                         GOTO(out, rc);
2976                 }
2977                 if (ladvise->lla_start >= ladvise->lla_end) {
2978                         rc = -EINVAL;
2979                         CDEBUG(D_VFSTRACE, "%s: Invalid range (%llu to %llu) "
2980                                "for %s: rc = %d\n",
2981                                ll_get_fsname(inode->i_sb, NULL, 0),
2982                                ladvise->lla_start, ladvise->lla_end,
2983                                ladvise_names[advice], rc);
2984                         GOTO(out, rc);
2985                 }
2986                 break;
2987         }
2988
2989 out:
2990         return rc;
2991 }
2992 #undef ERRSIZE
2993
2994 /*
2995  * Give file access advices
2996  *
2997  * The ladvise interface is similar to Linux fadvise() system call, except it
2998  * forwards the advices directly from Lustre client to server. The server side
2999  * codes will apply appropriate read-ahead and caching techniques for the
3000  * corresponding files.
3001  *
3002  * A typical workload for ladvise is e.g. a bunch of different clients are
3003  * doing small random reads of a file, so prefetching pages into OSS cache
3004  * with big linear reads before the random IO is a net benefit. Fetching
3005  * all that data into each client cache with fadvise() may not be, due to
3006  * much more data being sent to the client.
3007  */
3008 static int ll_ladvise(struct inode *inode, struct file *file, __u64 flags,
3009                       struct llapi_lu_ladvise *ladvise)
3010 {
3011         struct lu_env *env;
3012         struct cl_io *io;
3013         struct cl_ladvise_io *lio;
3014         int rc;
3015         __u16 refcheck;
3016         ENTRY;
3017
3018         env = cl_env_get(&refcheck);
3019         if (IS_ERR(env))
3020                 RETURN(PTR_ERR(env));
3021
3022         io = vvp_env_thread_io(env);
3023         io->ci_obj = ll_i2info(inode)->lli_clob;
3024
3025         /* initialize parameters for ladvise */
3026         lio = &io->u.ci_ladvise;
3027         lio->li_start = ladvise->lla_start;
3028         lio->li_end = ladvise->lla_end;
3029         lio->li_fid = ll_inode2fid(inode);
3030         lio->li_advice = ladvise->lla_advice;
3031         lio->li_flags = flags;
3032
3033         if (cl_io_init(env, io, CIT_LADVISE, io->ci_obj) == 0)
3034                 rc = cl_io_loop(env, io);
3035         else
3036                 rc = io->ci_result;
3037
3038         cl_io_fini(env, io);
3039         cl_env_put(env, &refcheck);
3040         RETURN(rc);
3041 }
3042
3043 static int ll_lock_noexpand(struct file *file, int flags)
3044 {
3045         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
3046
3047         fd->ll_lock_no_expand = !(flags & LF_UNSET);
3048
3049         return 0;
3050 }
3051
3052 int ll_ioctl_fsgetxattr(struct inode *inode, unsigned int cmd,
3053                         unsigned long arg)
3054 {
3055         struct fsxattr fsxattr;
3056
3057         if (copy_from_user(&fsxattr,
3058                            (const struct fsxattr __user *)arg,
3059                            sizeof(fsxattr)))
3060                 RETURN(-EFAULT);
3061
3062         fsxattr.fsx_xflags = ll_inode_flags_to_xflags(inode->i_flags);
3063         if (ll_file_test_flag(ll_i2info(inode), LLIF_PROJECT_INHERIT))
3064                 fsxattr.fsx_xflags |= FS_XFLAG_PROJINHERIT;
3065         fsxattr.fsx_projid = ll_i2info(inode)->lli_projid;
3066         if (copy_to_user((struct fsxattr __user *)arg,
3067                          &fsxattr, sizeof(fsxattr)))
3068                 RETURN(-EFAULT);
3069
3070         RETURN(0);
3071 }
3072
3073 int ll_ioctl_fssetxattr(struct inode *inode, unsigned int cmd,
3074                         unsigned long arg)
3075 {
3076
3077         struct md_op_data *op_data;
3078         struct ptlrpc_request *req = NULL;
3079         int rc = 0;
3080         struct fsxattr fsxattr;
3081         struct cl_object *obj;
3082         struct iattr *attr;
3083         int flags;
3084
3085         /* only root could change project ID */
3086         if (!cfs_capable(CFS_CAP_SYS_ADMIN))
3087                 RETURN(-EPERM);
3088
3089         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3090                                      LUSTRE_OPC_ANY, NULL);
3091         if (IS_ERR(op_data))
3092                 RETURN(PTR_ERR(op_data));
3093
3094         if (copy_from_user(&fsxattr,
3095                            (const struct fsxattr __user *)arg,
3096                            sizeof(fsxattr)))
3097                 GOTO(out_fsxattr, rc = -EFAULT);
3098
3099         flags = ll_xflags_to_inode_flags(fsxattr.fsx_xflags);
3100         op_data->op_attr_flags = ll_inode_to_ext_flags(flags);
3101         if (fsxattr.fsx_xflags & FS_XFLAG_PROJINHERIT)
3102                 op_data->op_attr_flags |= LUSTRE_PROJINHERIT_FL;
3103         op_data->op_projid = fsxattr.fsx_projid;
3104         op_data->op_xvalid |= OP_XVALID_PROJID | OP_XVALID_FLAGS;
3105         rc = md_setattr(ll_i2sbi(inode)->ll_md_exp, op_data, NULL,
3106                         0, &req);
3107         ptlrpc_req_finished(req);
3108         if (rc)
3109                 GOTO(out_fsxattr, rc);
3110         ll_update_inode_flags(inode, op_data->op_attr_flags);
3111         obj = ll_i2info(inode)->lli_clob;
3112         if (obj == NULL)
3113                 GOTO(out_fsxattr, rc);
3114
3115         OBD_ALLOC_PTR(attr);
3116         if (attr == NULL)
3117                 GOTO(out_fsxattr, rc = -ENOMEM);
3118
3119         rc = cl_setattr_ost(obj, attr, OP_XVALID_FLAGS,
3120                             fsxattr.fsx_xflags);
3121         OBD_FREE_PTR(attr);
3122 out_fsxattr:
3123         ll_finish_md_op_data(op_data);
3124         RETURN(rc);
3125 }
3126
3127 static long ll_file_unlock_lease(struct file *file, struct ll_ioc_lease *ioc,
3128                                  unsigned long arg)
3129 {
3130         struct inode            *inode = file_inode(file);
3131         struct ll_file_data     *fd = LUSTRE_FPRIVATE(file);
3132         struct ll_inode_info    *lli = ll_i2info(inode);
3133         struct obd_client_handle *och = NULL;
3134         struct split_param sp;
3135         bool lease_broken;
3136         fmode_t fmode = 0;
3137         enum mds_op_bias bias = 0;
3138         struct file *layout_file = NULL;
3139         void *data = NULL;
3140         size_t data_size = 0;
3141         long rc;
3142         ENTRY;
3143
3144         mutex_lock(&lli->lli_och_mutex);
3145         if (fd->fd_lease_och != NULL) {
3146                 och = fd->fd_lease_och;
3147                 fd->fd_lease_och = NULL;
3148         }
3149         mutex_unlock(&lli->lli_och_mutex);
3150
3151         if (och == NULL)
3152                 GOTO(out, rc = -ENOLCK);
3153
3154         fmode = och->och_flags;
3155
3156         switch (ioc->lil_flags) {
3157         case LL_LEASE_RESYNC_DONE:
3158                 if (ioc->lil_count > IOC_IDS_MAX)
3159                         GOTO(out, rc = -EINVAL);
3160
3161                 data_size = offsetof(typeof(*ioc), lil_ids[ioc->lil_count]);
3162                 OBD_ALLOC(data, data_size);
3163                 if (!data)
3164                         GOTO(out, rc = -ENOMEM);
3165
3166                 if (copy_from_user(data, (void __user *)arg, data_size))
3167                         GOTO(out, rc = -EFAULT);
3168
3169                 bias = MDS_CLOSE_RESYNC_DONE;
3170                 break;
3171         case LL_LEASE_LAYOUT_MERGE: {
3172                 int fd;
3173
3174                 if (ioc->lil_count != 1)
3175                         GOTO(out, rc = -EINVAL);
3176
3177                 arg += sizeof(*ioc);
3178                 if (copy_from_user(&fd, (void __user *)arg, sizeof(__u32)))
3179                         GOTO(out, rc = -EFAULT);
3180
3181                 layout_file = fget(fd);
3182                 if (!layout_file)
3183                         GOTO(out, rc = -EBADF);
3184
3185                 if ((file->f_flags & O_ACCMODE) == O_RDONLY ||
3186                                 (layout_file->f_flags & O_ACCMODE) == O_RDONLY)
3187                         GOTO(out, rc = -EPERM);
3188
3189                 data = file_inode(layout_file);
3190                 bias = MDS_CLOSE_LAYOUT_MERGE;
3191                 break;
3192         }
3193         case LL_LEASE_LAYOUT_SPLIT: {
3194                 int fdv;
3195                 int mirror_id;
3196
3197                 if (ioc->lil_count != 2)
3198                         GOTO(out, rc = -EINVAL);
3199
3200                 arg += sizeof(*ioc);
3201                 if (copy_from_user(&fdv, (void __user *)arg, sizeof(__u32)))
3202                         GOTO(out, rc = -EFAULT);
3203
3204                 arg += sizeof(__u32);
3205                 if (copy_from_user(&mirror_id, (void __user *)arg,
3206                                    sizeof(__u32)))
3207                         GOTO(out, rc = -EFAULT);
3208
3209                 layout_file = fget(fdv);
3210                 if (!layout_file)
3211                         GOTO(out, rc = -EBADF);
3212
3213                 sp.sp_inode = file_inode(layout_file);
3214                 sp.sp_mirror_id = (__u16)mirror_id;
3215                 data = &sp;
3216                 bias = MDS_CLOSE_LAYOUT_SPLIT;
3217                 break;
3218         }
3219         default:
3220                 /* without close intent */
3221                 break;
3222         }
3223
3224         rc = ll_lease_close_intent(och, inode, &lease_broken, bias, data);
3225         if (rc < 0)
3226                 GOTO(out, rc);
3227
3228         rc = ll_lease_och_release(inode, file);
3229         if (rc < 0)
3230                 GOTO(out, rc);
3231
3232         if (lease_broken)
3233                 fmode = 0;
3234         EXIT;
3235
3236 out:
3237         switch (ioc->lil_flags) {
3238         case LL_LEASE_RESYNC_DONE:
3239                 if (data)
3240                         OBD_FREE(data, data_size);
3241                 break;
3242         case LL_LEASE_LAYOUT_MERGE:
3243         case LL_LEASE_LAYOUT_SPLIT:
3244                 if (layout_file)
3245                         fput(layout_file);
3246                 break;
3247         }
3248
3249         if (!rc)
3250                 rc = ll_lease_type_from_fmode(fmode);
3251         RETURN(rc);
3252 }
3253
3254 static long ll_file_set_lease(struct file *file, struct ll_ioc_lease *ioc,
3255                               unsigned long arg)
3256 {
3257         struct inode *inode = file_inode(file);
3258         struct ll_inode_info *lli = ll_i2info(inode);
3259         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
3260         struct obd_client_handle *och = NULL;
3261         __u64 open_flags = 0;
3262         bool lease_broken;
3263         fmode_t fmode;
3264         long rc;
3265         ENTRY;
3266
3267         switch (ioc->lil_mode) {
3268         case LL_LEASE_WRLCK:
3269                 if (!(file->f_mode & FMODE_WRITE))
3270                         RETURN(-EPERM);
3271                 fmode = FMODE_WRITE;
3272                 break;
3273         case LL_LEASE_RDLCK:
3274                 if (!(file->f_mode & FMODE_READ))
3275                         RETURN(-EPERM);
3276                 fmode = FMODE_READ;
3277                 break;
3278         case LL_LEASE_UNLCK:
3279                 RETURN(ll_file_unlock_lease(file, ioc, arg));
3280         default:
3281                 RETURN(-EINVAL);
3282         }
3283
3284         CDEBUG(D_INODE, "Set lease with mode %u\n", fmode);
3285
3286         /* apply for lease */
3287         if (ioc->lil_flags & LL_LEASE_RESYNC)
3288                 open_flags = MDS_OPEN_RESYNC;
3289         och = ll_lease_open(inode, file, fmode, open_flags);
3290         if (IS_ERR(och))
3291                 RETURN(PTR_ERR(och));
3292
3293         if (ioc->lil_flags & LL_LEASE_RESYNC) {
3294                 rc = ll_lease_file_resync(och, inode);
3295                 if (rc) {
3296                         ll_lease_close(och, inode, NULL);
3297                         RETURN(rc);
3298                 }
3299                 rc = ll_layout_refresh(inode, &fd->fd_layout_version);
3300                 if (rc) {
3301                         ll_lease_close(och, inode, NULL);
3302                         RETURN(rc);
3303                 }
3304         }
3305
3306         rc = 0;
3307         mutex_lock(&lli->lli_och_mutex);
3308         if (fd->fd_lease_och == NULL) {
3309                 fd->fd_lease_och = och;
3310                 och = NULL;
3311         }
3312         mutex_unlock(&lli->lli_och_mutex);
3313         if (och != NULL) {
3314                 /* impossible now that only excl is supported for now */
3315                 ll_lease_close(och, inode, &lease_broken);
3316                 rc = -EBUSY;
3317         }
3318         RETURN(rc);
3319 }
3320
3321 static long
3322 ll_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3323 {
3324         struct inode            *inode = file_inode(file);
3325         struct ll_file_data     *fd = LUSTRE_FPRIVATE(file);
3326         int                      flags, rc;
3327         ENTRY;
3328
3329         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%x\n",
3330                PFID(ll_inode2fid(inode)), inode, cmd);
3331         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
3332
3333         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
3334         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
3335                 RETURN(-ENOTTY);
3336
3337         switch (cmd) {
3338         case LL_IOC_GETFLAGS:
3339                 /* Get the current value of the file flags */
3340                 return put_user(fd->fd_flags, (int __user *)arg);
3341         case LL_IOC_SETFLAGS:
3342         case LL_IOC_CLRFLAGS:
3343                 /* Set or clear specific file flags */
3344                 /* XXX This probably needs checks to ensure the flags are
3345                  *     not abused, and to handle any flag side effects.
3346                  */
3347                 if (get_user(flags, (int __user *) arg))
3348                         RETURN(-EFAULT);
3349
3350                 if (cmd == LL_IOC_SETFLAGS) {
3351                         if ((flags & LL_FILE_IGNORE_LOCK) &&
3352                             !(file->f_flags & O_DIRECT)) {
3353                                 CERROR("%s: unable to disable locking on "
3354                                        "non-O_DIRECT file\n", current->comm);
3355                                 RETURN(-EINVAL);
3356                         }
3357
3358                         fd->fd_flags |= flags;
3359                 } else {
3360                         fd->fd_flags &= ~flags;
3361                 }
3362                 RETURN(0);
3363         case LL_IOC_LOV_SETSTRIPE:
3364         case LL_IOC_LOV_SETSTRIPE_NEW:
3365                 RETURN(ll_lov_setstripe(inode, file, (void __user *)arg));
3366         case LL_IOC_LOV_SETEA:
3367                 RETURN(ll_lov_setea(inode, file, (void __user *)arg));
3368         case LL_IOC_LOV_SWAP_LAYOUTS: {
3369                 struct file *file2;
3370                 struct lustre_swap_layouts lsl;
3371
3372                 if (copy_from_user(&lsl, (char __user *)arg,
3373                                    sizeof(struct lustre_swap_layouts)))
3374                         RETURN(-EFAULT);
3375
3376                 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
3377                         RETURN(-EPERM);
3378
3379                 file2 = fget(lsl.sl_fd);
3380                 if (file2 == NULL)
3381                         RETURN(-EBADF);
3382
3383                 /* O_WRONLY or O_RDWR */
3384                 if ((file2->f_flags & O_ACCMODE) == O_RDONLY)
3385                         GOTO(out, rc = -EPERM);
3386
3387                 if (lsl.sl_flags & SWAP_LAYOUTS_CLOSE) {
3388                         struct inode                    *inode2;
3389                         struct ll_inode_info            *lli;
3390                         struct obd_client_handle        *och = NULL;
3391
3392                         lli = ll_i2info(inode);
3393                         mutex_lock(&lli->lli_och_mutex);
3394                         if (fd->fd_lease_och != NULL) {
3395                                 och = fd->fd_lease_och;
3396                                 fd->fd_lease_och = NULL;
3397                         }
3398                         mutex_unlock(&lli->lli_och_mutex);
3399                         if (och == NULL)
3400                                 GOTO(out, rc = -ENOLCK);
3401                         inode2 = file_inode(file2);
3402                         rc = ll_swap_layouts_close(och, inode, inode2);
3403                 } else {
3404                         rc = ll_swap_layouts(file, file2, &lsl);
3405                 }
3406 out:
3407                 fput(file2);
3408                 RETURN(rc);
3409         }
3410         case LL_IOC_LOV_GETSTRIPE:
3411         case LL_IOC_LOV_GETSTRIPE_NEW:
3412                 RETURN(ll_file_getstripe(inode, (void __user *)arg, 0));
3413         case FS_IOC_GETFLAGS:
3414         case FS_IOC_SETFLAGS:
3415                 RETURN(ll_iocontrol(inode, file, cmd, arg));
3416         case FSFILT_IOC_GETVERSION:
3417         case FS_IOC_GETVERSION:
3418                 RETURN(put_user(inode->i_generation, (int __user *)arg));
3419         /* We need to special case any other ioctls we want to handle,
3420          * to send them to the MDS/OST as appropriate and to properly
3421          * network encode the arg field. */
3422         case FS_IOC_SETVERSION:
3423                 RETURN(-ENOTSUPP);
3424
3425         case LL_IOC_GROUP_LOCK:
3426                 RETURN(ll_get_grouplock(inode, file, arg));
3427         case LL_IOC_GROUP_UNLOCK:
3428                 RETURN(ll_put_grouplock(inode, file, arg));
3429         case IOC_OBD_STATFS:
3430                 RETURN(ll_obd_statfs(inode, (void __user *)arg));
3431
3432         case LL_IOC_FLUSHCTX:
3433                 RETURN(ll_flush_ctx(inode));
3434         case LL_IOC_PATH2FID: {
3435                 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
3436                                  sizeof(struct lu_fid)))
3437                         RETURN(-EFAULT);
3438
3439                 RETURN(0);
3440         }
3441         case LL_IOC_GETPARENT:
3442                 RETURN(ll_getparent(file, (struct getparent __user *)arg));
3443
3444         case OBD_IOC_FID2PATH:
3445                 RETURN(ll_fid2path(inode, (void __user *)arg));
3446         case LL_IOC_DATA_VERSION: {
3447                 struct ioc_data_version idv;
3448                 int rc;
3449
3450                 if (copy_from_user(&idv, (char __user *)arg, sizeof(idv)))
3451                         RETURN(-EFAULT);
3452
3453                 idv.idv_flags &= LL_DV_RD_FLUSH | LL_DV_WR_FLUSH;
3454                 rc = ll_ioc_data_version(inode, &idv);
3455
3456                 if (rc == 0 &&
3457                     copy_to_user((char __user *)arg, &idv, sizeof(idv)))
3458                         RETURN(-EFAULT);
3459
3460                 RETURN(rc);
3461         }
3462
3463         case LL_IOC_GET_MDTIDX: {
3464                 int mdtidx;
3465
3466                 mdtidx = ll_get_mdt_idx(inode);
3467                 if (mdtidx < 0)
3468                         RETURN(mdtidx);
3469
3470                 if (put_user((int)mdtidx, (int __user *)arg))
3471                         RETURN(-EFAULT);
3472
3473                 RETURN(0);
3474         }
3475         case OBD_IOC_GETDTNAME:
3476         case OBD_IOC_GETMDNAME:
3477                 RETURN(ll_get_obd_name(inode, cmd, arg));
3478         case LL_IOC_HSM_STATE_GET: {
3479                 struct md_op_data       *op_data;
3480                 struct hsm_user_state   *hus;
3481                 int                      rc;
3482
3483                 OBD_ALLOC_PTR(hus);
3484                 if (hus == NULL)
3485                         RETURN(-ENOMEM);
3486
3487                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3488                                              LUSTRE_OPC_ANY, hus);
3489                 if (IS_ERR(op_data)) {
3490                         OBD_FREE_PTR(hus);
3491                         RETURN(PTR_ERR(op_data));
3492                 }
3493
3494                 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
3495                                    op_data, NULL);
3496
3497                 if (copy_to_user((void __user *)arg, hus, sizeof(*hus)))
3498                         rc = -EFAULT;
3499
3500                 ll_finish_md_op_data(op_data);
3501                 OBD_FREE_PTR(hus);
3502                 RETURN(rc);
3503         }
3504         case LL_IOC_HSM_STATE_SET: {
3505                 struct hsm_state_set    *hss;
3506                 int                      rc;
3507
3508                 OBD_ALLOC_PTR(hss);
3509                 if (hss == NULL)
3510                         RETURN(-ENOMEM);
3511
3512                 if (copy_from_user(hss, (char __user *)arg, sizeof(*hss))) {
3513                         OBD_FREE_PTR(hss);
3514                         RETURN(-EFAULT);
3515                 }
3516
3517                 rc = ll_hsm_state_set(inode, hss);
3518
3519                 OBD_FREE_PTR(hss);
3520                 RETURN(rc);
3521         }
3522         case LL_IOC_HSM_ACTION: {
3523                 struct md_op_data               *op_data;
3524                 struct hsm_current_action       *hca;
3525                 int                              rc;
3526
3527                 OBD_ALLOC_PTR(hca);
3528                 if (hca == NULL)
3529                         RETURN(-ENOMEM);
3530
3531                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3532                                              LUSTRE_OPC_ANY, hca);
3533                 if (IS_ERR(op_data)) {
3534                         OBD_FREE_PTR(hca);
3535                         RETURN(PTR_ERR(op_data));
3536                 }
3537
3538                 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
3539                                    op_data, NULL);
3540
3541                 if (copy_to_user((char __user *)arg, hca, sizeof(*hca)))
3542                         rc = -EFAULT;
3543
3544                 ll_finish_md_op_data(op_data);
3545                 OBD_FREE_PTR(hca);
3546                 RETURN(rc);
3547         }
3548         case LL_IOC_SET_LEASE_OLD: {
3549                 struct ll_ioc_lease ioc = { .lil_mode = (__u32)arg };
3550
3551                 RETURN(ll_file_set_lease(file, &ioc, 0));
3552         }
3553         case LL_IOC_SET_LEASE: {
3554                 struct ll_ioc_lease ioc;
3555
3556                 if (copy_from_user(&ioc, (void __user *)arg, sizeof(ioc)))
3557                         RETURN(-EFAULT);
3558
3559                 RETURN(ll_file_set_lease(file, &ioc, arg));
3560         }
3561         case LL_IOC_GET_LEASE: {
3562                 struct ll_inode_info *lli = ll_i2info(inode);
3563                 struct ldlm_lock *lock = NULL;
3564                 fmode_t fmode = 0;
3565
3566                 mutex_lock(&lli->lli_och_mutex);
3567                 if (fd->fd_lease_och != NULL) {
3568                         struct obd_client_handle *och = fd->fd_lease_och;
3569
3570                         lock = ldlm_handle2lock(&och->och_lease_handle);
3571                         if (lock != NULL) {
3572                                 lock_res_and_lock(lock);
3573                                 if (!ldlm_is_cancel(lock))
3574                                         fmode = och->och_flags;
3575
3576                                 unlock_res_and_lock(lock);
3577                                 LDLM_LOCK_PUT(lock);
3578                         }
3579                 }
3580                 mutex_unlock(&lli->lli_och_mutex);
3581
3582                 RETURN(ll_lease_type_from_fmode(fmode));
3583         }
3584         case LL_IOC_HSM_IMPORT: {
3585                 struct hsm_user_import *hui;
3586
3587                 OBD_ALLOC_PTR(hui);
3588                 if (hui == NULL)
3589                         RETURN(-ENOMEM);
3590
3591                 if (copy_from_user(hui, (void __user *)arg, sizeof(*hui))) {
3592                         OBD_FREE_PTR(hui);
3593                         RETURN(-EFAULT);
3594                 }
3595
3596                 rc = ll_hsm_import(inode, file, hui);
3597
3598                 OBD_FREE_PTR(hui);
3599                 RETURN(rc);
3600         }
3601         case LL_IOC_FUTIMES_3: {
3602                 struct ll_futimes_3 lfu;
3603
3604                 if (copy_from_user(&lfu,
3605                                    (const struct ll_futimes_3 __user *)arg,
3606                                    sizeof(lfu)))
3607                         RETURN(-EFAULT);
3608
3609                 RETURN(ll_file_futimes_3(file, &lfu));
3610         }
3611         case LL_IOC_LADVISE: {
3612                 struct llapi_ladvise_hdr *k_ladvise_hdr;
3613                 struct llapi_ladvise_hdr __user *u_ladvise_hdr;
3614                 int i;
3615                 int num_advise;
3616                 int alloc_size = sizeof(*k_ladvise_hdr);
3617
3618                 rc = 0;
3619                 u_ladvise_hdr = (void __user *)arg;
3620                 OBD_ALLOC_PTR(k_ladvise_hdr);
3621                 if (k_ladvise_hdr == NULL)
3622                         RETURN(-ENOMEM);
3623
3624                 if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
3625                         GOTO(out_ladvise, rc = -EFAULT);
3626
3627                 if (k_ladvise_hdr->lah_magic != LADVISE_MAGIC ||
3628                     k_ladvise_hdr->lah_count < 1)
3629                         GOTO(out_ladvise, rc = -EINVAL);
3630
3631                 num_advise = k_ladvise_hdr->lah_count;
3632                 if (num_advise >= LAH_COUNT_MAX)
3633                         GOTO(out_ladvise, rc = -EFBIG);
3634
3635                 OBD_FREE_PTR(k_ladvise_hdr);
3636                 alloc_size = offsetof(typeof(*k_ladvise_hdr),
3637                                       lah_advise[num_advise]);
3638                 OBD_ALLOC(k_ladvise_hdr, alloc_size);
3639                 if (k_ladvise_hdr == NULL)
3640                         RETURN(-ENOMEM);
3641
3642                 /*
3643                  * TODO: submit multiple advices to one server in a single RPC
3644                  */
3645                 if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
3646                         GOTO(out_ladvise, rc = -EFAULT);
3647
3648                 for (i = 0; i < num_advise; i++) {
3649                         struct llapi_lu_ladvise *k_ladvise =
3650                                         &k_ladvise_hdr->lah_advise[i];
3651                         struct llapi_lu_ladvise __user *u_ladvise =
3652                                         &u_ladvise_hdr->lah_advise[i];
3653
3654                         rc = ll_ladvise_sanity(inode, k_ladvise);
3655                         if (rc)
3656                                 GOTO(out_ladvise, rc);
3657
3658                         switch (k_ladvise->lla_advice) {
3659                         case LU_LADVISE_LOCKNOEXPAND:
3660                                 rc = ll_lock_noexpand(file,
3661                                                k_ladvise->lla_peradvice_flags);
3662                                 GOTO(out_ladvise, rc);
3663                         case LU_LADVISE_LOCKAHEAD:
3664
3665                                 rc = ll_file_lock_ahead(file, k_ladvise);
3666
3667                                 if (rc < 0)
3668                                         GOTO(out_ladvise, rc);
3669
3670                                 if (put_user(rc,
3671                                              &u_ladvise->lla_lockahead_result))
3672                                         GOTO(out_ladvise, rc = -EFAULT);
3673                                 break;
3674                         default:
3675                                 rc = ll_ladvise(inode, file,
3676                                                 k_ladvise_hdr->lah_flags,
3677                                                 k_ladvise);
3678                                 if (rc)
3679                                         GOTO(out_ladvise, rc);
3680                                 break;
3681                         }
3682
3683                 }
3684
3685 out_ladvise:
3686                 OBD_FREE(k_ladvise_hdr, alloc_size);
3687                 RETURN(rc);
3688         }
3689         case LL_IOC_FLR_SET_MIRROR: {
3690                 /* mirror I/O must be direct to avoid polluting page cache
3691                  * by stale data. */
3692                 if (!(file->f_flags & O_DIRECT))
3693                         RETURN(-EINVAL);
3694
3695                 fd->fd_designated_mirror = (__u32)arg;
3696                 RETURN(0);
3697         }
3698         case LL_IOC_FSGETXATTR:
3699                 RETURN(ll_ioctl_fsgetxattr(inode, cmd, arg));
3700         case LL_IOC_FSSETXATTR:
3701                 RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
3702         case BLKSSZGET:
3703                 RETURN(put_user(PAGE_SIZE, (int __user *)arg));
3704         default:
3705                 RETURN(obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
3706                                      (void __user *)arg));
3707         }
3708 }
3709
3710 #ifndef HAVE_FILE_LLSEEK_SIZE
3711 static inline loff_t
3712 llseek_execute(struct file *file, loff_t offset, loff_t maxsize)
3713 {
3714         if (offset < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET))
3715                 return -EINVAL;
3716         if (offset > maxsize)
3717                 return -EINVAL;
3718
3719         if (offset != file->f_pos) {
3720                 file->f_pos = offset;
3721                 file->f_version = 0;
3722         }
3723         return offset;
3724 }
3725
3726 static loff_t
3727 generic_file_llseek_size(struct file *file, loff_t offset, int origin,
3728                 loff_t maxsize, loff_t eof)
3729 {
3730         struct inode *inode = file_inode(file);
3731
3732         switch (origin) {
3733         case SEEK_END:
3734                 offset += eof;
3735                 break;
3736         case SEEK_CUR:
3737                 /*
3738                  * Here we special-case the lseek(fd, 0, SEEK_CUR)
3739                  * position-querying operation.  Avoid rewriting the "same"
3740                  * f_pos value back to the file because a concurrent read(),
3741                  * write() or lseek() might have altered it
3742                  */
3743                 if (offset == 0)
3744                         return file->f_pos;
3745                 /*
3746                  * f_lock protects against read/modify/write race with other
3747                  * SEEK_CURs. Note that parallel writes and reads behave
3748                  * like SEEK_SET.
3749                  */
3750                 inode_lock(inode);
3751                 offset = llseek_execute(file, file->f_pos + offset, maxsize);
3752                 inode_unlock(inode);
3753                 return offset;
3754         case SEEK_DATA:
3755                 /*
3756                  * In the generic case the entire file is data, so as long as
3757                  * offset isn't at the end of the file then the offset is data.
3758                  */
3759                 if (offset >= eof)
3760                         return -ENXIO;
3761                 break;
3762         case SEEK_HOLE:
3763                 /*
3764                  * There is a virtual hole at the end of the file, so as long as
3765                  * offset isn't i_size or larger, return i_size.
3766                  */
3767                 if (offset >= eof)
3768                         return -ENXIO;
3769                 offset = eof;
3770                 break;
3771         }
3772
3773         return llseek_execute(file, offset, maxsize);
3774 }
3775 #endif
3776
3777 static loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
3778 {
3779         struct inode *inode = file_inode(file);
3780         loff_t retval, eof = 0;
3781
3782         ENTRY;
3783         retval = offset + ((origin == SEEK_END) ? i_size_read(inode) :
3784                            (origin == SEEK_CUR) ? file->f_pos : 0);
3785         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), to=%llu=%#llx(%d)\n",
3786                PFID(ll_inode2fid(inode)), inode, retval, retval,
3787                origin);
3788         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LLSEEK, 1);
3789
3790         if (origin == SEEK_END || origin == SEEK_HOLE || origin == SEEK_DATA) {
3791                 retval = ll_glimpse_size(inode);
3792                 if (retval != 0)
3793                         RETURN(retval);
3794                 eof = i_size_read(inode);
3795         }
3796
3797         retval = ll_generic_file_llseek_size(file, offset, origin,
3798                                           ll_file_maxbytes(inode), eof);
3799         RETURN(retval);
3800 }
3801
3802 static int ll_flush(struct file *file, fl_owner_t id)
3803 {
3804         struct inode *inode = file_inode(file);
3805         struct ll_inode_info *lli = ll_i2info(inode);
3806         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
3807         int rc, err;
3808
3809         LASSERT(!S_ISDIR(inode->i_mode));
3810
3811         /* catch async errors that were recorded back when async writeback
3812          * failed for pages in this mapping. */
3813         rc = lli->lli_async_rc;
3814         lli->lli_async_rc = 0;
3815         if (lli->lli_clob != NULL) {
3816                 err = lov_read_and_clear_async_rc(lli->lli_clob);
3817                 if (rc == 0)
3818                         rc = err;
3819         }
3820
3821         /* The application has been told write failure already.
3822          * Do not report failure again. */
3823         if (fd->fd_write_failed)
3824                 return 0;
3825         return rc ? -EIO : 0;
3826 }
3827
3828 /**
3829  * Called to make sure a portion of file has been written out.
3830  * if @mode is not CL_FSYNC_LOCAL, it will send OST_SYNC RPCs to OST.
3831  *
3832  * Return how many pages have been written.
3833  */
3834 int cl_sync_file_range(struct inode *inode, loff_t start, loff_t end,
3835                        enum cl_fsync_mode mode, int ignore_layout)
3836 {
3837         struct lu_env *env;
3838         struct cl_io *io;
3839         struct cl_fsync_io *fio;
3840         int result;
3841         __u16 refcheck;
3842         ENTRY;
3843
3844         if (mode != CL_FSYNC_NONE && mode != CL_FSYNC_LOCAL &&
3845             mode != CL_FSYNC_DISCARD && mode != CL_FSYNC_ALL)
3846                 RETURN(-EINVAL);
3847
3848         env = cl_env_get(&refcheck);
3849         if (IS_ERR(env))
3850                 RETURN(PTR_ERR(env));
3851
3852         io = vvp_env_thread_io(env);
3853         io->ci_obj = ll_i2info(inode)->lli_clob;
3854         io->ci_ignore_layout = ignore_layout;
3855
3856         /* initialize parameters for sync */
3857         fio = &io->u.ci_fsync;
3858         fio->fi_start = start;
3859         fio->fi_end = end;
3860         fio->fi_fid = ll_inode2fid(inode);
3861         fio->fi_mode = mode;
3862         fio->fi_nr_written = 0;
3863
3864         if (cl_io_init(env, io, CIT_FSYNC, io->ci_obj) == 0)
3865                 result = cl_io_loop(env, io);
3866         else
3867                 result = io->ci_result;
3868         if (result == 0)
3869                 result = fio->fi_nr_written;
3870         cl_io_fini(env, io);
3871         cl_env_put(env, &refcheck);
3872
3873         RETURN(result);
3874 }
3875
3876 /*
3877  * When dentry is provided (the 'else' case), file_dentry() may be
3878  * null and dentry must be used directly rather than pulled from
3879  * file_dentry() as is done otherwise.
3880  */
3881
3882 #ifdef HAVE_FILE_FSYNC_4ARGS
3883 int ll_fsync(struct file *file, loff_t start, loff_t end, int datasync)
3884 {
3885         struct dentry *dentry = file_dentry(file);
3886         bool lock_inode;
3887 #elif defined(HAVE_FILE_FSYNC_2ARGS)
3888 int ll_fsync(struct file *file, int datasync)
3889 {
3890         struct dentry *dentry = file_dentry(file);
3891         loff_t start = 0;
3892         loff_t end = LLONG_MAX;
3893 #else
3894 int ll_fsync(struct file *file, struct dentry *dentry, int datasync)
3895 {
3896         loff_t start = 0;
3897         loff_t end = LLONG_MAX;
3898 #endif
3899         struct inode *inode = dentry->d_inode;
3900         struct ll_inode_info *lli = ll_i2info(inode);
3901         struct ptlrpc_request *req;
3902         int rc, err;
3903         ENTRY;
3904
3905         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
3906                PFID(ll_inode2fid(inode)), inode);
3907         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FSYNC, 1);
3908
3909 #ifdef HAVE_FILE_FSYNC_4ARGS
3910         rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
3911         lock_inode = !lli->lli_inode_locked;
3912         if (lock_inode)
3913                 inode_lock(inode);
3914 #else
3915         /* fsync's caller has already called _fdata{sync,write}, we want
3916          * that IO to finish before calling the osc and mdc sync methods */
3917         rc = filemap_fdatawait(inode->i_mapping);
3918 #endif
3919
3920         /* catch async errors that were recorded back when async writeback
3921          * failed for pages in this mapping. */
3922         if (!S_ISDIR(inode->i_mode)) {
3923                 err = lli->lli_async_rc;
3924                 lli->lli_async_rc = 0;
3925                 if (rc == 0)
3926                         rc = err;
3927                 if (lli->lli_clob != NULL) {
3928                         err = lov_read_and_clear_async_rc(lli->lli_clob);
3929                         if (rc == 0)
3930                                 rc = err;
3931                 }
3932         }
3933
3934         err = md_fsync(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode), &req);
3935         if (!rc)
3936                 rc = err;
3937         if (!err)
3938                 ptlrpc_req_finished(req);
3939
3940         if (S_ISREG(inode->i_mode)) {
3941                 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
3942
3943                 err = cl_sync_file_range(inode, start, end, CL_FSYNC_ALL, 0);
3944                 if (rc == 0 && err < 0)
3945                         rc = err;
3946                 if (rc < 0)
3947                         fd->fd_write_failed = true;
3948                 else
3949                         fd->fd_write_failed = false;
3950         }
3951
3952 #ifdef HAVE_FILE_FSYNC_4ARGS
3953         if (lock_inode)
3954                 inode_unlock(inode);
3955 #endif
3956         RETURN(rc);
3957 }
3958
3959 static int
3960 ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
3961 {
3962         struct inode *inode = file_inode(file);
3963         struct ll_sb_info *sbi = ll_i2sbi(inode);
3964         struct ldlm_enqueue_info einfo = {
3965                 .ei_type        = LDLM_FLOCK,
3966                 .ei_cb_cp       = ldlm_flock_completion_ast,
3967                 .ei_cbdata      = file_lock,
3968         };
3969         struct md_op_data *op_data;
3970         struct lustre_handle lockh = { 0 };
3971         union ldlm_policy_data flock = { { 0 } };
3972         int fl_type = file_lock->fl_type;
3973         __u64 flags = 0;
3974         int rc;
3975         int rc2 = 0;
3976         ENTRY;
3977
3978         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID" file_lock=%p\n",
3979                PFID(ll_inode2fid(inode)), file_lock);
3980
3981         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FLOCK, 1);
3982
3983         if (file_lock->fl_flags & FL_FLOCK) {
3984                 LASSERT((cmd == F_SETLKW) || (cmd == F_SETLK));
3985                 /* flocks are whole-file locks */
3986                 flock.l_flock.end = OFFSET_MAX;
3987                 /* For flocks owner is determined by the local file desctiptor*/
3988                 flock.l_flock.owner = (unsigned long)file_lock->fl_file;
3989         } else if (file_lock->fl_flags & FL_POSIX) {
3990                 flock.l_flock.owner = (unsigned long)file_lock->fl_owner;
3991                 flock.l_flock.start = file_lock->fl_start;
3992                 flock.l_flock.end = file_lock->fl_end;
3993         } else {
3994                 RETURN(-EINVAL);
3995         }
3996         flock.l_flock.pid = file_lock->fl_pid;
3997
3998         /* Somewhat ugly workaround for svc lockd.
3999          * lockd installs custom fl_lmops->lm_compare_owner that checks
4000          * for the fl_owner to be the same (which it always is on local node
4001          * I guess between lockd processes) and then compares pid.
4002          * As such we assign pid to the owner field to make it all work,
4003          * conflict with normal locks is unlikely since pid space and
4004          * pointer space for current->files are not intersecting */
4005         if (file_lock->fl_lmops && file_lock->fl_lmops->lm_compare_owner)
4006                 flock.l_flock.owner = (unsigned long)file_lock->fl_pid;
4007
4008         switch (fl_type) {
4009         case F_RDLCK:
4010                 einfo.ei_mode = LCK_PR;
4011                 break;
4012         case F_UNLCK:
4013                 /* An unlock request may or may not have any relation to
4014                  * existing locks so we may not be able to pass a lock handle
4015                  * via a normal ldlm_lock_cancel() request. The request may even
4016                  * unlock a byte range in the middle of an existing lock. In
4017                  * order to process an unlock request we need all of the same
4018                  * information that is given with a normal read or write record
4019                  * lock request. To avoid creating another ldlm unlock (cancel)
4020                  * message we'll treat a LCK_NL flock request as an unlock. */
4021                 einfo.ei_mode = LCK_NL;
4022                 break;
4023         case F_WRLCK:
4024                 einfo.ei_mode = LCK_PW;
4025                 break;
4026         default:
4027                 CDEBUG(D_INFO, "Unknown fcntl lock type: %d\n", fl_type);
4028                 RETURN (-ENOTSUPP);
4029         }
4030
4031         switch (cmd) {
4032         case F_SETLKW:
4033 #ifdef F_SETLKW64
4034         case F_SETLKW64:
4035 #endif
4036                 flags = 0;
4037                 break;
4038         case F_SETLK:
4039 #ifdef F_SETLK64
4040         case F_SETLK64:
4041 #endif
4042                 flags = LDLM_FL_BLOCK_NOWAIT;
4043                 break;
4044         case F_GETLK:
4045 #ifdef F_GETLK64
4046         case F_GETLK64:
4047 #endif
4048                 flags = LDLM_FL_TEST_LOCK;
4049                 break;
4050         default:
4051                 CERROR("unknown fcntl lock command: %d\n", cmd);
4052                 RETURN (-EINVAL);
4053         }
4054
4055         /* Save the old mode so that if the mode in the lock changes we
4056          * can decrement the appropriate reader or writer refcount. */
4057         file_lock->fl_type = einfo.ei_mode;
4058
4059         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
4060                                      LUSTRE_OPC_ANY, NULL);
4061         if (IS_ERR(op_data))
4062                 RETURN(PTR_ERR(op_data));
4063
4064         CDEBUG(D_DLMTRACE, "inode="DFID", pid=%u, flags=%#llx, mode=%u, "
4065                "start=%llu, end=%llu\n", PFID(ll_inode2fid(inode)),
4066                flock.l_flock.pid, flags, einfo.ei_mode,
4067                flock.l_flock.start, flock.l_flock.end);
4068
4069         rc = md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data, &lockh,
4070                         flags);
4071
4072         /* Restore the file lock type if not TEST lock. */
4073         if (!(flags & LDLM_FL_TEST_LOCK))
4074                 file_lock->fl_type = fl_type;
4075
4076 #ifdef HAVE_LOCKS_LOCK_FILE_WAIT
4077         if ((rc == 0 || file_lock->fl_type == F_UNLCK) &&
4078             !(flags & LDLM_FL_TEST_LOCK))
4079                 rc2  = locks_lock_file_wait(file, file_lock);
4080 #else
4081         if ((file_lock->fl_flags & FL_FLOCK) &&
4082             (rc == 0 || file_lock->fl_type == F_UNLCK))
4083                 rc2  = flock_lock_file_wait(file, file_lock);
4084         if ((file_lock->fl_flags & FL_POSIX) &&
4085             (rc == 0 || file_lock->fl_type == F_UNLCK) &&
4086             !(flags & LDLM_FL_TEST_LOCK))
4087                 rc2  = posix_lock_file_wait(file, file_lock);
4088 #endif /* HAVE_LOCKS_LOCK_FILE_WAIT */
4089
4090         if (rc2 && file_lock->fl_type != F_UNLCK) {
4091                 einfo.ei_mode = LCK_NL;
4092                 md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data,
4093                            &lockh, flags);
4094                 rc = rc2;
4095         }
4096
4097         ll_finish_md_op_data(op_data);
4098
4099         RETURN(rc);
4100 }
4101
4102 int ll_get_fid_by_name(struct inode *parent, const char *name,
4103                        int namelen, struct lu_fid *fid,
4104                        struct inode **inode)
4105 {
4106         struct md_op_data       *op_data = NULL;
4107         struct mdt_body         *body;
4108         struct ptlrpc_request   *req;
4109         int                     rc;
4110         ENTRY;
4111
4112         op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen, 0,
4113                                      LUSTRE_OPC_ANY, NULL);
4114         if (IS_ERR(op_data))
4115                 RETURN(PTR_ERR(op_data));
4116
4117         op_data->op_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
4118         rc = md_getattr_name(ll_i2sbi(parent)->ll_md_exp, op_data, &req);
4119         ll_finish_md_op_data(op_data);
4120         if (rc < 0)
4121                 RETURN(rc);
4122
4123         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
4124         if (body == NULL)
4125                 GOTO(out_req, rc = -EFAULT);
4126         if (fid != NULL)
4127                 *fid = body->mbo_fid1;
4128
4129         if (inode != NULL)
4130                 rc = ll_prep_inode(inode, req, parent->i_sb, NULL);
4131 out_req:
4132         ptlrpc_req_finished(req);
4133         RETURN(rc);
4134 }
4135
4136 int ll_migrate(struct inode *parent, struct file *file, struct lmv_user_md *lum,
4137                const char *name)
4138 {
4139         struct dentry *dchild = NULL;
4140         struct inode *child_inode = NULL;
4141         struct md_op_data *op_data;
4142         struct ptlrpc_request *request = NULL;
4143         struct obd_client_handle *och = NULL;
4144         struct qstr qstr;
4145         struct mdt_body *body;
4146         __u64 data_version = 0;
4147         size_t namelen = strlen(name);
4148         int lumlen = lmv_user_md_size(lum->lum_stripe_count, lum->lum_magic);
4149         int rc;
4150         ENTRY;
4151
4152         CDEBUG(D_VFSTRACE, "migrate "DFID"/%s to MDT%04x stripe count %d\n",
4153                PFID(ll_inode2fid(parent)), name,
4154                lum->lum_stripe_offset, lum->lum_stripe_count);
4155
4156         if (lum->lum_magic != cpu_to_le32(LMV_USER_MAGIC) &&
4157             lum->lum_magic != cpu_to_le32(LMV_USER_MAGIC_SPECIFIC))
4158                 lustre_swab_lmv_user_md(lum);
4159
4160         /* Get child FID first */
4161         qstr.hash = ll_full_name_hash(file_dentry(file), name, namelen);
4162         qstr.name = name;
4163         qstr.len = namelen;
4164         dchild = d_lookup(file_dentry(file), &qstr);
4165         if (dchild) {
4166                 if (dchild->d_inode)
4167                         child_inode = igrab(dchild->d_inode);
4168                 dput(dchild);
4169         }
4170
4171         if (!child_inode) {
4172                 rc = ll_get_fid_by_name(parent, name, namelen, NULL,
4173                                         &child_inode);
4174                 if (rc)
4175                         RETURN(rc);
4176         }
4177
4178         if (!child_inode)
4179                 RETURN(-ENOENT);
4180
4181         if (!(exp_connect_flags2(ll_i2sbi(parent)->ll_md_exp) &
4182               OBD_CONNECT2_DIR_MIGRATE)) {
4183                 if (le32_to_cpu(lum->lum_stripe_count) > 1 ||
4184                     ll_i2info(child_inode)->lli_lsm_md) {
4185                         CERROR("%s: MDT doesn't support stripe directory "
4186                                "migration!\n",
4187                                ll_get_fsname(parent->i_sb, NULL, 0));
4188                         GOTO(out_iput, rc = -EOPNOTSUPP);
4189                 }
4190         }
4191
4192         /*
4193          * lfs migrate command needs to be blocked on the client
4194          * by checking the migrate FID against the FID of the
4195          * filesystem root.
4196          */
4197         if (child_inode == parent->i_sb->s_root->d_inode)
4198                 GOTO(out_iput, rc = -EINVAL);
4199
4200         op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen,
4201                                      child_inode->i_mode, LUSTRE_OPC_ANY, NULL);
4202         if (IS_ERR(op_data))
4203                 GOTO(out_iput, rc = PTR_ERR(op_data));
4204
4205         inode_lock(child_inode);
4206         op_data->op_fid3 = *ll_inode2fid(child_inode);
4207         if (!fid_is_sane(&op_data->op_fid3)) {
4208                 CERROR("%s: migrate %s, but FID "DFID" is insane\n",
4209                        ll_get_fsname(parent->i_sb, NULL, 0), name,
4210                        PFID(&op_data->op_fid3));
4211                 GOTO(out_unlock, rc = -EINVAL);
4212         }
4213
4214         op_data->op_cli_flags |= CLI_MIGRATE | CLI_SET_MEA;
4215         op_data->op_data = lum;
4216         op_data->op_data_size = lumlen;
4217
4218 again:
4219         if (S_ISREG(child_inode->i_mode)) {
4220                 och = ll_lease_open(child_inode, NULL, FMODE_WRITE, 0);
4221                 if (IS_ERR(och)) {
4222                         rc = PTR_ERR(och);
4223                         och = NULL;
4224                         GOTO(out_unlock, rc);
4225                 }
4226
4227                 rc = ll_data_version(child_inode, &data_version,
4228                                      LL_DV_WR_FLUSH);
4229                 if (rc != 0)
4230                         GOTO(out_close, rc);
4231
4232                 op_data->op_open_handle = och->och_open_handle;
4233                 op_data->op_data_version = data_version;
4234                 op_data->op_lease_handle = och->och_lease_handle;
4235                 op_data->op_bias |= MDS_CLOSE_MIGRATE;
4236
4237                 spin_lock(&och->och_mod->mod_open_req->rq_lock);
4238                 och->och_mod->mod_open_req->rq_replay = 0;
4239                 spin_unlock(&och->och_mod->mod_open_req->rq_lock);
4240         }
4241
4242         rc = md_rename(ll_i2sbi(parent)->ll_md_exp, op_data, name, namelen,
4243                        name, namelen, &request);
4244         if (rc == 0) {
4245                 LASSERT(request != NULL);
4246                 ll_update_times(request, parent);
4247
4248                 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
4249                 LASSERT(body != NULL);
4250
4251                 /* If the server does release layout lock, then we cleanup
4252                  * the client och here, otherwise release it in out_close: */
4253                 if (och && body->mbo_valid & OBD_MD_CLOSE_INTENT_EXECED) {
4254                         obd_mod_put(och->och_mod);
4255                         md_clear_open_replay_data(ll_i2sbi(parent)->ll_md_exp,
4256                                                   och);
4257                         och->och_open_handle.cookie = DEAD_HANDLE_MAGIC;
4258                         OBD_FREE_PTR(och);
4259                         och = NULL;
4260                 }
4261         }
4262
4263         if (request != NULL) {
4264                 ptlrpc_req_finished(request);
4265                 request = NULL;
4266         }
4267
4268         /* Try again if the file layout has changed. */
4269         if (rc == -EAGAIN && S_ISREG(child_inode->i_mode))
4270                 goto again;
4271
4272 out_close:
4273         if (och)
4274                 ll_lease_close(och, child_inode, NULL);
4275         if (!rc)
4276                 clear_nlink(child_inode);
4277 out_unlock:
4278         inode_unlock(child_inode);
4279         ll_finish_md_op_data(op_data);
4280 out_iput:
4281         iput(child_inode);
4282         RETURN(rc);
4283 }
4284
4285 static int
4286 ll_file_noflock(struct file *file, int cmd, struct file_lock *file_lock)
4287 {
4288         ENTRY;
4289
4290         RETURN(-ENOSYS);
4291 }
4292
4293 /**
4294  * test if some locks matching bits and l_req_mode are acquired
4295  * - bits can be in different locks
4296  * - if found clear the common lock bits in *bits
4297  * - the bits not found, are kept in *bits
4298  * \param inode [IN]
4299  * \param bits [IN] searched lock bits [IN]
4300  * \param l_req_mode [IN] searched lock mode
4301  * \retval boolean, true iff all bits are found
4302  */
4303 int ll_have_md_lock(struct inode *inode, __u64 *bits, enum ldlm_mode l_req_mode)
4304 {
4305         struct lustre_handle lockh;
4306         union ldlm_policy_data policy;
4307         enum ldlm_mode mode = (l_req_mode == LCK_MINMODE) ?
4308                               (LCK_CR | LCK_CW | LCK_PR | LCK_PW) : l_req_mode;
4309         struct lu_fid *fid;
4310         __u64 flags;
4311         int i;
4312         ENTRY;
4313
4314         if (!inode)
4315                RETURN(0);
4316
4317         fid = &ll_i2info(inode)->lli_fid;
4318         CDEBUG(D_INFO, "trying to match res "DFID" mode %s\n", PFID(fid),
4319                ldlm_lockname[mode]);
4320
4321         flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
4322         for (i = 0; i <= MDS_INODELOCK_MAXSHIFT && *bits != 0; i++) {
4323                 policy.l_inodebits.bits = *bits & (1 << i);
4324                 if (policy.l_inodebits.bits == 0)
4325                         continue;
4326
4327                 if (md_lock_match(ll_i2mdexp(inode), flags, fid, LDLM_IBITS,
4328                                   &policy, mode, &lockh)) {
4329                         struct ldlm_lock *lock;
4330
4331                         lock = ldlm_handle2lock(&lockh);
4332                         if (lock) {
4333                                 *bits &=
4334                                       ~(lock->l_policy_data.l_inodebits.bits);
4335                                 LDLM_LOCK_PUT(lock);
4336                         } else {
4337                                 *bits &= ~policy.l_inodebits.bits;
4338                         }
4339                 }
4340         }
4341         RETURN(*bits == 0);
4342 }
4343
4344 enum ldlm_mode ll_take_md_lock(struct inode *inode, __u64 bits,
4345                                struct lustre_handle *lockh, __u64 flags,
4346                                enum ldlm_mode mode)
4347 {
4348         union ldlm_policy_data policy = { .l_inodebits = { bits } };
4349         struct lu_fid *fid;
4350         enum ldlm_mode rc;
4351         ENTRY;
4352
4353         fid = &ll_i2info(inode)->lli_fid;
4354         CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
4355
4356         rc = md_lock_match(ll_i2mdexp(inode), LDLM_FL_BLOCK_GRANTED|flags,
4357                            fid, LDLM_IBITS, &policy, mode, lockh);
4358
4359         RETURN(rc);
4360 }
4361
4362 static int ll_inode_revalidate_fini(struct inode *inode, int rc)
4363 {
4364         /* Already unlinked. Just update nlink and return success */
4365         if (rc == -ENOENT) {
4366                 clear_nlink(inode);
4367                 /* If it is striped directory, and there is bad stripe
4368                  * Let's revalidate the dentry again, instead of returning
4369                  * error */
4370                 if (S_ISDIR(inode->i_mode) &&
4371                     ll_i2info(inode)->lli_lsm_md != NULL)
4372                         return 0;
4373
4374                 /* This path cannot be hit for regular files unless in
4375                  * case of obscure races, so no need to to validate
4376                  * size. */
4377                 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
4378                         return 0;
4379         } else if (rc != 0) {
4380                 CDEBUG_LIMIT((rc == -EACCES || rc == -EIDRM) ? D_INFO : D_ERROR,
4381                              "%s: revalidate FID "DFID" error: rc = %d\n",
4382                              ll_get_fsname(inode->i_sb, NULL, 0),
4383                              PFID(ll_inode2fid(inode)), rc);
4384         }
4385
4386         return rc;
4387 }
4388
4389 static int ll_inode_revalidate(struct dentry *dentry, enum ldlm_intent_flags op)
4390 {
4391         struct inode *inode = dentry->d_inode;
4392         struct obd_export *exp = ll_i2mdexp(inode);
4393         struct lookup_intent oit = {
4394                 .it_op = op,
4395         };
4396         struct ptlrpc_request *req = NULL;
4397         struct md_op_data *op_data;
4398         int rc = 0;
4399         ENTRY;
4400
4401         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p),name=%s\n",
4402                PFID(ll_inode2fid(inode)), inode, dentry->d_name.name);
4403
4404         /* Call getattr by fid, so do not provide name at all. */
4405         op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
4406                                      LUSTRE_OPC_ANY, NULL);
4407         if (IS_ERR(op_data))
4408                 RETURN(PTR_ERR(op_data));
4409
4410         rc = md_intent_lock(exp, op_data, &oit, &req, &ll_md_blocking_ast, 0);
4411         ll_finish_md_op_data(op_data);
4412         if (rc < 0) {
4413                 rc = ll_inode_revalidate_fini(inode, rc);
4414                 GOTO(out, rc);
4415         }
4416
4417         rc = ll_revalidate_it_finish(req, &oit, dentry);
4418         if (rc != 0) {
4419                 ll_intent_release(&oit);
4420                 GOTO(out, rc);
4421         }
4422
4423         /* Unlinked? Unhash dentry, so it is not picked up later by
4424          * do_lookup() -> ll_revalidate_it(). We cannot use d_drop
4425          * here to preserve get_cwd functionality on 2.6.
4426          * Bug 10503 */
4427         if (!dentry->d_inode->i_nlink) {
4428                 ll_lock_dcache(inode);
4429                 d_lustre_invalidate(dentry, 0);
4430                 ll_unlock_dcache(inode);
4431         }
4432
4433         ll_lookup_finish_locks(&oit, dentry);
4434 out:
4435         ptlrpc_req_finished(req);
4436
4437         return rc;
4438 }
4439
4440 static int ll_merge_md_attr(struct inode *inode)
4441 {
4442         struct cl_attr attr = { 0 };
4443         int rc;
4444
4445         LASSERT(ll_i2info(inode)->lli_lsm_md != NULL);
4446         rc = md_merge_attr(ll_i2mdexp(inode), ll_i2info(inode)->lli_lsm_md,
4447                            &attr, ll_md_blocking_ast);
4448         if (rc != 0)
4449                 RETURN(rc);
4450
4451         set_nlink(inode, attr.cat_nlink);
4452         inode->i_blocks = attr.cat_blocks;
4453         i_size_write(inode, attr.cat_size);
4454
4455         ll_i2info(inode)->lli_atime = attr.cat_atime;
4456         ll_i2info(inode)->lli_mtime = attr.cat_mtime;
4457         ll_i2info(inode)->lli_ctime = attr.cat_ctime;
4458
4459         RETURN(0);
4460 }
4461
4462 static inline dev_t ll_compat_encode_dev(dev_t dev)
4463 {
4464         /* The compat_sys_*stat*() syscalls will fail unless the
4465          * device majors and minors are both less than 256. Note that
4466          * the value returned here will be passed through
4467          * old_encode_dev() in cp_compat_stat(). And so we are not
4468          * trying to return a valid compat (u16) device number, just
4469          * one that will pass the old_valid_dev() check. */
4470
4471         return MKDEV(MAJOR(dev) & 0xff, MINOR(dev) & 0xff);
4472 }
4473
4474 #ifdef HAVE_INODEOPS_ENHANCED_GETATTR
4475 int ll_getattr(const struct path *path, struct kstat *stat,
4476                u32 request_mask, unsigned int flags)
4477 {
4478         struct dentry *de = path->dentry;
4479 #else
4480 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
4481 {
4482 #endif
4483         struct inode *inode = de->d_inode;
4484         struct ll_sb_info *sbi = ll_i2sbi(inode);
4485         struct ll_inode_info *lli = ll_i2info(inode);
4486         int rc;
4487
4488         ll_stats_ops_tally(sbi, LPROC_LL_GETATTR, 1);
4489
4490         rc = ll_inode_revalidate(de, IT_GETATTR);
4491         if (rc < 0)
4492                 RETURN(rc);
4493
4494         if (S_ISREG(inode->i_mode)) {
4495                 /* In case of restore, the MDT has the right size and has
4496                  * already send it back without granting the layout lock,
4497                  * inode is up-to-date so glimpse is useless.
4498                  * Also to glimpse we need the layout, in case of a running
4499                  * restore the MDT holds the layout lock so the glimpse will
4500                  * block up to the end of restore (getattr will block)
4501                  */
4502                 if (!ll_file_test_flag(lli, LLIF_FILE_RESTORING)) {
4503                         rc = ll_glimpse_size(inode);
4504                         if (rc < 0)
4505                                 RETURN(rc);
4506                 }
4507         } else {
4508                 /* If object isn't regular a file then don't validate size. */
4509                 if (S_ISDIR(inode->i_mode) &&
4510                     lli->lli_lsm_md != NULL) {
4511                         rc = ll_merge_md_attr(inode);
4512                         if (rc < 0)
4513                                 RETURN(rc);
4514                 }
4515
4516                 LTIME_S(inode->i_atime) = lli->lli_atime;
4517                 LTIME_S(inode->i_mtime) = lli->lli_mtime;
4518                 LTIME_S(inode->i_ctime) = lli->lli_ctime;
4519         }
4520
4521         OBD_FAIL_TIMEOUT(OBD_FAIL_GETATTR_DELAY, 30);
4522
4523         if (ll_need_32bit_api(sbi)) {
4524                 stat->ino = cl_fid_build_ino(&lli->lli_fid, 1);
4525                 stat->dev = ll_compat_encode_dev(inode->i_sb->s_dev);
4526                 stat->rdev = ll_compat_encode_dev(inode->i_rdev);
4527         } else {
4528                 stat->ino = inode->i_ino;
4529                 stat->dev = inode->i_sb->s_dev;
4530                 stat->rdev = inode->i_rdev;
4531         }
4532
4533         stat->mode = inode->i_mode;
4534         stat->uid = inode->i_uid;
4535         stat->gid = inode->i_gid;
4536         stat->atime = inode->i_atime;
4537         stat->mtime = inode->i_mtime;
4538         stat->ctime = inode->i_ctime;
4539         stat->blksize = sbi->ll_stat_blksize ?: 1 << inode->i_blkbits;
4540
4541         stat->nlink = inode->i_nlink;
4542         stat->size = i_size_read(inode);
4543         stat->blocks = inode->i_blocks;
4544
4545         return 0;
4546 }
4547
4548 static int ll_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4549                      __u64 start, __u64 len)
4550 {
4551         int             rc;
4552         size_t          num_bytes;
4553         struct fiemap   *fiemap;
4554         unsigned int    extent_count = fieinfo->fi_extents_max;
4555
4556         num_bytes = sizeof(*fiemap) + (extent_count *
4557                                        sizeof(struct fiemap_extent));
4558         OBD_ALLOC_LARGE(fiemap, num_bytes);
4559
4560         if (fiemap == NULL)
4561                 RETURN(-ENOMEM);
4562
4563         fiemap->fm_flags = fieinfo->fi_flags;
4564         fiemap->fm_extent_count = fieinfo->fi_extents_max;
4565         fiemap->fm_start = start;
4566         fiemap->fm_length = len;
4567         if (extent_count > 0 &&
4568             copy_from_user(&fiemap->fm_extents[0], fieinfo->fi_extents_start,
4569                            sizeof(struct fiemap_extent)) != 0)
4570                 GOTO(out, rc = -EFAULT);
4571
4572         rc = ll_do_fiemap(inode, fiemap, num_bytes);
4573
4574         fieinfo->fi_flags = fiemap->fm_flags;
4575         fieinfo->fi_extents_mapped = fiemap->fm_mapped_extents;
4576         if (extent_count > 0 &&
4577             copy_to_user(fieinfo->fi_extents_start, &fiemap->fm_extents[0],
4578                          fiemap->fm_mapped_extents *
4579                          sizeof(struct fiemap_extent)) != 0)
4580                 GOTO(out, rc = -EFAULT);
4581 out:
4582         OBD_FREE_LARGE(fiemap, num_bytes);
4583         return rc;
4584 }
4585
4586 struct posix_acl *ll_get_acl(struct inode *inode, int type)
4587 {
4588         struct ll_inode_info *lli = ll_i2info(inode);
4589         struct posix_acl *acl = NULL;
4590         ENTRY;
4591
4592         spin_lock(&lli->lli_lock);
4593         /* VFS' acl_permission_check->check_acl will release the refcount */
4594         acl = posix_acl_dup(lli->lli_posix_acl);
4595         spin_unlock(&lli->lli_lock);
4596
4597         RETURN(acl);
4598 }
4599
4600 #ifdef HAVE_IOP_SET_ACL
4601 #ifdef CONFIG_FS_POSIX_ACL
4602 int ll_set_acl(struct inode *inode, struct posix_acl *acl, int type)
4603 {
4604         struct ll_sb_info *sbi = ll_i2sbi(inode);
4605         struct ptlrpc_request *req = NULL;
4606         const char *name = NULL;
4607         char *value = NULL;
4608         size_t value_size = 0;
4609         int rc = 0;
4610         ENTRY;
4611
4612         switch (type) {
4613         case ACL_TYPE_ACCESS:
4614                 name = XATTR_NAME_POSIX_ACL_ACCESS;
4615                 if (acl)
4616                         rc = posix_acl_update_mode(inode, &inode->i_mode, &acl);
4617                 break;
4618
4619         case ACL_TYPE_DEFAULT:
4620                 name = XATTR_NAME_POSIX_ACL_DEFAULT;
4621                 if (!S_ISDIR(inode->i_mode))
4622                         rc = acl ? -EACCES : 0;
4623                 break;
4624
4625         default:
4626                 rc = -EINVAL;
4627                 break;
4628         }
4629         if (rc)
4630                 return rc;
4631
4632         if (acl) {
4633                 value_size = posix_acl_xattr_size(acl->a_count);
4634                 value = kmalloc(value_size, GFP_NOFS);
4635                 if (value == NULL)
4636                         GOTO(out, rc = -ENOMEM);
4637
4638                 rc = posix_acl_to_xattr(&init_user_ns, acl, value, value_size);
4639                 if (rc < 0)
4640                         GOTO(out_value, rc);
4641         }
4642
4643         rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode),
4644                          value ? OBD_MD_FLXATTR : OBD_MD_FLXATTRRM,
4645                          name, value, value_size, 0, 0, &req);
4646
4647         ptlrpc_req_finished(req);
4648 out_value:
4649         kfree(value);
4650 out:
4651         if (rc)
4652                 forget_cached_acl(inode, type);
4653         else
4654                 set_cached_acl(inode, type, acl);
4655         RETURN(rc);
4656 }
4657 #endif /* CONFIG_FS_POSIX_ACL */
4658 #endif /* HAVE_IOP_SET_ACL */
4659
4660 #ifndef HAVE_GENERIC_PERMISSION_2ARGS
4661 static int
4662 # ifdef HAVE_GENERIC_PERMISSION_4ARGS
4663 ll_check_acl(struct inode *inode, int mask, unsigned int flags)
4664 # else
4665 ll_check_acl(struct inode *inode, int mask)
4666 # endif
4667 {
4668 # ifdef CONFIG_FS_POSIX_ACL
4669         struct posix_acl *acl;
4670         int rc;
4671         ENTRY;
4672
4673 #  ifdef HAVE_GENERIC_PERMISSION_4ARGS
4674         if (flags & IPERM_FLAG_RCU)
4675                 return -ECHILD;
4676 #  endif
4677         acl = ll_get_acl(inode, ACL_TYPE_ACCESS);
4678
4679         if (!acl)
4680                 RETURN(-EAGAIN);
4681
4682         rc = posix_acl_permission(inode, acl, mask);
4683         posix_acl_release(acl);
4684
4685         RETURN(rc);
4686 # else /* !CONFIG_FS_POSIX_ACL */
4687         return -EAGAIN;
4688 # endif /* CONFIG_FS_POSIX_ACL */
4689 }
4690 #endif /* HAVE_GENERIC_PERMISSION_2ARGS */
4691
4692 #ifdef HAVE_GENERIC_PERMISSION_4ARGS
4693 int ll_inode_permission(struct inode *inode, int mask, unsigned int flags)
4694 #else
4695 # ifdef HAVE_INODE_PERMISION_2ARGS
4696 int ll_inode_permission(struct inode *inode, int mask)
4697 # else
4698 int ll_inode_permission(struct inode *inode, int mask, struct nameidata *nd)
4699 # endif
4700 #endif
4701 {
4702         int rc = 0;
4703         struct ll_sb_info *sbi;
4704         struct root_squash_info *squash;
4705         struct cred *cred = NULL;
4706         const struct cred *old_cred = NULL;
4707         cfs_cap_t cap;
4708         bool squash_id = false;
4709         ENTRY;
4710
4711 #ifdef MAY_NOT_BLOCK
4712         if (mask & MAY_NOT_BLOCK)
4713                 return -ECHILD;
4714 #elif defined(HAVE_GENERIC_PERMISSION_4ARGS)
4715         if (flags & IPERM_FLAG_RCU)
4716                 return -ECHILD;
4717 #endif
4718
4719        /* as root inode are NOT getting validated in lookup operation,
4720         * need to do it before permission check. */
4721
4722         if (inode == inode->i_sb->s_root->d_inode) {
4723                 rc = ll_inode_revalidate(inode->i_sb->s_root, IT_LOOKUP);
4724                 if (rc)
4725                         RETURN(rc);
4726         }
4727
4728         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), inode mode %x mask %o\n",
4729                PFID(ll_inode2fid(inode)), inode, inode->i_mode, mask);
4730
4731         /* squash fsuid/fsgid if needed */
4732         sbi = ll_i2sbi(inode);
4733         squash = &sbi->ll_squash;
4734         if (unlikely(squash->rsi_uid != 0 &&
4735                      uid_eq(current_fsuid(), GLOBAL_ROOT_UID) &&
4736                      !(sbi->ll_flags & LL_SBI_NOROOTSQUASH))) {
4737                         squash_id = true;
4738         }
4739         if (squash_id) {
4740                 CDEBUG(D_OTHER, "squash creds (%d:%d)=>(%d:%d)\n",
4741                        __kuid_val(current_fsuid()), __kgid_val(current_fsgid()),
4742                        squash->rsi_uid, squash->rsi_gid);
4743
4744                 /* update current process's credentials
4745                  * and FS capability */
4746                 cred = prepare_creds();
4747                 if (cred == NULL)
4748                         RETURN(-ENOMEM);
4749
4750                 cred->fsuid = make_kuid(&init_user_ns, squash->rsi_uid);
4751                 cred->fsgid = make_kgid(&init_user_ns, squash->rsi_gid);
4752                 for (cap = 0; cap < sizeof(cfs_cap_t) * 8; cap++) {
4753                         if ((1 << cap) & CFS_CAP_FS_MASK)
4754                                 cap_lower(cred->cap_effective, cap);
4755                 }
4756                 old_cred = override_creds(cred);
4757         }
4758
4759         ll_stats_ops_tally(sbi, LPROC_LL_INODE_PERM, 1);
4760         rc = ll_generic_permission(inode, mask, flags, ll_check_acl);
4761         /* restore current process's credentials and FS capability */
4762         if (squash_id) {
4763                 revert_creds(old_cred);
4764                 put_cred(cred);
4765         }
4766
4767         RETURN(rc);
4768 }
4769
4770 /* -o localflock - only provides locally consistent flock locks */
4771 struct file_operations ll_file_operations = {
4772 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
4773 # ifdef HAVE_SYNC_READ_WRITE
4774         .read           = new_sync_read,
4775         .write          = new_sync_write,
4776 # endif
4777         .read_iter      = ll_file_read_iter,
4778         .write_iter     = ll_file_write_iter,
4779 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
4780         .read           = ll_file_read,
4781         .aio_read       = ll_file_aio_read,
4782         .write          = ll_file_write,
4783         .aio_write      = ll_file_aio_write,
4784 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
4785         .unlocked_ioctl = ll_file_ioctl,
4786         .open           = ll_file_open,
4787         .release        = ll_file_release,
4788         .mmap           = ll_file_mmap,
4789         .llseek         = ll_file_seek,
4790         .splice_read    = ll_file_splice_read,
4791         .fsync          = ll_fsync,
4792         .flush          = ll_flush
4793 };
4794
4795 struct file_operations ll_file_operations_flock = {
4796 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
4797 # ifdef HAVE_SYNC_READ_WRITE
4798         .read           = new_sync_read,
4799         .write          = new_sync_write,
4800 # endif /* HAVE_SYNC_READ_WRITE */
4801         .read_iter      = ll_file_read_iter,
4802         .write_iter     = ll_file_write_iter,
4803 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
4804         .read           = ll_file_read,
4805         .aio_read       = ll_file_aio_read,
4806         .write          = ll_file_write,
4807         .aio_write      = ll_file_aio_write,
4808 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
4809         .unlocked_ioctl = ll_file_ioctl,
4810         .open           = ll_file_open,
4811         .release        = ll_file_release,
4812         .mmap           = ll_file_mmap,
4813         .llseek         = ll_file_seek,
4814         .splice_read    = ll_file_splice_read,
4815         .fsync          = ll_fsync,
4816         .flush          = ll_flush,
4817         .flock          = ll_file_flock,
4818         .lock           = ll_file_flock
4819 };
4820
4821 /* These are for -o noflock - to return ENOSYS on flock calls */
4822 struct file_operations ll_file_operations_noflock = {
4823 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
4824 # ifdef HAVE_SYNC_READ_WRITE
4825         .read           = new_sync_read,
4826         .write          = new_sync_write,
4827 # endif /* HAVE_SYNC_READ_WRITE */
4828         .read_iter      = ll_file_read_iter,
4829         .write_iter     = ll_file_write_iter,
4830 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
4831         .read           = ll_file_read,
4832         .aio_read       = ll_file_aio_read,
4833         .write          = ll_file_write,
4834         .aio_write      = ll_file_aio_write,
4835 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
4836         .unlocked_ioctl = ll_file_ioctl,
4837         .open           = ll_file_open,
4838         .release        = ll_file_release,
4839         .mmap           = ll_file_mmap,
4840         .llseek         = ll_file_seek,
4841         .splice_read    = ll_file_splice_read,
4842         .fsync          = ll_fsync,
4843         .flush          = ll_flush,
4844         .flock          = ll_file_noflock,
4845         .lock           = ll_file_noflock
4846 };
4847
4848 struct inode_operations ll_file_inode_operations = {
4849         .setattr        = ll_setattr,
4850         .getattr        = ll_getattr,
4851         .permission     = ll_inode_permission,
4852 #ifdef HAVE_IOP_XATTR
4853         .setxattr       = ll_setxattr,
4854         .getxattr       = ll_getxattr,
4855         .removexattr    = ll_removexattr,
4856 #endif
4857         .listxattr      = ll_listxattr,
4858         .fiemap         = ll_fiemap,
4859 #ifdef HAVE_IOP_GET_ACL
4860         .get_acl        = ll_get_acl,
4861 #endif
4862 #ifdef HAVE_IOP_SET_ACL
4863         .set_acl        = ll_set_acl,
4864 #endif
4865 };
4866
4867 int ll_layout_conf(struct inode *inode, const struct cl_object_conf *conf)
4868 {
4869         struct ll_inode_info *lli = ll_i2info(inode);
4870         struct cl_object *obj = lli->lli_clob;
4871         struct lu_env *env;
4872         int rc;
4873         __u16 refcheck;
4874         ENTRY;
4875
4876         if (obj == NULL)
4877                 RETURN(0);
4878
4879         env = cl_env_get(&refcheck);
4880         if (IS_ERR(env))
4881                 RETURN(PTR_ERR(env));
4882
4883         rc = cl_conf_set(env, lli->lli_clob, conf);
4884         if (rc < 0)
4885                 GOTO(out, rc);
4886
4887         if (conf->coc_opc == OBJECT_CONF_SET) {
4888                 struct ldlm_lock *lock = conf->coc_lock;
4889                 struct cl_layout cl = {
4890                         .cl_layout_gen = 0,
4891                 };
4892
4893                 LASSERT(lock != NULL);
4894                 LASSERT(ldlm_has_layout(lock));
4895
4896                 /* it can only be allowed to match after layout is
4897                  * applied to inode otherwise false layout would be
4898                  * seen. Applying layout shoud happen before dropping
4899                  * the intent lock. */
4900                 ldlm_lock_allow_match(lock);
4901
4902                 rc = cl_object_layout_get(env, obj, &cl);
4903                 if (rc < 0)
4904                         GOTO(out, rc);
4905
4906                 CDEBUG(D_VFSTRACE,
4907                        DFID": layout version change: %u -> %u\n",
4908                        PFID(&lli->lli_fid), ll_layout_version_get(lli),
4909                        cl.cl_layout_gen);
4910                 ll_layout_version_set(lli, cl.cl_layout_gen);
4911         }
4912
4913 out:
4914         cl_env_put(env, &refcheck);
4915
4916         RETURN(rc);
4917 }
4918
4919 /* Fetch layout from MDT with getxattr request, if it's not ready yet */
4920 static int ll_layout_fetch(struct inode *inode, struct ldlm_lock *lock)
4921
4922 {
4923         struct ll_sb_info *sbi = ll_i2sbi(inode);
4924         struct ptlrpc_request *req;
4925         struct mdt_body *body;
4926         void *lvbdata;
4927         void *lmm;
4928         int lmmsize;
4929         int rc;
4930         ENTRY;
4931
4932         CDEBUG(D_INODE, DFID" LVB_READY=%d l_lvb_data=%p l_lvb_len=%d\n",
4933                PFID(ll_inode2fid(inode)), ldlm_is_lvb_ready(lock),
4934                lock->l_lvb_data, lock->l_lvb_len);
4935
4936         if (lock->l_lvb_data != NULL)
4937                 RETURN(0);
4938
4939         /* if layout lock was granted right away, the layout is returned
4940          * within DLM_LVB of dlm reply; otherwise if the lock was ever
4941          * blocked and then granted via completion ast, we have to fetch
4942          * layout here. Please note that we can't use the LVB buffer in
4943          * completion AST because it doesn't have a large enough buffer */
4944         rc = ll_get_default_mdsize(sbi, &lmmsize);
4945         if (rc == 0)
4946                 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode),
4947                                 OBD_MD_FLXATTR, XATTR_NAME_LOV, lmmsize, &req);
4948         if (rc < 0)
4949                 RETURN(rc);
4950
4951         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
4952         if (body == NULL)
4953                 GOTO(out, rc = -EPROTO);
4954
4955         lmmsize = body->mbo_eadatasize;
4956         if (lmmsize == 0) /* empty layout */
4957                 GOTO(out, rc = 0);
4958
4959         lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA, lmmsize);
4960         if (lmm == NULL)
4961                 GOTO(out, rc = -EFAULT);
4962
4963         OBD_ALLOC_LARGE(lvbdata, lmmsize);
4964         if (lvbdata == NULL)
4965                 GOTO(out, rc = -ENOMEM);
4966
4967         memcpy(lvbdata, lmm, lmmsize);
4968         lock_res_and_lock(lock);
4969         if (unlikely(lock->l_lvb_data == NULL)) {
4970                 lock->l_lvb_type = LVB_T_LAYOUT;
4971                 lock->l_lvb_data = lvbdata;
4972                 lock->l_lvb_len = lmmsize;
4973                 lvbdata = NULL;
4974         }
4975         unlock_res_and_lock(lock);
4976
4977         if (lvbdata)
4978                 OBD_FREE_LARGE(lvbdata, lmmsize);
4979
4980         EXIT;
4981
4982 out:
4983         ptlrpc_req_finished(req);
4984         return rc;
4985 }
4986
4987 /**
4988  * Apply the layout to the inode. Layout lock is held and will be released
4989  * in this function.
4990  */
4991 static int ll_layout_lock_set(struct lustre_handle *lockh, enum ldlm_mode mode,
4992                               struct inode *inode)
4993 {
4994         struct ll_inode_info *lli = ll_i2info(inode);
4995         struct ll_sb_info    *sbi = ll_i2sbi(inode);
4996         struct ldlm_lock *lock;
4997         struct cl_object_conf conf;
4998         int rc = 0;
4999         bool lvb_ready;
5000         bool wait_layout = false;
5001         ENTRY;
5002
5003         LASSERT(lustre_handle_is_used(lockh));
5004
5005         lock = ldlm_handle2lock(lockh);
5006         LASSERT(lock != NULL);
5007         LASSERT(ldlm_has_layout(lock));
5008
5009         LDLM_DEBUG(lock, "file "DFID"(%p) being reconfigured",
5010                    PFID(&lli->lli_fid), inode);
5011
5012         /* in case this is a caching lock and reinstate with new inode */
5013         md_set_lock_data(sbi->ll_md_exp, lockh, inode, NULL);
5014
5015         lock_res_and_lock(lock);
5016         lvb_ready = ldlm_is_lvb_ready(lock);
5017         unlock_res_and_lock(lock);
5018
5019         /* checking lvb_ready is racy but this is okay. The worst case is
5020          * that multi processes may configure the file on the same time. */
5021         if (lvb_ready)
5022                 GOTO(out, rc = 0);
5023
5024         rc = ll_layout_fetch(inode, lock);
5025         if (rc < 0)
5026                 GOTO(out, rc);
5027
5028         /* for layout lock, lmm is stored in lock's lvb.
5029          * lvb_data is immutable if the lock is held so it's safe to access it
5030          * without res lock.
5031          *
5032          * set layout to file. Unlikely this will fail as old layout was
5033          * surely eliminated */
5034         memset(&conf, 0, sizeof conf);
5035         conf.coc_opc = OBJECT_CONF_SET;
5036         conf.coc_inode = inode;
5037         conf.coc_lock = lock;
5038         conf.u.coc_layout.lb_buf = lock->l_lvb_data;
5039         conf.u.coc_layout.lb_len = lock->l_lvb_len;
5040         rc = ll_layout_conf(inode, &conf);
5041
5042         /* refresh layout failed, need to wait */
5043         wait_layout = rc == -EBUSY;
5044         EXIT;
5045 out:
5046         LDLM_LOCK_PUT(lock);
5047         ldlm_lock_decref(lockh, mode);
5048
5049         /* wait for IO to complete if it's still being used. */
5050         if (wait_layout) {
5051                 CDEBUG(D_INODE, "%s: "DFID"(%p) wait for layout reconf\n",
5052                        ll_get_fsname(inode->i_sb, NULL, 0),
5053                        PFID(&lli->lli_fid), inode);
5054
5055                 memset(&conf, 0, sizeof conf);
5056                 conf.coc_opc = OBJECT_CONF_WAIT;
5057                 conf.coc_inode = inode;
5058                 rc = ll_layout_conf(inode, &conf);
5059                 if (rc == 0)
5060                         rc = -EAGAIN;
5061
5062                 CDEBUG(D_INODE, "%s file="DFID" waiting layout return: %d\n",
5063                        ll_get_fsname(inode->i_sb, NULL, 0),
5064                        PFID(&lli->lli_fid), rc);
5065         }
5066         RETURN(rc);
5067 }
5068
5069 /**
5070  * Issue layout intent RPC to MDS.
5071  * \param inode [in]    file inode
5072  * \param intent [in]   layout intent
5073  *
5074  * \retval 0    on success
5075  * \retval < 0  error code
5076  */
5077 static int ll_layout_intent(struct inode *inode, struct layout_intent *intent)
5078 {
5079         struct ll_inode_info  *lli = ll_i2info(inode);
5080         struct ll_sb_info     *sbi = ll_i2sbi(inode);
5081         struct md_op_data     *op_data;
5082         struct lookup_intent it;
5083         struct ptlrpc_request *req;
5084         int rc;
5085         ENTRY;
5086
5087         op_data = ll_prep_md_op_data(NULL, inode, inode, NULL,
5088                                      0, 0, LUSTRE_OPC_ANY, NULL);
5089         if (IS_ERR(op_data))
5090                 RETURN(PTR_ERR(op_data));
5091
5092         op_data->op_data = intent;
5093         op_data->op_data_size = sizeof(*intent);
5094
5095         memset(&it, 0, sizeof(it));
5096         it.it_op = IT_LAYOUT;
5097         if (intent->li_opc == LAYOUT_INTENT_WRITE ||
5098             intent->li_opc == LAYOUT_INTENT_TRUNC)
5099                 it.it_flags = FMODE_WRITE;
5100
5101         LDLM_DEBUG_NOLOCK("%s: requeue layout lock for file "DFID"(%p)",
5102                           ll_get_fsname(inode->i_sb, NULL, 0),
5103                           PFID(&lli->lli_fid), inode);
5104
5105         rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
5106                             &ll_md_blocking_ast, 0);
5107         if (it.it_request != NULL)
5108                 ptlrpc_req_finished(it.it_request);
5109         it.it_request = NULL;
5110
5111         ll_finish_md_op_data(op_data);
5112
5113         /* set lock data in case this is a new lock */
5114         if (!rc)
5115                 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
5116
5117         ll_intent_drop_lock(&it);
5118
5119         RETURN(rc);
5120 }
5121
5122 /**
5123  * This function checks if there exists a LAYOUT lock on the client side,
5124  * or enqueues it if it doesn't have one in cache.
5125  *
5126  * This function will not hold layout lock so it may be revoked any time after
5127  * this function returns. Any operations depend on layout should be redone
5128  * in that case.
5129  *
5130  * This function should be called before lov_io_init() to get an uptodate
5131  * layout version, the caller should save the version number and after IO
5132  * is finished, this function should be called again to verify that layout
5133  * is not changed during IO time.
5134  */
5135 int ll_layout_refresh(struct inode *inode, __u32 *gen)
5136 {
5137         struct ll_inode_info    *lli = ll_i2info(inode);
5138         struct ll_sb_info       *sbi = ll_i2sbi(inode);
5139         struct lustre_handle lockh;
5140         struct layout_intent intent = {
5141                 .li_opc = LAYOUT_INTENT_ACCESS,
5142         };
5143         enum ldlm_mode mode;
5144         int rc;
5145         ENTRY;
5146
5147         *gen = ll_layout_version_get(lli);
5148         if (!(sbi->ll_flags & LL_SBI_LAYOUT_LOCK) || *gen != CL_LAYOUT_GEN_NONE)
5149                 RETURN(0);
5150
5151         /* sanity checks */
5152         LASSERT(fid_is_sane(ll_inode2fid(inode)));
5153         LASSERT(S_ISREG(inode->i_mode));
5154
5155         /* take layout lock mutex to enqueue layout lock exclusively. */
5156         mutex_lock(&lli->lli_layout_mutex);
5157
5158         while (1) {
5159                 /* mostly layout lock is caching on the local side, so try to
5160                  * match it before grabbing layout lock mutex. */
5161                 mode = ll_take_md_lock(inode, MDS_INODELOCK_LAYOUT, &lockh, 0,
5162                                        LCK_CR | LCK_CW | LCK_PR | LCK_PW);
5163                 if (mode != 0) { /* hit cached lock */
5164                         rc = ll_layout_lock_set(&lockh, mode, inode);
5165                         if (rc == -EAGAIN)
5166                                 continue;
5167                         break;
5168                 }
5169
5170                 rc = ll_layout_intent(inode, &intent);
5171                 if (rc != 0)
5172                         break;
5173         }
5174
5175         if (rc == 0)
5176                 *gen = ll_layout_version_get(lli);
5177         mutex_unlock(&lli->lli_layout_mutex);
5178
5179         RETURN(rc);
5180 }
5181
5182 /**
5183  * Issue layout intent RPC indicating where in a file an IO is about to write.
5184  *
5185  * \param[in] inode     file inode.
5186  * \param[in] ext       write range with start offset of fille in bytes where
5187  *                      an IO is about to write, and exclusive end offset in
5188  *                      bytes.
5189  *
5190  * \retval 0    on success
5191  * \retval < 0  error code
5192  */
5193 int ll_layout_write_intent(struct inode *inode, enum layout_intent_opc opc,
5194                            struct lu_extent *ext)
5195 {
5196         struct layout_intent intent = {
5197                 .li_opc = opc,
5198                 .li_extent.e_start = ext->e_start,
5199                 .li_extent.e_end = ext->e_end,
5200         };
5201         int rc;
5202         ENTRY;
5203
5204         rc = ll_layout_intent(inode, &intent);
5205
5206         RETURN(rc);
5207 }
5208
5209 /**
5210  *  This function send a restore request to the MDT
5211  */
5212 int ll_layout_restore(struct inode *inode, loff_t offset, __u64 length)
5213 {
5214         struct hsm_user_request *hur;
5215         int                      len, rc;
5216         ENTRY;
5217
5218         len = sizeof(struct hsm_user_request) +
5219               sizeof(struct hsm_user_item);
5220         OBD_ALLOC(hur, len);
5221         if (hur == NULL)
5222                 RETURN(-ENOMEM);
5223
5224         hur->hur_request.hr_action = HUA_RESTORE;
5225         hur->hur_request.hr_archive_id = 0;
5226         hur->hur_request.hr_flags = 0;
5227         memcpy(&hur->hur_user_item[0].hui_fid, &ll_i2info(inode)->lli_fid,
5228                sizeof(hur->hur_user_item[0].hui_fid));
5229         hur->hur_user_item[0].hui_extent.offset = offset;
5230         hur->hur_user_item[0].hui_extent.length = length;
5231         hur->hur_request.hr_itemcount = 1;
5232         rc = obd_iocontrol(LL_IOC_HSM_REQUEST, ll_i2sbi(inode)->ll_md_exp,
5233                            len, hur, NULL);
5234         OBD_FREE(hur, len);
5235         RETURN(rc);
5236 }