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