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