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