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