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