Whamcloud - gitweb
LU-220 open: try open_by_fid for the known object
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/llite/file.c
37  *
38  * Author: Peter Braam <braam@clusterfs.com>
39  * Author: Phil Schwan <phil@clusterfs.com>
40  * Author: Andreas Dilger <adilger@clusterfs.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_LLITE
44 #include <lustre_dlm.h>
45 #include <lustre_lite.h>
46 #include <linux/pagemap.h>
47 #include <linux/file.h>
48 #include "llite_internal.h"
49 #include <lustre/ll_fiemap.h>
50
51 #include "cl_object.h"
52
53 struct ll_file_data *ll_file_data_get(void)
54 {
55         struct ll_file_data *fd;
56
57         OBD_SLAB_ALLOC_PTR_GFP(fd, ll_file_data_slab, CFS_ALLOC_IO);
58         return fd;
59 }
60
61 static void ll_file_data_put(struct ll_file_data *fd)
62 {
63         if (fd != NULL)
64                 OBD_SLAB_FREE_PTR(fd, ll_file_data_slab);
65 }
66
67 void ll_pack_inode2opdata(struct inode *inode, struct md_op_data *op_data,
68                           struct lustre_handle *fh)
69 {
70         op_data->op_fid1 = ll_i2info(inode)->lli_fid;
71         op_data->op_attr.ia_mode = inode->i_mode;
72         op_data->op_attr.ia_atime = inode->i_atime;
73         op_data->op_attr.ia_mtime = inode->i_mtime;
74         op_data->op_attr.ia_ctime = inode->i_ctime;
75         op_data->op_attr.ia_size = i_size_read(inode);
76         op_data->op_attr_blocks = inode->i_blocks;
77         ((struct ll_iattr *)&op_data->op_attr)->ia_attr_flags =
78                                         ll_inode_to_ext_flags(inode->i_flags);
79         op_data->op_ioepoch = ll_i2info(inode)->lli_ioepoch;
80         if (fh)
81                 op_data->op_handle = *fh;
82         op_data->op_capa1 = ll_mdscapa_get(inode);
83 }
84
85 /**
86  * Closes the IO epoch and packs all the attributes into @op_data for
87  * the CLOSE rpc.
88  */
89 static void ll_prepare_close(struct inode *inode, struct md_op_data *op_data,
90                              struct obd_client_handle *och)
91 {
92         ENTRY;
93
94         op_data->op_attr.ia_valid = ATTR_MODE | ATTR_ATIME_SET |
95                                  ATTR_MTIME_SET | ATTR_CTIME_SET;
96
97         if (!(och->och_flags & FMODE_WRITE))
98                 goto out;
99
100         if (!exp_connect_som(ll_i2mdexp(inode)) || !S_ISREG(inode->i_mode))
101                 op_data->op_attr.ia_valid |= ATTR_SIZE | ATTR_BLOCKS;
102         else
103                 ll_ioepoch_close(inode, op_data, &och, 0);
104
105 out:
106         ll_pack_inode2opdata(inode, op_data, &och->och_fh);
107         ll_prep_md_op_data(op_data, inode, NULL, NULL,
108                            0, 0, LUSTRE_OPC_ANY, NULL);
109         EXIT;
110 }
111
112 static int ll_close_inode_openhandle(struct obd_export *md_exp,
113                                      struct inode *inode,
114                                      struct obd_client_handle *och)
115 {
116         struct obd_export *exp = ll_i2mdexp(inode);
117         struct md_op_data *op_data;
118         struct ptlrpc_request *req = NULL;
119         struct obd_device *obd = class_exp2obd(exp);
120         int epoch_close = 1;
121         int rc;
122         ENTRY;
123
124         if (obd == NULL) {
125                 /*
126                  * XXX: in case of LMV, is this correct to access
127                  * ->exp_handle?
128                  */
129                 CERROR("Invalid MDC connection handle "LPX64"\n",
130                        ll_i2mdexp(inode)->exp_handle.h_cookie);
131                 GOTO(out, rc = 0);
132         }
133
134         OBD_ALLOC_PTR(op_data);
135         if (op_data == NULL)
136                 GOTO(out, rc = -ENOMEM); // XXX We leak openhandle and request here.
137
138         ll_prepare_close(inode, op_data, och);
139         epoch_close = (op_data->op_flags & MF_EPOCH_CLOSE);
140         rc = md_close(md_exp, op_data, och->och_mod, &req);
141         if (rc == -EAGAIN) {
142                 /* This close must have the epoch closed. */
143                 LASSERT(epoch_close);
144                 /* MDS has instructed us to obtain Size-on-MDS attribute from
145                  * OSTs and send setattr to back to MDS. */
146                 rc = ll_som_update(inode, op_data);
147                 if (rc) {
148                         CERROR("inode %lu mdc Size-on-MDS update failed: "
149                                "rc = %d\n", inode->i_ino, rc);
150                         rc = 0;
151                 }
152         } else if (rc) {
153                 CERROR("inode %lu mdc close failed: rc = %d\n",
154                        inode->i_ino, rc);
155         }
156         ll_finish_md_op_data(op_data);
157
158         if (rc == 0) {
159                 rc = ll_objects_destroy(req, inode);
160                 if (rc)
161                         CERROR("inode %lu ll_objects destroy: rc = %d\n",
162                                inode->i_ino, rc);
163         }
164
165         EXIT;
166 out:
167
168         if (exp_connect_som(exp) && !epoch_close &&
169             S_ISREG(inode->i_mode) && (och->och_flags & FMODE_WRITE)) {
170                 ll_queue_done_writing(inode, LLIF_DONE_WRITING);
171         } else {
172                 md_clear_open_replay_data(md_exp, och);
173                 /* Free @och if it is not waiting for DONE_WRITING. */
174                 och->och_fh.cookie = DEAD_HANDLE_MAGIC;
175                 OBD_FREE_PTR(och);
176         }
177         if (req) /* This is close request */
178                 ptlrpc_req_finished(req);
179         return rc;
180 }
181
182 int ll_md_real_close(struct inode *inode, int flags)
183 {
184         struct ll_inode_info *lli = ll_i2info(inode);
185         struct obd_client_handle **och_p;
186         struct obd_client_handle *och;
187         __u64 *och_usecount;
188         int rc = 0;
189         ENTRY;
190
191         if (flags & FMODE_WRITE) {
192                 och_p = &lli->lli_mds_write_och;
193                 och_usecount = &lli->lli_open_fd_write_count;
194         } else if (flags & FMODE_EXEC) {
195                 och_p = &lli->lli_mds_exec_och;
196                 och_usecount = &lli->lli_open_fd_exec_count;
197         } else {
198                 LASSERT(flags & FMODE_READ);
199                 och_p = &lli->lli_mds_read_och;
200                 och_usecount = &lli->lli_open_fd_read_count;
201         }
202
203         cfs_mutex_lock(&lli->lli_och_mutex);
204         if (*och_usecount) { /* There are still users of this handle, so
205                                 skip freeing it. */
206                 cfs_mutex_unlock(&lli->lli_och_mutex);
207                 RETURN(0);
208         }
209         och=*och_p;
210         *och_p = NULL;
211         cfs_mutex_unlock(&lli->lli_och_mutex);
212
213         if (och) { /* There might be a race and somebody have freed this och
214                       already */
215                 rc = ll_close_inode_openhandle(ll_i2sbi(inode)->ll_md_exp,
216                                                inode, och);
217         }
218
219         RETURN(rc);
220 }
221
222 int ll_md_close(struct obd_export *md_exp, struct inode *inode,
223                 struct file *file)
224 {
225         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
226         struct ll_inode_info *lli = ll_i2info(inode);
227         int rc = 0;
228         ENTRY;
229
230         /* clear group lock, if present */
231         if (unlikely(fd->fd_flags & LL_FILE_GROUP_LOCKED))
232                 ll_put_grouplock(inode, file, fd->fd_grouplock.cg_gid);
233
234         /* Let's see if we have good enough OPEN lock on the file and if
235            we can skip talking to MDS */
236         if (file->f_dentry->d_inode) { /* Can this ever be false? */
237                 int lockmode;
238                 int flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_TEST_LOCK;
239                 struct lustre_handle lockh;
240                 struct inode *inode = file->f_dentry->d_inode;
241                 ldlm_policy_data_t policy = {.l_inodebits={MDS_INODELOCK_OPEN}};
242
243                 cfs_mutex_lock(&lli->lli_och_mutex);
244                 if (fd->fd_omode & FMODE_WRITE) {
245                         lockmode = LCK_CW;
246                         LASSERT(lli->lli_open_fd_write_count);
247                         lli->lli_open_fd_write_count--;
248                 } else if (fd->fd_omode & FMODE_EXEC) {
249                         lockmode = LCK_PR;
250                         LASSERT(lli->lli_open_fd_exec_count);
251                         lli->lli_open_fd_exec_count--;
252                 } else {
253                         lockmode = LCK_CR;
254                         LASSERT(lli->lli_open_fd_read_count);
255                         lli->lli_open_fd_read_count--;
256                 }
257                 cfs_mutex_unlock(&lli->lli_och_mutex);
258
259                 if (!md_lock_match(md_exp, flags, ll_inode2fid(inode),
260                                    LDLM_IBITS, &policy, lockmode,
261                                    &lockh)) {
262                         rc = ll_md_real_close(file->f_dentry->d_inode,
263                                               fd->fd_omode);
264                 }
265         } else {
266                 CERROR("Releasing a file %p with negative dentry %p. Name %s",
267                        file, file->f_dentry, file->f_dentry->d_name.name);
268         }
269
270         LUSTRE_FPRIVATE(file) = NULL;
271         ll_file_data_put(fd);
272         ll_capa_close(inode);
273
274         RETURN(rc);
275 }
276
277 /* While this returns an error code, fput() the caller does not, so we need
278  * to make every effort to clean up all of our state here.  Also, applications
279  * rarely check close errors and even if an error is returned they will not
280  * re-try the close call.
281  */
282 int ll_file_release(struct inode *inode, struct file *file)
283 {
284         struct ll_file_data *fd;
285         struct ll_sb_info *sbi = ll_i2sbi(inode);
286         struct ll_inode_info *lli = ll_i2info(inode);
287         int rc;
288         ENTRY;
289
290         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
291                inode->i_generation, inode);
292
293 #ifdef CONFIG_FS_POSIX_ACL
294         if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
295             inode == inode->i_sb->s_root->d_inode) {
296                 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
297
298                 LASSERT(fd != NULL);
299                 if (unlikely(fd->fd_flags & LL_FILE_RMTACL)) {
300                         fd->fd_flags &= ~LL_FILE_RMTACL;
301                         rct_del(&sbi->ll_rct, cfs_curproc_pid());
302                         et_search_free(&sbi->ll_et, cfs_curproc_pid());
303                 }
304         }
305 #endif
306
307         if (inode->i_sb->s_root != file->f_dentry)
308                 ll_stats_ops_tally(sbi, LPROC_LL_RELEASE, 1);
309         fd = LUSTRE_FPRIVATE(file);
310         LASSERT(fd != NULL);
311
312         /* The last ref on @file, maybe not the the owner pid of statahead.
313          * Different processes can open the same dir, "ll_opendir_key" means:
314          * it is me that should stop the statahead thread. */
315         if (S_ISDIR(inode->i_mode) && lli->lli_opendir_key == fd &&
316             lli->lli_opendir_pid != 0)
317                 ll_stop_statahead(inode, lli->lli_opendir_key);
318
319         if (inode->i_sb->s_root == file->f_dentry) {
320                 LUSTRE_FPRIVATE(file) = NULL;
321                 ll_file_data_put(fd);
322                 RETURN(0);
323         }
324
325         if (!S_ISDIR(inode->i_mode)) {
326                 lov_read_and_clear_async_rc(lli->lli_clob);
327                 lli->lli_async_rc = 0;
328         }
329
330         rc = ll_md_close(sbi->ll_md_exp, inode, file);
331
332         if (CFS_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_DUMP_LOG, cfs_fail_val))
333                 libcfs_debug_dumplog();
334
335         RETURN(rc);
336 }
337
338 static int ll_intent_file_open(struct file *file, void *lmm,
339                                int lmmsize, struct lookup_intent *itp)
340 {
341         struct ll_sb_info *sbi = ll_i2sbi(file->f_dentry->d_inode);
342         struct dentry *parent = file->f_dentry->d_parent;
343         const char *name = file->f_dentry->d_name.name;
344         const int len = file->f_dentry->d_name.len;
345         struct md_op_data *op_data;
346         struct ptlrpc_request *req;
347         __u32 opc = LUSTRE_OPC_ANY;
348         int rc;
349         ENTRY;
350
351         if (!parent)
352                 RETURN(-ENOENT);
353
354         /* Usually we come here only for NFSD, and we want open lock.
355            But we can also get here with pre 2.6.15 patchless kernels, and in
356            that case that lock is also ok */
357         /* We can also get here if there was cached open handle in revalidate_it
358          * but it disappeared while we were getting from there to ll_file_open.
359          * But this means this file was closed and immediatelly opened which
360          * makes a good candidate for using OPEN lock */
361         /* If lmmsize & lmm are not 0, we are just setting stripe info
362          * parameters. No need for the open lock */
363         if (lmm == NULL && lmmsize == 0) {
364                 itp->it_flags |= MDS_OPEN_LOCK;
365                 if (itp->it_flags & FMODE_WRITE)
366                         opc = LUSTRE_OPC_CREATE;
367         }
368
369         op_data  = ll_prep_md_op_data(NULL, parent->d_inode,
370                                       file->f_dentry->d_inode, name, len,
371                                       O_RDWR, opc, NULL);
372         if (IS_ERR(op_data))
373                 RETURN(PTR_ERR(op_data));
374
375         itp->it_flags |= MDS_OPEN_BY_FID;
376         rc = md_intent_lock(sbi->ll_md_exp, op_data, lmm, lmmsize, itp,
377                             0 /*unused */, &req, ll_md_blocking_ast, 0);
378         ll_finish_md_op_data(op_data);
379         if (rc == -ESTALE) {
380                 /* reason for keep own exit path - don`t flood log
381                 * with messages with -ESTALE errors.
382                 */
383                 if (!it_disposition(itp, DISP_OPEN_OPEN) ||
384                      it_open_error(DISP_OPEN_OPEN, itp))
385                         GOTO(out, rc);
386                 ll_release_openhandle(file->f_dentry, itp);
387                 GOTO(out, rc);
388         }
389
390         if (it_disposition(itp, DISP_LOOKUP_NEG))
391                 GOTO(out, rc = -ENOENT);
392
393         if (rc != 0 || it_open_error(DISP_OPEN_OPEN, itp)) {
394                 rc = rc ? rc : it_open_error(DISP_OPEN_OPEN, itp);
395                 CDEBUG(D_VFSTRACE, "lock enqueue: err: %d\n", rc);
396                 GOTO(out, rc);
397         }
398
399         rc = ll_prep_inode(&file->f_dentry->d_inode, req, NULL);
400         if (!rc && itp->d.lustre.it_lock_mode)
401                 ll_set_lock_data(sbi->ll_md_exp, file->f_dentry->d_inode,
402                                  itp, NULL);
403
404 out:
405         ptlrpc_req_finished(itp->d.lustre.it_data);
406         it_clear_disposition(itp, DISP_ENQ_COMPLETE);
407         ll_intent_drop_lock(itp);
408
409         RETURN(rc);
410 }
411
412 /**
413  * Assign an obtained @ioepoch to client's inode. No lock is needed, MDS does
414  * not believe attributes if a few ioepoch holders exist. Attributes for
415  * previous ioepoch if new one is opened are also skipped by MDS.
416  */
417 void ll_ioepoch_open(struct ll_inode_info *lli, __u64 ioepoch)
418 {
419         if (ioepoch && lli->lli_ioepoch != ioepoch) {
420                 lli->lli_ioepoch = ioepoch;
421                 CDEBUG(D_INODE, "Epoch "LPU64" opened on "DFID"\n",
422                        ioepoch, PFID(&lli->lli_fid));
423         }
424 }
425
426 static int ll_och_fill(struct obd_export *md_exp, struct ll_inode_info *lli,
427                        struct lookup_intent *it, struct obd_client_handle *och)
428 {
429         struct ptlrpc_request *req = it->d.lustre.it_data;
430         struct mdt_body *body;
431
432         LASSERT(och);
433
434         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
435         LASSERT(body != NULL);                      /* reply already checked out */
436
437         memcpy(&och->och_fh, &body->handle, sizeof(body->handle));
438         och->och_magic = OBD_CLIENT_HANDLE_MAGIC;
439         och->och_fid = lli->lli_fid;
440         och->och_flags = it->it_flags;
441         ll_ioepoch_open(lli, body->ioepoch);
442
443         return md_set_open_replay_data(md_exp, och, req);
444 }
445
446 int ll_local_open(struct file *file, struct lookup_intent *it,
447                   struct ll_file_data *fd, struct obd_client_handle *och)
448 {
449         struct inode *inode = file->f_dentry->d_inode;
450         struct ll_inode_info *lli = ll_i2info(inode);
451         ENTRY;
452
453         LASSERT(!LUSTRE_FPRIVATE(file));
454
455         LASSERT(fd != NULL);
456
457         if (och) {
458                 struct ptlrpc_request *req = it->d.lustre.it_data;
459                 struct mdt_body *body;
460                 int rc;
461
462                 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, lli, it, och);
463                 if (rc)
464                         RETURN(rc);
465
466                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
467                 if ((it->it_flags & FMODE_WRITE) &&
468                     (body->valid & OBD_MD_FLSIZE))
469                         CDEBUG(D_INODE, "Epoch "LPU64" opened on "DFID"\n",
470                                lli->lli_ioepoch, PFID(&lli->lli_fid));
471         }
472
473         LUSTRE_FPRIVATE(file) = fd;
474         ll_readahead_init(inode, &fd->fd_ras);
475         fd->fd_omode = it->it_flags;
476         RETURN(0);
477 }
478
479 /* Open a file, and (for the very first open) create objects on the OSTs at
480  * this time.  If opened with O_LOV_DELAY_CREATE, then we don't do the object
481  * creation or open until ll_lov_setstripe() ioctl is called.
482  *
483  * If we already have the stripe MD locally then we don't request it in
484  * md_open(), by passing a lmm_size = 0.
485  *
486  * It is up to the application to ensure no other processes open this file
487  * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
488  * used.  We might be able to avoid races of that sort by getting lli_open_sem
489  * before returning in the O_LOV_DELAY_CREATE case and dropping it here
490  * or in ll_file_release(), but I'm not sure that is desirable/necessary.
491  */
492 int ll_file_open(struct inode *inode, struct file *file)
493 {
494         struct ll_inode_info *lli = ll_i2info(inode);
495         struct lookup_intent *it, oit = { .it_op = IT_OPEN,
496                                           .it_flags = file->f_flags };
497         struct obd_client_handle **och_p = NULL;
498         __u64 *och_usecount = NULL;
499         struct ll_file_data *fd;
500         int rc = 0, opendir_set = 0;
501         ENTRY;
502
503         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), flags %o\n", inode->i_ino,
504                inode->i_generation, inode, file->f_flags);
505
506         it = file->private_data; /* XXX: compat macro */
507         file->private_data = NULL; /* prevent ll_local_open assertion */
508
509         fd = ll_file_data_get();
510         if (fd == NULL)
511                 GOTO(out_och_free, rc = -ENOMEM);
512
513         fd->fd_file = file;
514         if (S_ISDIR(inode->i_mode)) {
515                 cfs_spin_lock(&lli->lli_sa_lock);
516                 if (lli->lli_opendir_key == NULL && lli->lli_sai == NULL &&
517                     lli->lli_opendir_pid == 0) {
518                         lli->lli_opendir_key = fd;
519                         lli->lli_opendir_pid = cfs_curproc_pid();
520                         opendir_set = 1;
521                 }
522                 cfs_spin_unlock(&lli->lli_sa_lock);
523         }
524
525         if (inode->i_sb->s_root == file->f_dentry) {
526                 LUSTRE_FPRIVATE(file) = fd;
527                 RETURN(0);
528         }
529
530         if (!it || !it->d.lustre.it_disposition) {
531                 /* Convert f_flags into access mode. We cannot use file->f_mode,
532                  * because everything but O_ACCMODE mask was stripped from
533                  * there */
534                 if ((oit.it_flags + 1) & O_ACCMODE)
535                         oit.it_flags++;
536                 if (file->f_flags & O_TRUNC)
537                         oit.it_flags |= FMODE_WRITE;
538
539                 /* kernel only call f_op->open in dentry_open.  filp_open calls
540                  * dentry_open after call to open_namei that checks permissions.
541                  * Only nfsd_open call dentry_open directly without checking
542                  * permissions and because of that this code below is safe. */
543                 if (oit.it_flags & (FMODE_WRITE | FMODE_READ))
544                         oit.it_flags |= MDS_OPEN_OWNEROVERRIDE;
545
546                 /* We do not want O_EXCL here, presumably we opened the file
547                  * already? XXX - NFS implications? */
548                 oit.it_flags &= ~O_EXCL;
549
550                 /* bug20584, if "it_flags" contains O_CREAT, the file will be
551                  * created if necessary, then "IT_CREAT" should be set to keep
552                  * consistent with it */
553                 if (oit.it_flags & O_CREAT)
554                         oit.it_op |= IT_CREAT;
555
556                 it = &oit;
557         }
558
559 restart:
560         /* Let's see if we have file open on MDS already. */
561         if (it->it_flags & FMODE_WRITE) {
562                 och_p = &lli->lli_mds_write_och;
563                 och_usecount = &lli->lli_open_fd_write_count;
564         } else if (it->it_flags & FMODE_EXEC) {
565                 och_p = &lli->lli_mds_exec_och;
566                 och_usecount = &lli->lli_open_fd_exec_count;
567          } else {
568                 och_p = &lli->lli_mds_read_och;
569                 och_usecount = &lli->lli_open_fd_read_count;
570         }
571
572         cfs_mutex_lock(&lli->lli_och_mutex);
573         if (*och_p) { /* Open handle is present */
574                 if (it_disposition(it, DISP_OPEN_OPEN)) {
575                         /* Well, there's extra open request that we do not need,
576                            let's close it somehow. This will decref request. */
577                         rc = it_open_error(DISP_OPEN_OPEN, it);
578                         if (rc) {
579                                 cfs_mutex_unlock(&lli->lli_och_mutex);
580                                 GOTO(out_openerr, rc);
581                         }
582
583                         ll_release_openhandle(file->f_dentry, it);
584                 }
585                 (*och_usecount)++;
586
587                 rc = ll_local_open(file, it, fd, NULL);
588                 if (rc) {
589                         (*och_usecount)--;
590                         cfs_mutex_unlock(&lli->lli_och_mutex);
591                         GOTO(out_openerr, rc);
592                 }
593         } else {
594                 LASSERT(*och_usecount == 0);
595                 if (!it->d.lustre.it_disposition) {
596                         /* We cannot just request lock handle now, new ELC code
597                            means that one of other OPEN locks for this file
598                            could be cancelled, and since blocking ast handler
599                            would attempt to grab och_mutex as well, that would
600                            result in a deadlock */
601                         cfs_mutex_unlock(&lli->lli_och_mutex);
602                         it->it_create_mode |= M_CHECK_STALE;
603                         rc = ll_intent_file_open(file, NULL, 0, it);
604                         it->it_create_mode &= ~M_CHECK_STALE;
605                         if (rc)
606                                 GOTO(out_openerr, rc);
607
608                         goto restart;
609                 }
610                 OBD_ALLOC(*och_p, sizeof (struct obd_client_handle));
611                 if (!*och_p)
612                         GOTO(out_och_free, rc = -ENOMEM);
613
614                 (*och_usecount)++;
615
616                 /* md_intent_lock() didn't get a request ref if there was an
617                  * open error, so don't do cleanup on the request here
618                  * (bug 3430) */
619                 /* XXX (green): Should not we bail out on any error here, not
620                  * just open error? */
621                 rc = it_open_error(DISP_OPEN_OPEN, it);
622                 if (rc)
623                         GOTO(out_och_free, rc);
624
625                 LASSERT(it_disposition(it, DISP_ENQ_OPEN_REF));
626
627                 rc = ll_local_open(file, it, fd, *och_p);
628                 if (rc)
629                         GOTO(out_och_free, rc);
630         }
631         cfs_mutex_unlock(&lli->lli_och_mutex);
632         fd = NULL;
633
634         /* Must do this outside lli_och_mutex lock to prevent deadlock where
635            different kind of OPEN lock for this same inode gets cancelled
636            by ldlm_cancel_lru */
637         if (!S_ISREG(inode->i_mode))
638                 GOTO(out_och_free, rc);
639
640         ll_capa_open(inode);
641
642         if (!lli->lli_has_smd) {
643                 if (file->f_flags & O_LOV_DELAY_CREATE ||
644                     !(file->f_mode & FMODE_WRITE)) {
645                         CDEBUG(D_INODE, "object creation was delayed\n");
646                         GOTO(out_och_free, rc);
647                 }
648         }
649         file->f_flags &= ~O_LOV_DELAY_CREATE;
650         GOTO(out_och_free, rc);
651
652 out_och_free:
653         if (it && it_disposition(it, DISP_ENQ_OPEN_REF)) {
654                 ptlrpc_req_finished(it->d.lustre.it_data);
655                 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
656         }
657
658         if (rc) {
659                 if (och_p && *och_p) {
660                         OBD_FREE(*och_p, sizeof (struct obd_client_handle));
661                         *och_p = NULL; /* OBD_FREE writes some magic there */
662                         (*och_usecount)--;
663                 }
664                 cfs_mutex_unlock(&lli->lli_och_mutex);
665
666 out_openerr:
667                 if (opendir_set != 0)
668                         ll_stop_statahead(inode, lli->lli_opendir_key);
669                 if (fd != NULL)
670                         ll_file_data_put(fd);
671         } else {
672                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_OPEN, 1);
673         }
674
675         return rc;
676 }
677
678 /* Fills the obdo with the attributes for the lsm */
679 static int ll_lsm_getattr(struct lov_stripe_md *lsm, struct obd_export *exp,
680                           struct obd_capa *capa, struct obdo *obdo,
681                           __u64 ioepoch, int sync)
682 {
683         struct ptlrpc_request_set *set;
684         struct obd_info            oinfo = { { { 0 } } };
685         int                        rc;
686
687         ENTRY;
688
689         LASSERT(lsm != NULL);
690
691         oinfo.oi_md = lsm;
692         oinfo.oi_oa = obdo;
693         oinfo.oi_oa->o_id = lsm->lsm_object_id;
694         oinfo.oi_oa->o_seq = lsm->lsm_object_seq;
695         oinfo.oi_oa->o_mode = S_IFREG;
696         oinfo.oi_oa->o_ioepoch = ioepoch;
697         oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
698                                OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
699                                OBD_MD_FLBLKSZ | OBD_MD_FLATIME |
700                                OBD_MD_FLMTIME | OBD_MD_FLCTIME |
701                                OBD_MD_FLGROUP | OBD_MD_FLEPOCH |
702                                OBD_MD_FLDATAVERSION;
703         oinfo.oi_capa = capa;
704         if (sync) {
705                 oinfo.oi_oa->o_valid |= OBD_MD_FLFLAGS;
706                 oinfo.oi_oa->o_flags |= OBD_FL_SRVLOCK;
707         }
708
709         set = ptlrpc_prep_set();
710         if (set == NULL) {
711                 CERROR("can't allocate ptlrpc set\n");
712                 rc = -ENOMEM;
713         } else {
714                 rc = obd_getattr_async(exp, &oinfo, set);
715                 if (rc == 0)
716                         rc = ptlrpc_set_wait(set);
717                 ptlrpc_set_destroy(set);
718         }
719         if (rc == 0)
720                 oinfo.oi_oa->o_valid &= (OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ |
721                                          OBD_MD_FLATIME | OBD_MD_FLMTIME |
722                                          OBD_MD_FLCTIME | OBD_MD_FLSIZE |
723                                          OBD_MD_FLDATAVERSION);
724         RETURN(rc);
725 }
726
727 /**
728   * Performs the getattr on the inode and updates its fields.
729   * If @sync != 0, perform the getattr under the server-side lock.
730   */
731 int ll_inode_getattr(struct inode *inode, struct obdo *obdo,
732                      __u64 ioepoch, int sync)
733 {
734         struct obd_capa      *capa = ll_mdscapa_get(inode);
735         struct lov_stripe_md *lsm;
736         int rc;
737         ENTRY;
738
739         lsm = ccc_inode_lsm_get(inode);
740         rc = ll_lsm_getattr(lsm, ll_i2dtexp(inode),
741                             capa, obdo, ioepoch, sync);
742         capa_put(capa);
743         if (rc == 0) {
744                 obdo_refresh_inode(inode, obdo, obdo->o_valid);
745                 CDEBUG(D_INODE,
746                        "objid "LPX64" size %llu, blocks %llu, blksize %lu\n",
747                        lsm ? lsm->lsm_object_id : 0, i_size_read(inode),
748                        (unsigned long long)inode->i_blocks,
749                        (unsigned long)ll_inode_blksize(inode));
750         }
751         ccc_inode_lsm_put(inode, lsm);
752         RETURN(rc);
753 }
754
755 int ll_merge_lvb(struct inode *inode)
756 {
757         struct ll_inode_info *lli = ll_i2info(inode);
758         struct ll_sb_info *sbi = ll_i2sbi(inode);
759         struct lov_stripe_md *lsm;
760         struct ost_lvb lvb;
761         int rc = 0;
762
763         ENTRY;
764
765         lsm = ccc_inode_lsm_get(inode);
766         ll_inode_size_lock(inode);
767         inode_init_lvb(inode, &lvb);
768
769         /* merge timestamps the most resently obtained from mds with
770            timestamps obtained from osts */
771         lvb.lvb_atime = lli->lli_lvb.lvb_atime;
772         lvb.lvb_mtime = lli->lli_lvb.lvb_mtime;
773         lvb.lvb_ctime = lli->lli_lvb.lvb_ctime;
774         if (lsm != NULL) {
775                 rc = obd_merge_lvb(sbi->ll_dt_exp, lsm, &lvb, 0);
776                 cl_isize_write_nolock(inode, lvb.lvb_size);
777
778                 CDEBUG(D_VFSTRACE, DFID" updating i_size "LPU64"\n",
779                                 PFID(&lli->lli_fid), lvb.lvb_size);
780                 inode->i_blocks = lvb.lvb_blocks;
781
782                 LTIME_S(inode->i_mtime) = lvb.lvb_mtime;
783                 LTIME_S(inode->i_atime) = lvb.lvb_atime;
784                 LTIME_S(inode->i_ctime) = lvb.lvb_ctime;
785         }
786         ll_inode_size_unlock(inode);
787         ccc_inode_lsm_put(inode, lsm);
788
789         RETURN(rc);
790 }
791
792 int ll_glimpse_ioctl(struct ll_sb_info *sbi, struct lov_stripe_md *lsm,
793                      lstat_t *st)
794 {
795         struct obdo obdo = { 0 };
796         int rc;
797
798         rc = ll_lsm_getattr(lsm, sbi->ll_dt_exp, NULL, &obdo, 0, 0);
799         if (rc == 0) {
800                 st->st_size   = obdo.o_size;
801                 st->st_blocks = obdo.o_blocks;
802                 st->st_mtime  = obdo.o_mtime;
803                 st->st_atime  = obdo.o_atime;
804                 st->st_ctime  = obdo.o_ctime;
805         }
806         return rc;
807 }
808
809 void ll_io_init(struct cl_io *io, const struct file *file, int write)
810 {
811         struct inode *inode = file->f_dentry->d_inode;
812
813         io->u.ci_rw.crw_nonblock = file->f_flags & O_NONBLOCK;
814         if (write) {
815                 io->u.ci_wr.wr_append = !!(file->f_flags & O_APPEND);
816                 io->u.ci_wr.wr_sync = file->f_flags & O_SYNC || IS_SYNC(inode);
817         }
818         io->ci_obj     = ll_i2info(inode)->lli_clob;
819         io->ci_lockreq = CILR_MAYBE;
820         if (ll_file_nolock(file)) {
821                 io->ci_lockreq = CILR_NEVER;
822                 io->ci_no_srvlock = 1;
823         } else if (file->f_flags & O_APPEND) {
824                 io->ci_lockreq = CILR_MANDATORY;
825         }
826 }
827
828 static ssize_t ll_file_io_generic(const struct lu_env *env,
829                 struct vvp_io_args *args, struct file *file,
830                 enum cl_io_type iot, loff_t *ppos, size_t count)
831 {
832         struct ll_inode_info *lli = ll_i2info(file->f_dentry->d_inode);
833         struct cl_io         *io;
834         ssize_t               result;
835         ENTRY;
836
837         io = ccc_env_thread_io(env);
838         ll_io_init(io, file, iot == CIT_WRITE);
839
840         if (cl_io_rw_init(env, io, iot, *ppos, count) == 0) {
841                 struct vvp_io *vio = vvp_env_io(env);
842                 struct ccc_io *cio = ccc_env_io(env);
843                 int write_mutex_locked = 0;
844
845                 cio->cui_fd  = LUSTRE_FPRIVATE(file);
846                 vio->cui_io_subtype = args->via_io_subtype;
847
848                 switch (vio->cui_io_subtype) {
849                 case IO_NORMAL:
850                         cio->cui_iov = args->u.normal.via_iov;
851                         cio->cui_nrsegs = args->u.normal.via_nrsegs;
852                         cio->cui_tot_nrsegs = cio->cui_nrsegs;
853 #ifndef HAVE_FILE_WRITEV
854                         cio->cui_iocb = args->u.normal.via_iocb;
855 #endif
856                         if ((iot == CIT_WRITE) &&
857                             !(cio->cui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
858                                 if (cfs_mutex_lock_interruptible(&lli->
859                                                                lli_write_mutex))
860                                         GOTO(out, result = -ERESTARTSYS);
861                                 write_mutex_locked = 1;
862                         } else if (iot == CIT_READ) {
863                                 cfs_down_read(&lli->lli_trunc_sem);
864                         }
865                         break;
866                 case IO_SENDFILE:
867                         vio->u.sendfile.cui_actor = args->u.sendfile.via_actor;
868                         vio->u.sendfile.cui_target = args->u.sendfile.via_target;
869                         break;
870                 case IO_SPLICE:
871                         vio->u.splice.cui_pipe = args->u.splice.via_pipe;
872                         vio->u.splice.cui_flags = args->u.splice.via_flags;
873                         break;
874                 default:
875                         CERROR("Unknow IO type - %u\n", vio->cui_io_subtype);
876                         LBUG();
877                 }
878                 result = cl_io_loop(env, io);
879                 if (write_mutex_locked)
880                         cfs_mutex_unlock(&lli->lli_write_mutex);
881                 else if (args->via_io_subtype == IO_NORMAL && iot == CIT_READ)
882                         cfs_up_read(&lli->lli_trunc_sem);
883         } else {
884                 /* cl_io_rw_init() handled IO */
885                 result = io->ci_result;
886         }
887
888         if (io->ci_nob > 0) {
889                 result = io->ci_nob;
890                 *ppos = io->u.ci_wr.wr.crw_pos;
891         }
892         GOTO(out, result);
893 out:
894         cl_io_fini(env, io);
895
896         if (iot == CIT_READ) {
897                 if (result >= 0)
898                         ll_stats_ops_tally(ll_i2sbi(file->f_dentry->d_inode),
899                                            LPROC_LL_READ_BYTES, result);
900         } else if (iot == CIT_WRITE) {
901                 if (result >= 0) {
902                         ll_stats_ops_tally(ll_i2sbi(file->f_dentry->d_inode),
903                                            LPROC_LL_WRITE_BYTES, result);
904                         lli->lli_write_rc = 0;
905                 } else {
906                         lli->lli_write_rc = result;
907                 }
908         }
909
910         return result;
911 }
912
913
914 /*
915  * XXX: exact copy from kernel code (__generic_file_aio_write_nolock)
916  */
917 static int ll_file_get_iov_count(const struct iovec *iov,
918                                  unsigned long *nr_segs, size_t *count)
919 {
920         size_t cnt = 0;
921         unsigned long seg;
922
923         for (seg = 0; seg < *nr_segs; seg++) {
924                 const struct iovec *iv = &iov[seg];
925
926                 /*
927                  * If any segment has a negative length, or the cumulative
928                  * length ever wraps negative then return -EINVAL.
929                  */
930                 cnt += iv->iov_len;
931                 if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
932                         return -EINVAL;
933                 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
934                         continue;
935                 if (seg == 0)
936                         return -EFAULT;
937                 *nr_segs = seg;
938                 cnt -= iv->iov_len;   /* This segment is no good */
939                 break;
940         }
941         *count = cnt;
942         return 0;
943 }
944
945 #ifdef HAVE_FILE_READV
946 static ssize_t ll_file_readv(struct file *file, const struct iovec *iov,
947                               unsigned long nr_segs, loff_t *ppos)
948 {
949         struct lu_env      *env;
950         struct vvp_io_args *args;
951         size_t              count;
952         ssize_t             result;
953         int                 refcheck;
954         ENTRY;
955
956         result = ll_file_get_iov_count(iov, &nr_segs, &count);
957         if (result)
958                 RETURN(result);
959
960         env = cl_env_get(&refcheck);
961         if (IS_ERR(env))
962                 RETURN(PTR_ERR(env));
963
964         args = vvp_env_args(env, IO_NORMAL);
965         args->u.normal.via_iov = (struct iovec *)iov;
966         args->u.normal.via_nrsegs = nr_segs;
967
968         result = ll_file_io_generic(env, args, file, CIT_READ, ppos, count);
969         cl_env_put(env, &refcheck);
970         RETURN(result);
971 }
972
973 static ssize_t ll_file_read(struct file *file, char *buf, size_t count,
974                             loff_t *ppos)
975 {
976         struct lu_env *env;
977         struct iovec  *local_iov;
978         ssize_t        result;
979         int            refcheck;
980         ENTRY;
981
982         env = cl_env_get(&refcheck);
983         if (IS_ERR(env))
984                 RETURN(PTR_ERR(env));
985
986         local_iov = &vvp_env_info(env)->vti_local_iov;
987         local_iov->iov_base = (void __user *)buf;
988         local_iov->iov_len = count;
989         result = ll_file_readv(file, local_iov, 1, ppos);
990         cl_env_put(env, &refcheck);
991         RETURN(result);
992 }
993
994 #else
995 static ssize_t ll_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
996                                 unsigned long nr_segs, loff_t pos)
997 {
998         struct lu_env      *env;
999         struct vvp_io_args *args;
1000         size_t              count;
1001         ssize_t             result;
1002         int                 refcheck;
1003         ENTRY;
1004
1005         result = ll_file_get_iov_count(iov, &nr_segs, &count);
1006         if (result)
1007                 RETURN(result);
1008
1009         env = cl_env_get(&refcheck);
1010         if (IS_ERR(env))
1011                 RETURN(PTR_ERR(env));
1012
1013         args = vvp_env_args(env, IO_NORMAL);
1014         args->u.normal.via_iov = (struct iovec *)iov;
1015         args->u.normal.via_nrsegs = nr_segs;
1016         args->u.normal.via_iocb = iocb;
1017
1018         result = ll_file_io_generic(env, args, iocb->ki_filp, CIT_READ,
1019                                     &iocb->ki_pos, count);
1020         cl_env_put(env, &refcheck);
1021         RETURN(result);
1022 }
1023
1024 static ssize_t ll_file_read(struct file *file, char *buf, size_t count,
1025                             loff_t *ppos)
1026 {
1027         struct lu_env *env;
1028         struct iovec  *local_iov;
1029         struct kiocb  *kiocb;
1030         ssize_t        result;
1031         int            refcheck;
1032         ENTRY;
1033
1034         env = cl_env_get(&refcheck);
1035         if (IS_ERR(env))
1036                 RETURN(PTR_ERR(env));
1037
1038         local_iov = &vvp_env_info(env)->vti_local_iov;
1039         kiocb = &vvp_env_info(env)->vti_kiocb;
1040         local_iov->iov_base = (void __user *)buf;
1041         local_iov->iov_len = count;
1042         init_sync_kiocb(kiocb, file);
1043         kiocb->ki_pos = *ppos;
1044         kiocb->ki_left = count;
1045
1046         result = ll_file_aio_read(kiocb, local_iov, 1, kiocb->ki_pos);
1047         *ppos = kiocb->ki_pos;
1048
1049         cl_env_put(env, &refcheck);
1050         RETURN(result);
1051 }
1052 #endif
1053
1054 /*
1055  * Write to a file (through the page cache).
1056  */
1057 #ifdef HAVE_FILE_WRITEV
1058 static ssize_t ll_file_writev(struct file *file, const struct iovec *iov,
1059                               unsigned long nr_segs, loff_t *ppos)
1060 {
1061         struct lu_env      *env;
1062         struct vvp_io_args *args;
1063         size_t              count;
1064         ssize_t             result;
1065         int                 refcheck;
1066         ENTRY;
1067
1068         result = ll_file_get_iov_count(iov, &nr_segs, &count);
1069         if (result)
1070                 RETURN(result);
1071
1072         env = cl_env_get(&refcheck);
1073         if (IS_ERR(env))
1074                 RETURN(PTR_ERR(env));
1075
1076         args = vvp_env_args(env, IO_NORMAL);
1077         args->u.normal.via_iov = (struct iovec *)iov;
1078         args->u.normal.via_nrsegs = nr_segs;
1079
1080         result = ll_file_io_generic(env, args, file, CIT_WRITE, ppos, count);
1081         cl_env_put(env, &refcheck);
1082         RETURN(result);
1083 }
1084
1085 static ssize_t ll_file_write(struct file *file, const char *buf, size_t count,
1086                              loff_t *ppos)
1087 {
1088         struct lu_env    *env;
1089         struct iovec     *local_iov;
1090         ssize_t           result;
1091         int               refcheck;
1092         ENTRY;
1093
1094         env = cl_env_get(&refcheck);
1095         if (IS_ERR(env))
1096                 RETURN(PTR_ERR(env));
1097
1098         local_iov = &vvp_env_info(env)->vti_local_iov;
1099         local_iov->iov_base = (void __user *)buf;
1100         local_iov->iov_len = count;
1101
1102         result = ll_file_writev(file, local_iov, 1, ppos);
1103         cl_env_put(env, &refcheck);
1104         RETURN(result);
1105 }
1106
1107 #else /* AIO stuff */
1108 static ssize_t ll_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
1109                                  unsigned long nr_segs, loff_t pos)
1110 {
1111         struct lu_env      *env;
1112         struct vvp_io_args *args;
1113         size_t              count;
1114         ssize_t             result;
1115         int                 refcheck;
1116         ENTRY;
1117
1118         result = ll_file_get_iov_count(iov, &nr_segs, &count);
1119         if (result)
1120                 RETURN(result);
1121
1122         env = cl_env_get(&refcheck);
1123         if (IS_ERR(env))
1124                 RETURN(PTR_ERR(env));
1125
1126         args = vvp_env_args(env, IO_NORMAL);
1127         args->u.normal.via_iov = (struct iovec *)iov;
1128         args->u.normal.via_nrsegs = nr_segs;
1129         args->u.normal.via_iocb = iocb;
1130
1131         result = ll_file_io_generic(env, args, iocb->ki_filp, CIT_WRITE,
1132                                   &iocb->ki_pos, count);
1133         cl_env_put(env, &refcheck);
1134         RETURN(result);
1135 }
1136
1137 static ssize_t ll_file_write(struct file *file, const char *buf, size_t count,
1138                              loff_t *ppos)
1139 {
1140         struct lu_env *env;
1141         struct iovec  *local_iov;
1142         struct kiocb  *kiocb;
1143         ssize_t        result;
1144         int            refcheck;
1145         ENTRY;
1146
1147         env = cl_env_get(&refcheck);
1148         if (IS_ERR(env))
1149                 RETURN(PTR_ERR(env));
1150
1151         local_iov = &vvp_env_info(env)->vti_local_iov;
1152         kiocb = &vvp_env_info(env)->vti_kiocb;
1153         local_iov->iov_base = (void __user *)buf;
1154         local_iov->iov_len = count;
1155         init_sync_kiocb(kiocb, file);
1156         kiocb->ki_pos = *ppos;
1157         kiocb->ki_left = count;
1158
1159         result = ll_file_aio_write(kiocb, local_iov, 1, kiocb->ki_pos);
1160         *ppos = kiocb->ki_pos;
1161
1162         cl_env_put(env, &refcheck);
1163         RETURN(result);
1164 }
1165 #endif
1166
1167
1168 #ifdef HAVE_KERNEL_SENDFILE
1169 /*
1170  * Send file content (through pagecache) somewhere with helper
1171  */
1172 static ssize_t ll_file_sendfile(struct file *in_file, loff_t *ppos,size_t count,
1173                                 read_actor_t actor, void *target)
1174 {
1175         struct lu_env      *env;
1176         struct vvp_io_args *args;
1177         ssize_t             result;
1178         int                 refcheck;
1179         ENTRY;
1180
1181         env = cl_env_get(&refcheck);
1182         if (IS_ERR(env))
1183                 RETURN(PTR_ERR(env));
1184
1185         args = vvp_env_args(env, IO_SENDFILE);
1186         args->u.sendfile.via_target = target;
1187         args->u.sendfile.via_actor = actor;
1188
1189         result = ll_file_io_generic(env, args, in_file, CIT_READ, ppos, count);
1190         cl_env_put(env, &refcheck);
1191         RETURN(result);
1192 }
1193 #endif
1194
1195 #ifdef HAVE_KERNEL_SPLICE_READ
1196 /*
1197  * Send file content (through pagecache) somewhere with helper
1198  */
1199 static ssize_t ll_file_splice_read(struct file *in_file, loff_t *ppos,
1200                                    struct pipe_inode_info *pipe, size_t count,
1201                                    unsigned int flags)
1202 {
1203         struct lu_env      *env;
1204         struct vvp_io_args *args;
1205         ssize_t             result;
1206         int                 refcheck;
1207         ENTRY;
1208
1209         env = cl_env_get(&refcheck);
1210         if (IS_ERR(env))
1211                 RETURN(PTR_ERR(env));
1212
1213         args = vvp_env_args(env, IO_SPLICE);
1214         args->u.splice.via_pipe = pipe;
1215         args->u.splice.via_flags = flags;
1216
1217         result = ll_file_io_generic(env, args, in_file, CIT_READ, ppos, count);
1218         cl_env_put(env, &refcheck);
1219         RETURN(result);
1220 }
1221 #endif
1222
1223 static int ll_lov_recreate(struct inode *inode, obd_id id, obd_seq seq,
1224                            obd_count ost_idx)
1225 {
1226         struct obd_export *exp = ll_i2dtexp(inode);
1227         struct obd_trans_info oti = { 0 };
1228         struct obdo *oa = NULL;
1229         int lsm_size;
1230         int rc = 0;
1231         struct lov_stripe_md *lsm = NULL, *lsm2;
1232         ENTRY;
1233
1234         OBDO_ALLOC(oa);
1235         if (oa == NULL)
1236                 RETURN(-ENOMEM);
1237
1238         lsm = ccc_inode_lsm_get(inode);
1239         if (lsm == NULL)
1240                 GOTO(out, rc = -ENOENT);
1241
1242         lsm_size = sizeof(*lsm) + (sizeof(struct lov_oinfo) *
1243                    (lsm->lsm_stripe_count));
1244
1245         OBD_ALLOC_LARGE(lsm2, lsm_size);
1246         if (lsm2 == NULL)
1247                 GOTO(out, rc = -ENOMEM);
1248
1249         oa->o_id = id;
1250         oa->o_seq = seq;
1251         oa->o_nlink = ost_idx;
1252         oa->o_flags |= OBD_FL_RECREATE_OBJS;
1253         oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
1254         obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
1255                                    OBD_MD_FLMTIME | OBD_MD_FLCTIME);
1256         obdo_set_parent_fid(oa, &ll_i2info(inode)->lli_fid);
1257         memcpy(lsm2, lsm, lsm_size);
1258         ll_inode_size_lock(inode);
1259         rc = obd_create(NULL, exp, oa, &lsm2, &oti);
1260         ll_inode_size_unlock(inode);
1261
1262         OBD_FREE_LARGE(lsm2, lsm_size);
1263         GOTO(out, rc);
1264 out:
1265         ccc_inode_lsm_put(inode, lsm);
1266         OBDO_FREE(oa);
1267         return rc;
1268 }
1269
1270 static int ll_lov_recreate_obj(struct inode *inode, unsigned long arg)
1271 {
1272         struct ll_recreate_obj ucreat;
1273         ENTRY;
1274
1275         if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1276                 RETURN(-EPERM);
1277
1278         if (cfs_copy_from_user(&ucreat, (struct ll_recreate_obj *)arg,
1279                                sizeof(struct ll_recreate_obj)))
1280                 RETURN(-EFAULT);
1281
1282         RETURN(ll_lov_recreate(inode, ucreat.lrc_id, 0,
1283                                ucreat.lrc_ost_idx));
1284 }
1285
1286 static int ll_lov_recreate_fid(struct inode *inode, unsigned long arg)
1287 {
1288         struct lu_fid fid;
1289         obd_id id;
1290         obd_count ost_idx;
1291         ENTRY;
1292
1293         if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1294                 RETURN(-EPERM);
1295
1296         if (cfs_copy_from_user(&fid, (struct lu_fid *)arg,
1297                                sizeof(struct lu_fid)))
1298                 RETURN(-EFAULT);
1299
1300         id = fid_oid(&fid) | ((fid_seq(&fid) & 0xffff) << 32);
1301         ost_idx = (fid_seq(&fid) >> 16) & 0xffff;
1302         RETURN(ll_lov_recreate(inode, id, 0, ost_idx));
1303 }
1304
1305 int ll_lov_setstripe_ea_info(struct inode *inode, struct file *file,
1306                              int flags, struct lov_user_md *lum, int lum_size)
1307 {
1308         struct lov_stripe_md *lsm = NULL;
1309         struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
1310         int rc = 0;
1311         ENTRY;
1312
1313         lsm = ccc_inode_lsm_get(inode);
1314         if (lsm != NULL) {
1315                 ccc_inode_lsm_put(inode, lsm);
1316                 CDEBUG(D_IOCTL, "stripe already exists for ino %lu\n",
1317                        inode->i_ino);
1318                 RETURN(-EEXIST);
1319         }
1320
1321         ll_inode_size_lock(inode);
1322         rc = ll_intent_file_open(file, lum, lum_size, &oit);
1323         if (rc)
1324                 GOTO(out, rc);
1325         rc = oit.d.lustre.it_status;
1326         if (rc < 0)
1327                 GOTO(out_req_free, rc);
1328
1329         ll_release_openhandle(file->f_dentry, &oit);
1330
1331  out:
1332         ll_inode_size_unlock(inode);
1333         ll_intent_release(&oit);
1334         ccc_inode_lsm_put(inode, lsm);
1335         RETURN(rc);
1336 out_req_free:
1337         ptlrpc_req_finished((struct ptlrpc_request *) oit.d.lustre.it_data);
1338         goto out;
1339 }
1340
1341 int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
1342                              struct lov_mds_md **lmmp, int *lmm_size,
1343                              struct ptlrpc_request **request)
1344 {
1345         struct ll_sb_info *sbi = ll_i2sbi(inode);
1346         struct mdt_body  *body;
1347         struct lov_mds_md *lmm = NULL;
1348         struct ptlrpc_request *req = NULL;
1349         struct md_op_data *op_data;
1350         int rc, lmmsize;
1351
1352         rc = ll_get_max_mdsize(sbi, &lmmsize);
1353         if (rc)
1354                 RETURN(rc);
1355
1356         op_data = ll_prep_md_op_data(NULL, inode, NULL, filename,
1357                                      strlen(filename), lmmsize,
1358                                      LUSTRE_OPC_ANY, NULL);
1359         if (IS_ERR(op_data))
1360                 RETURN(PTR_ERR(op_data));
1361
1362         op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
1363         rc = md_getattr_name(sbi->ll_md_exp, op_data, &req);
1364         ll_finish_md_op_data(op_data);
1365         if (rc < 0) {
1366                 CDEBUG(D_INFO, "md_getattr_name failed "
1367                        "on %s: rc %d\n", filename, rc);
1368                 GOTO(out, rc);
1369         }
1370
1371         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1372         LASSERT(body != NULL); /* checked by mdc_getattr_name */
1373
1374         lmmsize = body->eadatasize;
1375
1376         if (!(body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
1377                         lmmsize == 0) {
1378                 GOTO(out, rc = -ENODATA);
1379         }
1380
1381         lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_MDT_MD, lmmsize);
1382         LASSERT(lmm != NULL);
1383
1384         if ((lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V1)) &&
1385             (lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V3))) {
1386                 GOTO(out, rc = -EPROTO);
1387         }
1388
1389         /*
1390          * This is coming from the MDS, so is probably in
1391          * little endian.  We convert it to host endian before
1392          * passing it to userspace.
1393          */
1394         if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC)) {
1395                 /* if function called for directory - we should
1396                  * avoid swab not existent lsm objects */
1397                 if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1)) {
1398                         lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
1399                         if (S_ISREG(body->mode))
1400                                 lustre_swab_lov_user_md_objects(
1401                                  ((struct lov_user_md_v1 *)lmm)->lmm_objects,
1402                                  ((struct lov_user_md_v1 *)lmm)->lmm_stripe_count);
1403                 } else if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3)) {
1404                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
1405                         if (S_ISREG(body->mode))
1406                                 lustre_swab_lov_user_md_objects(
1407                                  ((struct lov_user_md_v3 *)lmm)->lmm_objects,
1408                                  ((struct lov_user_md_v3 *)lmm)->lmm_stripe_count);
1409                 }
1410         }
1411
1412 out:
1413         *lmmp = lmm;
1414         *lmm_size = lmmsize;
1415         *request = req;
1416         return rc;
1417 }
1418
1419 static int ll_lov_setea(struct inode *inode, struct file *file,
1420                             unsigned long arg)
1421 {
1422         int flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
1423         struct lov_user_md  *lump;
1424         int lum_size = sizeof(struct lov_user_md) +
1425                        sizeof(struct lov_user_ost_data);
1426         int rc;
1427         ENTRY;
1428
1429         if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1430                 RETURN(-EPERM);
1431
1432         OBD_ALLOC_LARGE(lump, lum_size);
1433         if (lump == NULL) {
1434                 RETURN(-ENOMEM);
1435         }
1436         if (cfs_copy_from_user(lump, (struct lov_user_md  *)arg, lum_size)) {
1437                 OBD_FREE_LARGE(lump, lum_size);
1438                 RETURN(-EFAULT);
1439         }
1440
1441         rc = ll_lov_setstripe_ea_info(inode, file, flags, lump, lum_size);
1442
1443         OBD_FREE_LARGE(lump, lum_size);
1444         RETURN(rc);
1445 }
1446
1447 static int ll_lov_setstripe(struct inode *inode, struct file *file,
1448                             unsigned long arg)
1449 {
1450         struct lov_user_md_v3 lumv3;
1451         struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
1452         struct lov_user_md_v1 *lumv1p = (struct lov_user_md_v1 *)arg;
1453         struct lov_user_md_v3 *lumv3p = (struct lov_user_md_v3 *)arg;
1454         int lum_size;
1455         int rc;
1456         int flags = FMODE_WRITE;
1457         ENTRY;
1458
1459         /* first try with v1 which is smaller than v3 */
1460         lum_size = sizeof(struct lov_user_md_v1);
1461         if (cfs_copy_from_user(lumv1, lumv1p, lum_size))
1462                 RETURN(-EFAULT);
1463
1464         if (lumv1->lmm_magic == LOV_USER_MAGIC_V3) {
1465                 lum_size = sizeof(struct lov_user_md_v3);
1466                 if (cfs_copy_from_user(&lumv3, lumv3p, lum_size))
1467                         RETURN(-EFAULT);
1468         }
1469
1470         rc = ll_lov_setstripe_ea_info(inode, file, flags, lumv1, lum_size);
1471         if (rc == 0) {
1472                 struct lov_stripe_md *lsm;
1473                 put_user(0, &lumv1p->lmm_stripe_count);
1474                 lsm = ccc_inode_lsm_get(inode);
1475                 rc = obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2dtexp(inode),
1476                                    0, lsm, (void *)arg);
1477                 ccc_inode_lsm_put(inode, lsm);
1478         }
1479         RETURN(rc);
1480 }
1481
1482 static int ll_lov_getstripe(struct inode *inode, unsigned long arg)
1483 {
1484         struct lov_stripe_md *lsm;
1485         int rc = -ENODATA;
1486         ENTRY;
1487
1488         lsm = ccc_inode_lsm_get(inode);
1489         if (lsm != NULL)
1490                 rc = obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2dtexp(inode), 0,
1491                                    lsm, (void *)arg);
1492         ccc_inode_lsm_put(inode, lsm);
1493         RETURN(rc);
1494 }
1495
1496 int ll_get_grouplock(struct inode *inode, struct file *file, unsigned long arg)
1497 {
1498         struct ll_inode_info   *lli = ll_i2info(inode);
1499         struct ll_file_data    *fd = LUSTRE_FPRIVATE(file);
1500         struct ccc_grouplock    grouplock;
1501         int                     rc;
1502         ENTRY;
1503
1504         if (ll_file_nolock(file))
1505                 RETURN(-EOPNOTSUPP);
1506
1507         cfs_spin_lock(&lli->lli_lock);
1508         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1509                 CWARN("group lock already existed with gid %lu\n",
1510                        fd->fd_grouplock.cg_gid);
1511                 cfs_spin_unlock(&lli->lli_lock);
1512                 RETURN(-EINVAL);
1513         }
1514         LASSERT(fd->fd_grouplock.cg_lock == NULL);
1515         cfs_spin_unlock(&lli->lli_lock);
1516
1517         rc = cl_get_grouplock(cl_i2info(inode)->lli_clob,
1518                               arg, (file->f_flags & O_NONBLOCK), &grouplock);
1519         if (rc)
1520                 RETURN(rc);
1521
1522         cfs_spin_lock(&lli->lli_lock);
1523         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1524                 cfs_spin_unlock(&lli->lli_lock);
1525                 CERROR("another thread just won the race\n");
1526                 cl_put_grouplock(&grouplock);
1527                 RETURN(-EINVAL);
1528         }
1529
1530         fd->fd_flags |= LL_FILE_GROUP_LOCKED;
1531         fd->fd_grouplock = grouplock;
1532         cfs_spin_unlock(&lli->lli_lock);
1533
1534         CDEBUG(D_INFO, "group lock %lu obtained\n", arg);
1535         RETURN(0);
1536 }
1537
1538 int ll_put_grouplock(struct inode *inode, struct file *file, unsigned long arg)
1539 {
1540         struct ll_inode_info   *lli = ll_i2info(inode);
1541         struct ll_file_data    *fd = LUSTRE_FPRIVATE(file);
1542         struct ccc_grouplock    grouplock;
1543         ENTRY;
1544
1545         cfs_spin_lock(&lli->lli_lock);
1546         if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1547                 cfs_spin_unlock(&lli->lli_lock);
1548                 CWARN("no group lock held\n");
1549                 RETURN(-EINVAL);
1550         }
1551         LASSERT(fd->fd_grouplock.cg_lock != NULL);
1552
1553         if (fd->fd_grouplock.cg_gid != arg) {
1554                 CWARN("group lock %lu doesn't match current id %lu\n",
1555                        arg, fd->fd_grouplock.cg_gid);
1556                 cfs_spin_unlock(&lli->lli_lock);
1557                 RETURN(-EINVAL);
1558         }
1559
1560         grouplock = fd->fd_grouplock;
1561         memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
1562         fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
1563         cfs_spin_unlock(&lli->lli_lock);
1564
1565         cl_put_grouplock(&grouplock);
1566         CDEBUG(D_INFO, "group lock %lu released\n", arg);
1567         RETURN(0);
1568 }
1569
1570 /**
1571  * Close inode open handle
1572  *
1573  * \param dentry [in]     dentry which contains the inode
1574  * \param it     [in,out] intent which contains open info and result
1575  *
1576  * \retval 0     success
1577  * \retval <0    failure
1578  */
1579 int ll_release_openhandle(struct dentry *dentry, struct lookup_intent *it)
1580 {
1581         struct inode *inode = dentry->d_inode;
1582         struct obd_client_handle *och;
1583         int rc;
1584         ENTRY;
1585
1586         LASSERT(inode);
1587
1588         /* Root ? Do nothing. */
1589         if (dentry->d_inode->i_sb->s_root == dentry)
1590                 RETURN(0);
1591
1592         /* No open handle to close? Move away */
1593         if (!it_disposition(it, DISP_OPEN_OPEN))
1594                 RETURN(0);
1595
1596         LASSERT(it_open_error(DISP_OPEN_OPEN, it) == 0);
1597
1598         OBD_ALLOC(och, sizeof(*och));
1599         if (!och)
1600                 GOTO(out, rc = -ENOMEM);
1601
1602         ll_och_fill(ll_i2sbi(inode)->ll_md_exp,
1603                     ll_i2info(inode), it, och);
1604
1605         rc = ll_close_inode_openhandle(ll_i2sbi(inode)->ll_md_exp,
1606                                        inode, och);
1607  out:
1608         /* this one is in place of ll_file_open */
1609         if (it_disposition(it, DISP_ENQ_OPEN_REF)) {
1610                 ptlrpc_req_finished(it->d.lustre.it_data);
1611                 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
1612         }
1613         RETURN(rc);
1614 }
1615
1616 /**
1617  * Get size for inode for which FIEMAP mapping is requested.
1618  * Make the FIEMAP get_info call and returns the result.
1619  */
1620 int ll_do_fiemap(struct inode *inode, struct ll_user_fiemap *fiemap,
1621               int num_bytes)
1622 {
1623         struct obd_export *exp = ll_i2dtexp(inode);
1624         struct lov_stripe_md *lsm = NULL;
1625         struct ll_fiemap_info_key fm_key = { .name = KEY_FIEMAP, };
1626         int vallen = num_bytes;
1627         int rc;
1628         ENTRY;
1629
1630         /* Checks for fiemap flags */
1631         if (fiemap->fm_flags & ~LUSTRE_FIEMAP_FLAGS_COMPAT) {
1632                 fiemap->fm_flags &= ~LUSTRE_FIEMAP_FLAGS_COMPAT;
1633                 return -EBADR;
1634         }
1635
1636         /* Check for FIEMAP_FLAG_SYNC */
1637         if (fiemap->fm_flags & FIEMAP_FLAG_SYNC) {
1638                 rc = filemap_fdatawrite(inode->i_mapping);
1639                 if (rc)
1640                         return rc;
1641         }
1642
1643         lsm = ccc_inode_lsm_get(inode);
1644         if (lsm == NULL)
1645                 return -ENOENT;
1646
1647         /* If the stripe_count > 1 and the application does not understand
1648          * DEVICE_ORDER flag, then it cannot interpret the extents correctly.
1649          */
1650         if (lsm->lsm_stripe_count > 1 &&
1651             !(fiemap->fm_flags & FIEMAP_FLAG_DEVICE_ORDER))
1652                 GOTO(out, rc = -EOPNOTSUPP);
1653
1654         fm_key.oa.o_id = lsm->lsm_object_id;
1655         fm_key.oa.o_seq = lsm->lsm_object_seq;
1656         fm_key.oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1657
1658         obdo_from_inode(&fm_key.oa, inode, OBD_MD_FLSIZE);
1659         obdo_set_parent_fid(&fm_key.oa, &ll_i2info(inode)->lli_fid);
1660         /* If filesize is 0, then there would be no objects for mapping */
1661         if (fm_key.oa.o_size == 0) {
1662                 fiemap->fm_mapped_extents = 0;
1663                 GOTO(out, rc = 0);
1664         }
1665
1666         memcpy(&fm_key.fiemap, fiemap, sizeof(*fiemap));
1667
1668         rc = obd_get_info(NULL, exp, sizeof(fm_key), &fm_key, &vallen,
1669                           fiemap, lsm);
1670         if (rc)
1671                 CERROR("obd_get_info failed: rc = %d\n", rc);
1672
1673 out:
1674         ccc_inode_lsm_put(inode, lsm);
1675         RETURN(rc);
1676 }
1677
1678 int ll_fid2path(struct obd_export *exp, void *arg)
1679 {
1680         struct getinfo_fid2path *gfout, *gfin;
1681         int outsize, rc;
1682         ENTRY;
1683
1684         /* Need to get the buflen */
1685         OBD_ALLOC_PTR(gfin);
1686         if (gfin == NULL)
1687                 RETURN(-ENOMEM);
1688         if (cfs_copy_from_user(gfin, arg, sizeof(*gfin))) {
1689                 OBD_FREE_PTR(gfin);
1690                 RETURN(-EFAULT);
1691         }
1692
1693         outsize = sizeof(*gfout) + gfin->gf_pathlen;
1694         OBD_ALLOC(gfout, outsize);
1695         if (gfout == NULL) {
1696                 OBD_FREE_PTR(gfin);
1697                 RETURN(-ENOMEM);
1698         }
1699         memcpy(gfout, gfin, sizeof(*gfout));
1700         OBD_FREE_PTR(gfin);
1701
1702         /* Call mdc_iocontrol */
1703         rc = obd_iocontrol(OBD_IOC_FID2PATH, exp, outsize, gfout, NULL);
1704         if (rc)
1705                 GOTO(gf_free, rc);
1706         if (cfs_copy_to_user(arg, gfout, outsize))
1707                 rc = -EFAULT;
1708
1709 gf_free:
1710         OBD_FREE(gfout, outsize);
1711         RETURN(rc);
1712 }
1713
1714 static int ll_ioctl_fiemap(struct inode *inode, unsigned long arg)
1715 {
1716         struct ll_user_fiemap *fiemap_s;
1717         size_t num_bytes, ret_bytes;
1718         unsigned int extent_count;
1719         int rc = 0;
1720
1721         /* Get the extent count so we can calculate the size of
1722          * required fiemap buffer */
1723         if (get_user(extent_count,
1724             &((struct ll_user_fiemap __user *)arg)->fm_extent_count))
1725                 RETURN(-EFAULT);
1726         num_bytes = sizeof(*fiemap_s) + (extent_count *
1727                                          sizeof(struct ll_fiemap_extent));
1728
1729         OBD_ALLOC_LARGE(fiemap_s, num_bytes);
1730         if (fiemap_s == NULL)
1731                 RETURN(-ENOMEM);
1732
1733         /* get the fiemap value */
1734         if (copy_from_user(fiemap_s,(struct ll_user_fiemap __user *)arg,
1735                            sizeof(*fiemap_s)))
1736                 GOTO(error, rc = -EFAULT);
1737
1738         /* If fm_extent_count is non-zero, read the first extent since
1739          * it is used to calculate end_offset and device from previous
1740          * fiemap call. */
1741         if (extent_count) {
1742                 if (copy_from_user(&fiemap_s->fm_extents[0],
1743                     (char __user *)arg + sizeof(*fiemap_s),
1744                     sizeof(struct ll_fiemap_extent)))
1745                         GOTO(error, rc = -EFAULT);
1746         }
1747
1748         rc = ll_do_fiemap(inode, fiemap_s, num_bytes);
1749         if (rc)
1750                 GOTO(error, rc);
1751
1752         ret_bytes = sizeof(struct ll_user_fiemap);
1753
1754         if (extent_count != 0)
1755                 ret_bytes += (fiemap_s->fm_mapped_extents *
1756                                  sizeof(struct ll_fiemap_extent));
1757
1758         if (copy_to_user((void *)arg, fiemap_s, ret_bytes))
1759                 rc = -EFAULT;
1760
1761 error:
1762         OBD_FREE_LARGE(fiemap_s, num_bytes);
1763         RETURN(rc);
1764 }
1765
1766 /*
1767  * Read the data_version for inode.
1768  *
1769  * This value is computed using stripe object version on OST.
1770  * Version is computed using server side locking.
1771  *
1772  * @param extent_lock  Take extent lock. Not needed if a process is already
1773  *                     holding the OST object group locks.
1774  */
1775 static int ll_data_version(struct inode *inode, __u64 *data_version,
1776                            int extent_lock)
1777 {
1778         struct lov_stripe_md *lsm = NULL;
1779         struct ll_sb_info    *sbi = ll_i2sbi(inode);
1780         struct obdo          *obdo = NULL;
1781         int                   rc;
1782         ENTRY;
1783
1784         /* If no stripe, we consider version is 0. */
1785         lsm = ccc_inode_lsm_get(inode);
1786         if (lsm == NULL) {
1787                 *data_version = 0;
1788                 CDEBUG(D_INODE, "No object for inode\n");
1789                 RETURN(0);
1790         }
1791
1792         OBD_ALLOC_PTR(obdo);
1793         if (obdo == NULL) {
1794                 ccc_inode_lsm_put(inode, lsm);
1795                 RETURN(-ENOMEM);
1796         }
1797
1798         rc = ll_lsm_getattr(lsm, sbi->ll_dt_exp, NULL, obdo, 0, extent_lock);
1799         if (!rc) {
1800                 if (!(obdo->o_valid & OBD_MD_FLDATAVERSION))
1801                         rc = -EOPNOTSUPP;
1802                 else
1803                         *data_version = obdo->o_data_version;
1804         }
1805
1806         OBD_FREE_PTR(obdo);
1807         ccc_inode_lsm_put(inode, lsm);
1808
1809         RETURN(rc);
1810 }
1811
1812 long ll_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1813 {
1814         struct inode *inode = file->f_dentry->d_inode;
1815         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1816         int flags;
1817
1818         ENTRY;
1819
1820         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),cmd=%x\n", inode->i_ino,
1821                inode->i_generation, inode, cmd);
1822         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1823
1824         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1825         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1826                 RETURN(-ENOTTY);
1827
1828         switch(cmd) {
1829         case LL_IOC_GETFLAGS:
1830                 /* Get the current value of the file flags */
1831                 return put_user(fd->fd_flags, (int *)arg);
1832         case LL_IOC_SETFLAGS:
1833         case LL_IOC_CLRFLAGS:
1834                 /* Set or clear specific file flags */
1835                 /* XXX This probably needs checks to ensure the flags are
1836                  *     not abused, and to handle any flag side effects.
1837                  */
1838                 if (get_user(flags, (int *) arg))
1839                         RETURN(-EFAULT);
1840
1841                 if (cmd == LL_IOC_SETFLAGS) {
1842                         if ((flags & LL_FILE_IGNORE_LOCK) &&
1843                             !(file->f_flags & O_DIRECT)) {
1844                                 CERROR("%s: unable to disable locking on "
1845                                        "non-O_DIRECT file\n", current->comm);
1846                                 RETURN(-EINVAL);
1847                         }
1848
1849                         fd->fd_flags |= flags;
1850                 } else {
1851                         fd->fd_flags &= ~flags;
1852                 }
1853                 RETURN(0);
1854         case LL_IOC_LOV_SETSTRIPE:
1855                 RETURN(ll_lov_setstripe(inode, file, arg));
1856         case LL_IOC_LOV_SETEA:
1857                 RETURN(ll_lov_setea(inode, file, arg));
1858         case LL_IOC_LOV_GETSTRIPE:
1859                 RETURN(ll_lov_getstripe(inode, arg));
1860         case LL_IOC_RECREATE_OBJ:
1861                 RETURN(ll_lov_recreate_obj(inode, arg));
1862         case LL_IOC_RECREATE_FID:
1863                 RETURN(ll_lov_recreate_fid(inode, arg));
1864         case FSFILT_IOC_FIEMAP:
1865                 RETURN(ll_ioctl_fiemap(inode, arg));
1866         case FSFILT_IOC_GETFLAGS:
1867         case FSFILT_IOC_SETFLAGS:
1868                 RETURN(ll_iocontrol(inode, file, cmd, arg));
1869         case FSFILT_IOC_GETVERSION_OLD:
1870         case FSFILT_IOC_GETVERSION:
1871                 RETURN(put_user(inode->i_generation, (int *)arg));
1872         case LL_IOC_GROUP_LOCK:
1873                 RETURN(ll_get_grouplock(inode, file, arg));
1874         case LL_IOC_GROUP_UNLOCK:
1875                 RETURN(ll_put_grouplock(inode, file, arg));
1876         case IOC_OBD_STATFS:
1877                 RETURN(ll_obd_statfs(inode, (void *)arg));
1878
1879         /* We need to special case any other ioctls we want to handle,
1880          * to send them to the MDS/OST as appropriate and to properly
1881          * network encode the arg field.
1882         case FSFILT_IOC_SETVERSION_OLD:
1883         case FSFILT_IOC_SETVERSION:
1884         */
1885         case LL_IOC_FLUSHCTX:
1886                 RETURN(ll_flush_ctx(inode));
1887         case LL_IOC_PATH2FID: {
1888                 if (cfs_copy_to_user((void *)arg, ll_inode2fid(inode),
1889                                      sizeof(struct lu_fid)))
1890                         RETURN(-EFAULT);
1891
1892                 RETURN(0);
1893         }
1894         case OBD_IOC_FID2PATH:
1895                 RETURN(ll_fid2path(ll_i2mdexp(inode), (void *)arg));
1896         case LL_IOC_DATA_VERSION: {
1897                 struct ioc_data_version idv;
1898                 int rc;
1899
1900                 if (cfs_copy_from_user(&idv, (char *)arg, sizeof(idv)))
1901                         RETURN(-EFAULT);
1902
1903                 rc = ll_data_version(inode, &idv.idv_version,
1904                                      !(idv.idv_flags & LL_DV_NOFLUSH));
1905
1906                 if (rc == 0 &&
1907                     cfs_copy_to_user((char *) arg, &idv, sizeof(idv)))
1908                         RETURN(-EFAULT);
1909
1910                 RETURN(rc);
1911         }
1912
1913         case LL_IOC_GET_MDTIDX: {
1914                 int mdtidx;
1915
1916                 mdtidx = ll_get_mdt_idx(inode);
1917                 if (mdtidx < 0)
1918                         RETURN(mdtidx);
1919
1920                 if (put_user((int)mdtidx, (int*)arg))
1921                         RETURN(-EFAULT);
1922
1923                 RETURN(0);
1924         }
1925         case OBD_IOC_GETDTNAME:
1926         case OBD_IOC_GETMDNAME:
1927                 RETURN(ll_get_obd_name(inode, cmd, arg));
1928         default: {
1929                 int err;
1930
1931                 if (LLIOC_STOP ==
1932                     ll_iocontrol_call(inode, file, cmd, arg, &err))
1933                         RETURN(err);
1934
1935                 RETURN(obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
1936                                      (void *)arg));
1937         }
1938         }
1939 }
1940
1941 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
1942 {
1943         struct inode *inode = file->f_dentry->d_inode;
1944         loff_t retval;
1945         ENTRY;
1946         retval = offset + ((origin == 2) ? i_size_read(inode) :
1947                            (origin == 1) ? file->f_pos : 0);
1948         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), to=%llu=%#llx(%s)\n",
1949                inode->i_ino, inode->i_generation, inode, retval, retval,
1950                origin == 2 ? "SEEK_END": origin == 1 ? "SEEK_CUR" : "SEEK_SET");
1951         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LLSEEK, 1);
1952
1953         if (origin == 2) { /* SEEK_END */
1954                 int rc;
1955
1956                 rc = ll_glimpse_size(inode);
1957                 if (rc != 0)
1958                         RETURN(rc);
1959
1960                 offset += i_size_read(inode);
1961         } else if (origin == 1) { /* SEEK_CUR */
1962                 offset += file->f_pos;
1963         }
1964
1965         retval = -EINVAL;
1966         if (offset >= 0 && offset <= ll_file_maxbytes(inode)) {
1967                 if (offset != file->f_pos) {
1968                         file->f_pos = offset;
1969                 }
1970                 retval = offset;
1971         }
1972
1973         RETURN(retval);
1974 }
1975
1976 int ll_flush(struct file *file, fl_owner_t id)
1977 {
1978         struct inode *inode = file->f_dentry->d_inode;
1979         struct ll_inode_info *lli = ll_i2info(inode);
1980         int rc, err;
1981
1982         LASSERT(!S_ISDIR(inode->i_mode));
1983
1984         /* the application should know write failure already. */
1985         if (lli->lli_write_rc)
1986                 return 0;
1987
1988         /* catch async errors that were recorded back when async writeback
1989          * failed for pages in this mapping. */
1990         rc = lli->lli_async_rc;
1991         lli->lli_async_rc = 0;
1992         err = lov_read_and_clear_async_rc(lli->lli_clob);
1993         if (rc == 0)
1994                 rc = err;
1995
1996         return rc ? -EIO : 0;
1997 }
1998
1999 /**
2000  * Called to make sure a portion of file has been written out.
2001  * if @local_only is not true, it will send OST_SYNC RPCs to ost.
2002  *
2003  * Return how many pages have been written.
2004  */
2005 int cl_sync_file_range(struct inode *inode, loff_t start, loff_t end,
2006                        enum cl_fsync_mode mode)
2007 {
2008         struct cl_env_nest nest;
2009         struct lu_env *env;
2010         struct cl_io *io;
2011         struct obd_capa *capa = NULL;
2012         struct cl_fsync_io *fio;
2013         int result;
2014         ENTRY;
2015
2016         if (mode != CL_FSYNC_NONE && mode != CL_FSYNC_LOCAL &&
2017             mode != CL_FSYNC_DISCARD && mode != CL_FSYNC_ALL)
2018                 RETURN(-EINVAL);
2019
2020         env = cl_env_nested_get(&nest);
2021         if (IS_ERR(env))
2022                 RETURN(PTR_ERR(env));
2023
2024         capa = ll_osscapa_get(inode, CAPA_OPC_OSS_WRITE);
2025
2026         io = ccc_env_thread_io(env);
2027         io->ci_obj = cl_i2info(inode)->lli_clob;
2028         io->ci_ignore_layout = 1;
2029
2030         /* initialize parameters for sync */
2031         fio = &io->u.ci_fsync;
2032         fio->fi_capa = capa;
2033         fio->fi_start = start;
2034         fio->fi_end = end;
2035         fio->fi_fid = ll_inode2fid(inode);
2036         fio->fi_mode = mode;
2037         fio->fi_nr_written = 0;
2038
2039         if (cl_io_init(env, io, CIT_FSYNC, io->ci_obj) == 0)
2040                 result = cl_io_loop(env, io);
2041         else
2042                 result = io->ci_result;
2043         if (result == 0)
2044                 result = fio->fi_nr_written;
2045         cl_io_fini(env, io);
2046         cl_env_nested_put(&nest, env);
2047
2048         capa_put(capa);
2049
2050         RETURN(result);
2051 }
2052
2053 #ifdef HAVE_FILE_FSYNC_4ARGS
2054 int ll_fsync(struct file *file, loff_t start, loff_t end, int data)
2055 #elif defined(HAVE_FILE_FSYNC_2ARGS)
2056 int ll_fsync(struct file *file, int data)
2057 #else
2058 int ll_fsync(struct file *file, struct dentry *dentry, int data)
2059 #endif
2060 {
2061         struct inode *inode = file->f_dentry->d_inode;
2062         struct ll_inode_info *lli = ll_i2info(inode);
2063         struct ptlrpc_request *req;
2064         struct obd_capa *oc;
2065         struct lov_stripe_md *lsm;
2066         int rc, err;
2067         ENTRY;
2068         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
2069                inode->i_generation, inode);
2070         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FSYNC, 1);
2071
2072         /* fsync's caller has already called _fdata{sync,write}, we want
2073          * that IO to finish before calling the osc and mdc sync methods */
2074         rc = filemap_fdatawait(inode->i_mapping);
2075
2076         /* catch async errors that were recorded back when async writeback
2077          * failed for pages in this mapping. */
2078         if (!S_ISDIR(inode->i_mode)) {
2079                 err = lli->lli_async_rc;
2080                 lli->lli_async_rc = 0;
2081                 if (rc == 0)
2082                         rc = err;
2083                 err = lov_read_and_clear_async_rc(lli->lli_clob);
2084                 if (rc == 0)
2085                         rc = err;
2086         }
2087
2088         oc = ll_mdscapa_get(inode);
2089         err = md_sync(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode), oc,
2090                       &req);
2091         capa_put(oc);
2092         if (!rc)
2093                 rc = err;
2094         if (!err)
2095                 ptlrpc_req_finished(req);
2096
2097         lsm = ccc_inode_lsm_get(inode);
2098         if (data && lsm) {
2099                 err = cl_sync_file_range(inode, 0, OBD_OBJECT_EOF,
2100                                 CL_FSYNC_ALL);
2101                 if (rc == 0 && err < 0)
2102                         rc = err;
2103                 lli->lli_write_rc = rc < 0 ? rc : 0;
2104         }
2105         ccc_inode_lsm_put(inode, lsm);
2106
2107         RETURN(rc);
2108 }
2109
2110 int ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
2111 {
2112         struct inode *inode = file->f_dentry->d_inode;
2113         struct ll_sb_info *sbi = ll_i2sbi(inode);
2114         struct ldlm_enqueue_info einfo = { .ei_type = LDLM_FLOCK,
2115                                            .ei_cb_cp =ldlm_flock_completion_ast,
2116                                            .ei_cbdata = file_lock };
2117         struct md_op_data *op_data;
2118         struct lustre_handle lockh = {0};
2119         ldlm_policy_data_t flock = {{0}};
2120         int flags = 0;
2121         int rc;
2122         ENTRY;
2123
2124         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu file_lock=%p\n",
2125                inode->i_ino, file_lock);
2126
2127         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FLOCK, 1);
2128
2129         if (file_lock->fl_flags & FL_FLOCK) {
2130                 LASSERT((cmd == F_SETLKW) || (cmd == F_SETLK));
2131                 /* flocks are whole-file locks */
2132                 flock.l_flock.end = OFFSET_MAX;
2133                 /* For flocks owner is determined by the local file desctiptor*/
2134                 flock.l_flock.owner = (unsigned long)file_lock->fl_file;
2135         } else if (file_lock->fl_flags & FL_POSIX) {
2136                 flock.l_flock.owner = (unsigned long)file_lock->fl_owner;
2137                 flock.l_flock.start = file_lock->fl_start;
2138                 flock.l_flock.end = file_lock->fl_end;
2139         } else {
2140                 RETURN(-EINVAL);
2141         }
2142         flock.l_flock.pid = file_lock->fl_pid;
2143
2144         /* Somewhat ugly workaround for svc lockd.
2145          * lockd installs custom fl_lmops->fl_compare_owner that checks
2146          * for the fl_owner to be the same (which it always is on local node
2147          * I guess between lockd processes) and then compares pid.
2148          * As such we assign pid to the owner field to make it all work,
2149          * conflict with normal locks is unlikely since pid space and
2150          * pointer space for current->files are not intersecting */
2151         if (file_lock->fl_lmops && file_lock->fl_lmops->fl_compare_owner)
2152                 flock.l_flock.owner = (unsigned long)file_lock->fl_pid;
2153
2154         switch (file_lock->fl_type) {
2155         case F_RDLCK:
2156                 einfo.ei_mode = LCK_PR;
2157                 break;
2158         case F_UNLCK:
2159                 /* An unlock request may or may not have any relation to
2160                  * existing locks so we may not be able to pass a lock handle
2161                  * via a normal ldlm_lock_cancel() request. The request may even
2162                  * unlock a byte range in the middle of an existing lock. In
2163                  * order to process an unlock request we need all of the same
2164                  * information that is given with a normal read or write record
2165                  * lock request. To avoid creating another ldlm unlock (cancel)
2166                  * message we'll treat a LCK_NL flock request as an unlock. */
2167                 einfo.ei_mode = LCK_NL;
2168                 break;
2169         case F_WRLCK:
2170                 einfo.ei_mode = LCK_PW;
2171                 break;
2172         default:
2173                 CDEBUG(D_INFO, "Unknown fcntl lock type: %d\n",
2174                         file_lock->fl_type);
2175                 RETURN (-ENOTSUPP);
2176         }
2177
2178         switch (cmd) {
2179         case F_SETLKW:
2180 #ifdef F_SETLKW64
2181         case F_SETLKW64:
2182 #endif
2183                 flags = 0;
2184                 break;
2185         case F_SETLK:
2186 #ifdef F_SETLK64
2187         case F_SETLK64:
2188 #endif
2189                 flags = LDLM_FL_BLOCK_NOWAIT;
2190                 break;
2191         case F_GETLK:
2192 #ifdef F_GETLK64
2193         case F_GETLK64:
2194 #endif
2195                 flags = LDLM_FL_TEST_LOCK;
2196                 /* Save the old mode so that if the mode in the lock changes we
2197                  * can decrement the appropriate reader or writer refcount. */
2198                 file_lock->fl_type = einfo.ei_mode;
2199                 break;
2200         default:
2201                 CERROR("unknown fcntl lock command: %d\n", cmd);
2202                 RETURN (-EINVAL);
2203         }
2204
2205         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2206                                      LUSTRE_OPC_ANY, NULL);
2207         if (IS_ERR(op_data))
2208                 RETURN(PTR_ERR(op_data));
2209
2210         CDEBUG(D_DLMTRACE, "inode=%lu, pid=%u, flags=%#x, mode=%u, "
2211                "start="LPU64", end="LPU64"\n", inode->i_ino, flock.l_flock.pid,
2212                flags, einfo.ei_mode, flock.l_flock.start, flock.l_flock.end);
2213
2214         rc = md_enqueue(sbi->ll_md_exp, &einfo, NULL,
2215                         op_data, &lockh, &flock, 0, NULL /* req */, flags);
2216
2217         ll_finish_md_op_data(op_data);
2218
2219         if ((file_lock->fl_flags & FL_FLOCK) &&
2220             (rc == 0 || file_lock->fl_type == F_UNLCK))
2221                 flock_lock_file_wait(file, file_lock);
2222         if ((file_lock->fl_flags & FL_POSIX) &&
2223             (rc == 0 || file_lock->fl_type == F_UNLCK) &&
2224             !(flags & LDLM_FL_TEST_LOCK))
2225                 posix_lock_file_wait(file, file_lock);
2226
2227         RETURN(rc);
2228 }
2229
2230 int ll_file_noflock(struct file *file, int cmd, struct file_lock *file_lock)
2231 {
2232         ENTRY;
2233
2234         RETURN(-ENOSYS);
2235 }
2236
2237 /**
2238  * test if some locks matching bits and l_req_mode are acquired
2239  * - bits can be in different locks
2240  * - if found clear the common lock bits in *bits
2241  * - the bits not found, are kept in *bits
2242  * \param inode [IN]
2243  * \param bits [IN] searched lock bits [IN]
2244  * \param l_req_mode [IN] searched lock mode
2245  * \retval boolean, true iff all bits are found
2246  */
2247 int ll_have_md_lock(struct inode *inode, __u64 *bits,  ldlm_mode_t l_req_mode)
2248 {
2249         struct lustre_handle lockh;
2250         ldlm_policy_data_t policy;
2251         ldlm_mode_t mode = (l_req_mode == LCK_MINMODE) ?
2252                                 (LCK_CR|LCK_CW|LCK_PR|LCK_PW) : l_req_mode;
2253         struct lu_fid *fid;
2254         int flags;
2255         int i;
2256         ENTRY;
2257
2258         if (!inode)
2259                RETURN(0);
2260
2261         fid = &ll_i2info(inode)->lli_fid;
2262         CDEBUG(D_INFO, "trying to match res "DFID" mode %s\n", PFID(fid),
2263                ldlm_lockname[mode]);
2264
2265         flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
2266         for (i = 0; i < MDS_INODELOCK_MAXSHIFT && *bits != 0; i++) {
2267                 policy.l_inodebits.bits = *bits & (1 << i);
2268                 if (policy.l_inodebits.bits == 0)
2269                         continue;
2270
2271                 if (md_lock_match(ll_i2mdexp(inode), flags, fid, LDLM_IBITS,
2272                                   &policy, mode, &lockh)) {
2273                         struct ldlm_lock *lock;
2274
2275                         lock = ldlm_handle2lock(&lockh);
2276                         if (lock) {
2277                                 *bits &=
2278                                       ~(lock->l_policy_data.l_inodebits.bits);
2279                                 LDLM_LOCK_PUT(lock);
2280                         } else {
2281                                 *bits &= ~policy.l_inodebits.bits;
2282                         }
2283                 }
2284         }
2285         RETURN(*bits == 0);
2286 }
2287
2288 ldlm_mode_t ll_take_md_lock(struct inode *inode, __u64 bits,
2289                             struct lustre_handle *lockh)
2290 {
2291         ldlm_policy_data_t policy = { .l_inodebits = {bits}};
2292         struct lu_fid *fid;
2293         ldlm_mode_t rc;
2294         int flags;
2295         ENTRY;
2296
2297         fid = &ll_i2info(inode)->lli_fid;
2298         CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
2299
2300         flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
2301         rc = md_lock_match(ll_i2mdexp(inode), flags, fid, LDLM_IBITS, &policy,
2302                            LCK_CR|LCK_CW|LCK_PR|LCK_PW, lockh);
2303         RETURN(rc);
2304 }
2305
2306 static int ll_inode_revalidate_fini(struct inode *inode, int rc) {
2307         if (rc == -ENOENT) { /* Already unlinked. Just update nlink
2308                               * and return success */
2309                 inode->i_nlink = 0;
2310                 /* This path cannot be hit for regular files unless in
2311                  * case of obscure races, so no need to to validate
2312                  * size. */
2313                 if (!S_ISREG(inode->i_mode) &&
2314                     !S_ISDIR(inode->i_mode))
2315                         return 0;
2316         }
2317
2318         if (rc) {
2319                 CERROR("failure %d inode %lu\n", rc, inode->i_ino);
2320                 return -abs(rc);
2321
2322         }
2323
2324         return 0;
2325 }
2326
2327 int __ll_inode_revalidate_it(struct dentry *dentry, struct lookup_intent *it,
2328                              __u64 ibits)
2329 {
2330         struct inode *inode = dentry->d_inode;
2331         struct ptlrpc_request *req = NULL;
2332         struct obd_export *exp;
2333         int rc = 0;
2334         ENTRY;
2335
2336         if (!inode) {
2337                 CERROR("REPORT THIS LINE TO PETER\n");
2338                 RETURN(0);
2339         }
2340
2341         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),name=%s\n",
2342                inode->i_ino, inode->i_generation, inode, dentry->d_name.name);
2343
2344         exp = ll_i2mdexp(inode);
2345
2346         /* XXX: Enable OBD_CONNECT_ATTRFID to reduce unnecessary getattr RPC.
2347          *      But under CMD case, it caused some lock issues, should be fixed
2348          *      with new CMD ibits lock. See bug 12718 */
2349         if (exp->exp_connect_flags & OBD_CONNECT_ATTRFID) {
2350                 struct lookup_intent oit = { .it_op = IT_GETATTR };
2351                 struct md_op_data *op_data;
2352
2353                 if (ibits == MDS_INODELOCK_LOOKUP)
2354                         oit.it_op = IT_LOOKUP;
2355
2356                 /* Call getattr by fid, so do not provide name at all. */
2357                 op_data = ll_prep_md_op_data(NULL, dentry->d_parent->d_inode,
2358                                              dentry->d_inode, NULL, 0, 0,
2359                                              LUSTRE_OPC_ANY, NULL);
2360                 if (IS_ERR(op_data))
2361                         RETURN(PTR_ERR(op_data));
2362
2363                 oit.it_create_mode |= M_CHECK_STALE;
2364                 rc = md_intent_lock(exp, op_data, NULL, 0,
2365                                     /* we are not interested in name
2366                                        based lookup */
2367                                     &oit, 0, &req,
2368                                     ll_md_blocking_ast, 0);
2369                 ll_finish_md_op_data(op_data);
2370                 oit.it_create_mode &= ~M_CHECK_STALE;
2371                 if (rc < 0) {
2372                         rc = ll_inode_revalidate_fini(inode, rc);
2373                         GOTO (out, rc);
2374                 }
2375
2376                 rc = ll_revalidate_it_finish(req, &oit, dentry);
2377                 if (rc != 0) {
2378                         ll_intent_release(&oit);
2379                         GOTO(out, rc);
2380                 }
2381
2382                 /* Unlinked? Unhash dentry, so it is not picked up later by
2383                    do_lookup() -> ll_revalidate_it(). We cannot use d_drop
2384                    here to preserve get_cwd functionality on 2.6.
2385                    Bug 10503 */
2386                 if (!dentry->d_inode->i_nlink)
2387                         d_lustre_invalidate(dentry);
2388
2389                 ll_lookup_finish_locks(&oit, dentry);
2390         } else if (!ll_have_md_lock(dentry->d_inode, &ibits, LCK_MINMODE)) {
2391                 struct ll_sb_info *sbi = ll_i2sbi(dentry->d_inode);
2392                 obd_valid valid = OBD_MD_FLGETATTR;
2393                 struct md_op_data *op_data;
2394                 int ealen = 0;
2395
2396                 if (S_ISREG(inode->i_mode)) {
2397                         rc = ll_get_max_mdsize(sbi, &ealen);
2398                         if (rc)
2399                                 RETURN(rc);
2400                         valid |= OBD_MD_FLEASIZE | OBD_MD_FLMODEASIZE;
2401                 }
2402
2403                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
2404                                              0, ealen, LUSTRE_OPC_ANY,
2405                                              NULL);
2406                 if (IS_ERR(op_data))
2407                         RETURN(PTR_ERR(op_data));
2408
2409                 op_data->op_valid = valid;
2410                 /* Once OBD_CONNECT_ATTRFID is not supported, we can't find one
2411                  * capa for this inode. Because we only keep capas of dirs
2412                  * fresh. */
2413                 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
2414                 ll_finish_md_op_data(op_data);
2415                 if (rc) {
2416                         rc = ll_inode_revalidate_fini(inode, rc);
2417                         RETURN(rc);
2418                 }
2419
2420                 rc = ll_prep_inode(&inode, req, NULL);
2421         }
2422 out:
2423         ptlrpc_req_finished(req);
2424         return rc;
2425 }
2426
2427 int ll_inode_revalidate_it(struct dentry *dentry, struct lookup_intent *it,
2428                            __u64 ibits)
2429 {
2430         struct inode *inode = dentry->d_inode;
2431         int rc;
2432         ENTRY;
2433
2434         rc = __ll_inode_revalidate_it(dentry, it, ibits);
2435
2436         /* if object not yet allocated, don't validate size */
2437         if (rc == 0 && !ll_i2info(dentry->d_inode)->lli_has_smd) {
2438                 LTIME_S(inode->i_atime) = ll_i2info(inode)->lli_lvb.lvb_atime;
2439                 LTIME_S(inode->i_mtime) = ll_i2info(inode)->lli_lvb.lvb_mtime;
2440                 LTIME_S(inode->i_ctime) = ll_i2info(inode)->lli_lvb.lvb_ctime;
2441                 RETURN(0);
2442         }
2443
2444         /* ll_glimpse_size will prefer locally cached writes if they extend
2445          * the file */
2446
2447         if (rc == 0)
2448                 rc = ll_glimpse_size(inode);
2449
2450         RETURN(rc);
2451 }
2452
2453 int ll_getattr_it(struct vfsmount *mnt, struct dentry *de,
2454                   struct lookup_intent *it, struct kstat *stat)
2455 {
2456         struct inode *inode = de->d_inode;
2457         struct ll_sb_info *sbi = ll_i2sbi(inode);
2458         struct ll_inode_info *lli = ll_i2info(inode);
2459         int res = 0;
2460
2461         res = ll_inode_revalidate_it(de, it, MDS_INODELOCK_UPDATE |
2462                                              MDS_INODELOCK_LOOKUP);
2463         ll_stats_ops_tally(sbi, LPROC_LL_GETATTR, 1);
2464
2465         if (res)
2466                 return res;
2467
2468         stat->dev = inode->i_sb->s_dev;
2469         if (ll_need_32bit_api(sbi))
2470                 stat->ino = cl_fid_build_ino(&lli->lli_fid, 1);
2471         else
2472                 stat->ino = inode->i_ino;
2473         stat->mode = inode->i_mode;
2474         stat->nlink = inode->i_nlink;
2475         stat->uid = inode->i_uid;
2476         stat->gid = inode->i_gid;
2477         stat->rdev = kdev_t_to_nr(inode->i_rdev);
2478         stat->atime = inode->i_atime;
2479         stat->mtime = inode->i_mtime;
2480         stat->ctime = inode->i_ctime;
2481         stat->blksize = 1 << inode->i_blkbits;
2482
2483         stat->size = i_size_read(inode);
2484         stat->blocks = inode->i_blocks;
2485
2486         return 0;
2487 }
2488 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
2489 {
2490         struct lookup_intent it = { .it_op = IT_GETATTR };
2491
2492         return ll_getattr_it(mnt, de, &it, stat);
2493 }
2494
2495 #ifdef HAVE_LINUX_FIEMAP_H
2496 int ll_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2497                 __u64 start, __u64 len)
2498 {
2499         int rc;
2500         size_t num_bytes;
2501         struct ll_user_fiemap *fiemap;
2502         unsigned int extent_count = fieinfo->fi_extents_max;
2503
2504         num_bytes = sizeof(*fiemap) + (extent_count *
2505                                        sizeof(struct ll_fiemap_extent));
2506         OBD_ALLOC_LARGE(fiemap, num_bytes);
2507
2508         if (fiemap == NULL)
2509                 RETURN(-ENOMEM);
2510
2511         fiemap->fm_flags = fieinfo->fi_flags;
2512         fiemap->fm_extent_count = fieinfo->fi_extents_max;
2513         fiemap->fm_start = start;
2514         fiemap->fm_length = len;
2515         memcpy(&fiemap->fm_extents[0], fieinfo->fi_extents_start,
2516                sizeof(struct ll_fiemap_extent));
2517
2518         rc = ll_do_fiemap(inode, fiemap, num_bytes);
2519
2520         fieinfo->fi_flags = fiemap->fm_flags;
2521         fieinfo->fi_extents_mapped = fiemap->fm_mapped_extents;
2522         memcpy(fieinfo->fi_extents_start, &fiemap->fm_extents[0],
2523                fiemap->fm_mapped_extents * sizeof(struct ll_fiemap_extent));
2524
2525         OBD_FREE_LARGE(fiemap, num_bytes);
2526         return rc;
2527 }
2528 #endif
2529
2530
2531 static int
2532 #ifdef HAVE_GENERIC_PERMISSION_4ARGS
2533 lustre_check_acl(struct inode *inode, int mask, unsigned int flags)
2534 #else
2535 lustre_check_acl(struct inode *inode, int mask)
2536 #endif
2537 {
2538 #ifdef CONFIG_FS_POSIX_ACL
2539         struct ll_inode_info *lli = ll_i2info(inode);
2540         struct posix_acl *acl;
2541         int rc;
2542         ENTRY;
2543
2544 #ifdef HAVE_GENERIC_PERMISSION_4ARGS
2545         if (flags & IPERM_FLAG_RCU)
2546                 return -ECHILD;
2547 #endif
2548         cfs_spin_lock(&lli->lli_lock);
2549         acl = posix_acl_dup(lli->lli_posix_acl);
2550         cfs_spin_unlock(&lli->lli_lock);
2551
2552         if (!acl)
2553                 RETURN(-EAGAIN);
2554
2555         rc = posix_acl_permission(inode, acl, mask);
2556         posix_acl_release(acl);
2557
2558         RETURN(rc);
2559 #else
2560         return -EAGAIN;
2561 #endif
2562 }
2563
2564 #ifdef HAVE_GENERIC_PERMISSION_4ARGS
2565 int ll_inode_permission(struct inode *inode, int mask, unsigned int flags)
2566 #else
2567 # ifdef HAVE_INODE_PERMISION_2ARGS
2568 int ll_inode_permission(struct inode *inode, int mask)
2569 # else
2570 int ll_inode_permission(struct inode *inode, int mask, struct nameidata *nd)
2571 # endif
2572 #endif
2573 {
2574         int rc = 0;
2575         ENTRY;
2576
2577 #ifdef HAVE_GENERIC_PERMISSION_4ARGS
2578         if (flags & IPERM_FLAG_RCU)
2579                 return -ECHILD;
2580 #endif
2581
2582        /* as root inode are NOT getting validated in lookup operation,
2583         * need to do it before permission check. */
2584
2585         if (inode == inode->i_sb->s_root->d_inode) {
2586                 struct lookup_intent it = { .it_op = IT_LOOKUP };
2587
2588                 rc = __ll_inode_revalidate_it(inode->i_sb->s_root, &it,
2589                                               MDS_INODELOCK_LOOKUP);
2590                 if (rc)
2591                         RETURN(rc);
2592         }
2593
2594         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), inode mode %x mask %o\n",
2595                inode->i_ino, inode->i_generation, inode, inode->i_mode, mask);
2596
2597         if (ll_i2sbi(inode)->ll_flags & LL_SBI_RMT_CLIENT)
2598                 return lustre_check_remote_perm(inode, mask);
2599
2600         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_INODE_PERM, 1);
2601         rc = ll_generic_permission(inode, mask, flags, lustre_check_acl);
2602
2603         RETURN(rc);
2604 }
2605
2606 #ifdef HAVE_FILE_READV
2607 #define READ_METHOD readv
2608 #define READ_FUNCTION ll_file_readv
2609 #define WRITE_METHOD writev
2610 #define WRITE_FUNCTION ll_file_writev
2611 #else
2612 #define READ_METHOD aio_read
2613 #define READ_FUNCTION ll_file_aio_read
2614 #define WRITE_METHOD aio_write
2615 #define WRITE_FUNCTION ll_file_aio_write
2616 #endif
2617
2618 /* -o localflock - only provides locally consistent flock locks */
2619 struct file_operations ll_file_operations = {
2620         .read           = ll_file_read,
2621         .READ_METHOD    = READ_FUNCTION,
2622         .write          = ll_file_write,
2623         .WRITE_METHOD   = WRITE_FUNCTION,
2624         .unlocked_ioctl = ll_file_ioctl,
2625         .open           = ll_file_open,
2626         .release        = ll_file_release,
2627         .mmap           = ll_file_mmap,
2628         .llseek         = ll_file_seek,
2629 #ifdef HAVE_KERNEL_SENDFILE
2630         .sendfile       = ll_file_sendfile,
2631 #endif
2632 #ifdef HAVE_KERNEL_SPLICE_READ
2633         .splice_read    = ll_file_splice_read,
2634 #endif
2635         .fsync          = ll_fsync,
2636         .flush          = ll_flush
2637 };
2638
2639 struct file_operations ll_file_operations_flock = {
2640         .read           = ll_file_read,
2641         .READ_METHOD    = READ_FUNCTION,
2642         .write          = ll_file_write,
2643         .WRITE_METHOD   = WRITE_FUNCTION,
2644         .unlocked_ioctl = ll_file_ioctl,
2645         .open           = ll_file_open,
2646         .release        = ll_file_release,
2647         .mmap           = ll_file_mmap,
2648         .llseek         = ll_file_seek,
2649 #ifdef HAVE_KERNEL_SENDFILE
2650         .sendfile       = ll_file_sendfile,
2651 #endif
2652 #ifdef HAVE_KERNEL_SPLICE_READ
2653         .splice_read    = ll_file_splice_read,
2654 #endif
2655         .fsync          = ll_fsync,
2656         .flush          = ll_flush,
2657         .flock          = ll_file_flock,
2658         .lock           = ll_file_flock
2659 };
2660
2661 /* These are for -o noflock - to return ENOSYS on flock calls */
2662 struct file_operations ll_file_operations_noflock = {
2663         .read           = ll_file_read,
2664         .READ_METHOD    = READ_FUNCTION,
2665         .write          = ll_file_write,
2666         .WRITE_METHOD   = WRITE_FUNCTION,
2667         .unlocked_ioctl = ll_file_ioctl,
2668         .open           = ll_file_open,
2669         .release        = ll_file_release,
2670         .mmap           = ll_file_mmap,
2671         .llseek         = ll_file_seek,
2672 #ifdef HAVE_KERNEL_SENDFILE
2673         .sendfile       = ll_file_sendfile,
2674 #endif
2675 #ifdef HAVE_KERNEL_SPLICE_READ
2676         .splice_read    = ll_file_splice_read,
2677 #endif
2678         .fsync          = ll_fsync,
2679         .flush          = ll_flush,
2680         .flock          = ll_file_noflock,
2681         .lock           = ll_file_noflock
2682 };
2683
2684 struct inode_operations ll_file_inode_operations = {
2685         .setattr        = ll_setattr,
2686         .truncate       = ll_truncate,
2687         .getattr        = ll_getattr,
2688         .permission     = ll_inode_permission,
2689         .setxattr       = ll_setxattr,
2690         .getxattr       = ll_getxattr,
2691         .listxattr      = ll_listxattr,
2692         .removexattr    = ll_removexattr,
2693 #ifdef  HAVE_LINUX_FIEMAP_H
2694         .fiemap         = ll_fiemap,
2695 #endif
2696 };
2697
2698 /* dynamic ioctl number support routins */
2699 static struct llioc_ctl_data {
2700         cfs_rw_semaphore_t      ioc_sem;
2701         cfs_list_t              ioc_head;
2702 } llioc = {
2703         __RWSEM_INITIALIZER(llioc.ioc_sem),
2704         CFS_LIST_HEAD_INIT(llioc.ioc_head)
2705 };
2706
2707
2708 struct llioc_data {
2709         cfs_list_t              iocd_list;
2710         unsigned int            iocd_size;
2711         llioc_callback_t        iocd_cb;
2712         unsigned int            iocd_count;
2713         unsigned int            iocd_cmd[0];
2714 };
2715
2716 void *ll_iocontrol_register(llioc_callback_t cb, int count, unsigned int *cmd)
2717 {
2718         unsigned int size;
2719         struct llioc_data *in_data = NULL;
2720         ENTRY;
2721
2722         if (cb == NULL || cmd == NULL ||
2723             count > LLIOC_MAX_CMD || count < 0)
2724                 RETURN(NULL);
2725
2726         size = sizeof(*in_data) + count * sizeof(unsigned int);
2727         OBD_ALLOC(in_data, size);
2728         if (in_data == NULL)
2729                 RETURN(NULL);
2730
2731         memset(in_data, 0, sizeof(*in_data));
2732         in_data->iocd_size = size;
2733         in_data->iocd_cb = cb;
2734         in_data->iocd_count = count;
2735         memcpy(in_data->iocd_cmd, cmd, sizeof(unsigned int) * count);
2736
2737         cfs_down_write(&llioc.ioc_sem);
2738         cfs_list_add_tail(&in_data->iocd_list, &llioc.ioc_head);
2739         cfs_up_write(&llioc.ioc_sem);
2740
2741         RETURN(in_data);
2742 }
2743
2744 void ll_iocontrol_unregister(void *magic)
2745 {
2746         struct llioc_data *tmp;
2747
2748         if (magic == NULL)
2749                 return;
2750
2751         cfs_down_write(&llioc.ioc_sem);
2752         cfs_list_for_each_entry(tmp, &llioc.ioc_head, iocd_list) {
2753                 if (tmp == magic) {
2754                         unsigned int size = tmp->iocd_size;
2755
2756                         cfs_list_del(&tmp->iocd_list);
2757                         cfs_up_write(&llioc.ioc_sem);
2758
2759                         OBD_FREE(tmp, size);
2760                         return;
2761                 }
2762         }
2763         cfs_up_write(&llioc.ioc_sem);
2764
2765         CWARN("didn't find iocontrol register block with magic: %p\n", magic);
2766 }
2767
2768 EXPORT_SYMBOL(ll_iocontrol_register);
2769 EXPORT_SYMBOL(ll_iocontrol_unregister);
2770
2771 enum llioc_iter ll_iocontrol_call(struct inode *inode, struct file *file,
2772                         unsigned int cmd, unsigned long arg, int *rcp)
2773 {
2774         enum llioc_iter ret = LLIOC_CONT;
2775         struct llioc_data *data;
2776         int rc = -EINVAL, i;
2777
2778         cfs_down_read(&llioc.ioc_sem);
2779         cfs_list_for_each_entry(data, &llioc.ioc_head, iocd_list) {
2780                 for (i = 0; i < data->iocd_count; i++) {
2781                         if (cmd != data->iocd_cmd[i])
2782                                 continue;
2783
2784                         ret = data->iocd_cb(inode, file, cmd, arg, data, &rc);
2785                         break;
2786                 }
2787
2788                 if (ret == LLIOC_STOP)
2789                         break;
2790         }
2791         cfs_up_read(&llioc.ioc_sem);
2792
2793         if (rcp)
2794                 *rcp = rc;
2795         return ret;
2796 }
2797
2798 int ll_layout_conf(struct inode *inode, const struct cl_object_conf *conf)
2799 {
2800         struct ll_inode_info *lli = ll_i2info(inode);
2801         struct cl_env_nest nest;
2802         struct lu_env *env;
2803         int result;
2804         ENTRY;
2805
2806         if (lli->lli_clob == NULL)
2807                 RETURN(0);
2808
2809         env = cl_env_nested_get(&nest);
2810         if (IS_ERR(env))
2811                 RETURN(PTR_ERR(env));
2812
2813         result = cl_conf_set(env, lli->lli_clob, conf);
2814         cl_env_nested_put(&nest, env);
2815         RETURN(result);
2816 }
2817
2818 /**
2819  * This function checks if there exists a LAYOUT lock on the client side,
2820  * or enqueues it if it doesn't have one in cache.
2821  *
2822  * This function will not hold layout lock so it may be revoked any time after
2823  * this function returns. Any operations depend on layout should be redone
2824  * in that case.
2825  *
2826  * This function should be called before lov_io_init() to get an uptodate
2827  * layout version, the caller should save the version number and after IO
2828  * is finished, this function should be called again to verify that layout
2829  * is not changed during IO time.
2830  */
2831 int ll_layout_refresh(struct inode *inode, __u32 *gen)
2832 {
2833         struct ll_inode_info  *lli = ll_i2info(inode);
2834         struct ll_sb_info     *sbi = ll_i2sbi(inode);
2835         struct md_op_data     *op_data = NULL;
2836         struct ptlrpc_request *req = NULL;
2837         struct lookup_intent   it = { .it_op = IT_LAYOUT };
2838         struct lustre_handle   lockh;
2839         ldlm_mode_t            mode;
2840         struct cl_object_conf  conf = {  .coc_inode = inode,
2841                                          .coc_validate_only = true };
2842         int rc;
2843         ENTRY;
2844
2845         *gen = 0;
2846         if (!(ll_i2sbi(inode)->ll_flags & LL_SBI_LAYOUT_LOCK))
2847                 RETURN(0);
2848
2849         /* sanity checks */
2850         LASSERT(fid_is_sane(ll_inode2fid(inode)));
2851         LASSERT(S_ISREG(inode->i_mode));
2852
2853         /* mostly layout lock is caching on the local side, so try to match
2854          * it before grabbing layout lock mutex. */
2855         mode = ll_take_md_lock(inode, MDS_INODELOCK_LAYOUT, &lockh);
2856         if (mode != 0) { /* hit cached lock */
2857                 struct lov_stripe_md *lsm;
2858
2859                 lsm = ccc_inode_lsm_get(inode);
2860                 if (lsm != NULL)
2861                         *gen = lsm->lsm_layout_gen;
2862                 ccc_inode_lsm_put(inode, lsm);
2863                 ldlm_lock_decref(&lockh, mode);
2864
2865                 RETURN(0);
2866         }
2867
2868         op_data = ll_prep_md_op_data(NULL, inode, inode, NULL,
2869                                      0, 0, LUSTRE_OPC_ANY, NULL);
2870         if (IS_ERR(op_data))
2871                 RETURN(PTR_ERR(op_data));
2872
2873         /* take layout lock mutex to enqueue layout lock exclusively. */
2874         cfs_mutex_lock(&lli->lli_layout_mutex);
2875
2876         /* make sure the old conf goes away */
2877         ll_layout_conf(inode, &conf);
2878
2879         /* enqueue layout lock */
2880         rc = md_intent_lock(sbi->ll_md_exp, op_data, NULL, 0, &it, 0,
2881                         &req, ll_md_blocking_ast, 0);
2882         if (rc == 0) {
2883                 /* we get a new lock, so update the lock data */
2884                 lockh.cookie = it.d.lustre.it_lock_handle;
2885                 md_set_lock_data(sbi->ll_md_exp, &lockh.cookie, inode, NULL);
2886
2887                 /* req == NULL is when lock was found in client cache, without
2888                  * any request to server (but lsm can be canceled just after a
2889                  * release) */
2890                 if (req != NULL) {
2891                         struct ldlm_lock *lock = ldlm_handle2lock(&lockh);
2892                         struct lustre_md md = { NULL };
2893                         void *lmm;
2894                         int lmmsize;
2895
2896                         /* for IT_LAYOUT lock, lmm is returned in lock's lvb
2897                          * data via completion callback */
2898                         LASSERT(lock != NULL);
2899                         lmm = lock->l_lvb_data;
2900                         lmmsize = lock->l_lvb_len;
2901                         if (lmm != NULL)
2902                                 rc = obd_unpackmd(sbi->ll_dt_exp, &md.lsm,
2903                                                 lmm, lmmsize);
2904                         if (rc == 0) {
2905                                 if (md.lsm != NULL)
2906                                         *gen = md.lsm->lsm_layout_gen;
2907
2908                                 memset(&conf, 0, sizeof conf);
2909                                 conf.coc_inode = inode;
2910                                 conf.u.coc_md = &md;
2911                                 ll_layout_conf(inode, &conf);
2912                                 /* is this racy? */
2913                                 lli->lli_has_smd = md.lsm != NULL;
2914                         }
2915                         if (md.lsm != NULL)
2916                                 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
2917
2918                         LDLM_LOCK_PUT(lock);
2919                         ptlrpc_req_finished(req);
2920                 } else { /* hit caching lock */
2921                         struct lov_stripe_md *lsm;
2922
2923                         lsm = ccc_inode_lsm_get(inode);
2924                         if (lsm != NULL)
2925                                 *gen = lsm->lsm_layout_gen;
2926                         ccc_inode_lsm_put(inode, lsm);
2927                 }
2928                 ll_intent_drop_lock(&it);
2929         }
2930         cfs_mutex_unlock(&lli->lli_layout_mutex);
2931         ll_finish_md_op_data(op_data);
2932
2933         RETURN(rc);
2934 }