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