Whamcloud - gitweb
b=8654
[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  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5  *   Author: Peter Braam <braam@clusterfs.com>
6  *   Author: Phil Schwan <phil@clusterfs.com>
7  *   Author: Andreas Dilger <adilger@clusterfs.com>
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
11  *   Lustre is free software; you can redistribute it and/or
12  *   modify it under the terms of version 2 of the GNU General Public
13  *   License as published by the Free Software Foundation.
14  *
15  *   Lustre is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with Lustre; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #define DEBUG_SUBSYSTEM S_LLITE
26 #include <linux/lustre_dlm.h>
27 #include <linux/lustre_lite.h>
28 #include <linux/pagemap.h>
29 #include <linux/file.h>
30 #include <linux/lustre_acl.h>
31 #include <linux/lustre_sec.h>
32 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
33 #include <linux/lustre_compat25.h>
34 #endif
35 #include <linux/obd_lov.h>
36 #include <linux/lustre_audit.h>
37 #include "llite_internal.h"
38
39 __u64 lov_merge_size(struct lov_stripe_md *lsm, int kms);
40 __u64 lov_merge_blocks(struct lov_stripe_md *lsm);
41 __u64 lov_merge_mtime(struct lov_stripe_md *lsm, __u64 current_time);
42
43 int ll_validate_size(struct inode *inode, __u64 *size, __u64 *blocks)
44 {
45         ldlm_policy_data_t extent = { .l_extent = { 0, OBD_OBJECT_EOF } };
46         struct obd_export *exp = ll_i2sbi(inode)->ll_dt_exp;
47         struct ll_inode_info *lli = ll_i2info(inode);
48         struct lustre_handle match_lockh = {0};
49         int rc, flags;
50         ENTRY;
51
52         if (lli->lli_smd == NULL)
53                 RETURN(0);
54
55         LASSERT(size != NULL && blocks != NULL);
56
57         flags = LDLM_FL_TEST_LOCK | LDLM_FL_CBPENDING | LDLM_FL_BLOCK_GRANTED;
58         rc = obd_match(exp, lli->lli_smd, LDLM_EXTENT, &extent,
59                        LCK_PR | LCK_PW, &flags, inode, &match_lockh);
60         if (rc == 0) {
61                 /* we have no all needed locks,
62                  * so we don't know actual size */
63                 GOTO(finish, rc);
64         }
65
66         /* we know actual size! */
67         down(&lli->lli_size_sem);
68         *size = lov_merge_size(lli->lli_smd, 0);
69         *blocks = lov_merge_blocks(lli->lli_smd);
70         up(&lli->lli_size_sem);
71
72 finish:
73         RETURN(rc);
74 }
75
76 int ll_md_och_close(struct obd_export *md_exp, struct inode *inode,
77                     struct obd_client_handle *och, int dirty,
78                     __u64 epoch)
79 {
80         struct ptlrpc_request *req = NULL;
81         struct mdc_op_data *op_data;
82         struct obd_device *obd;
83         obd_valid valid;
84         int rc;
85         ENTRY;
86
87         obd = class_exp2obd(md_exp);
88         if (obd == NULL) {
89                 CERROR("Invalid MDC connection handle "LPX64"\n",
90                        md_exp->exp_handle.h_cookie);
91                 RETURN(0);
92         }
93
94         /* here we check if this is forced umount. If so this is called on
95          * canceling "open lock" and we do not call md_close() in this case , as
96          * it will not successful, as import is already deactivated. */
97         if (obd->obd_no_recov)
98                 GOTO(out, rc = 0);
99
100         /* prepare @op_data for close request */
101         OBD_ALLOC(op_data, sizeof(*op_data));
102         if (!op_data)
103                 RETURN(-ENOMEM);
104
105         memset(op_data, 0, sizeof(*op_data));
106
107         valid = (OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
108                  OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLEPOCH |
109                  OBD_MD_FLID);
110         
111         ll_inode2mdc_data(op_data, inode, valid);
112
113         if (0 /* ll_is_inode_dirty(inode) */) {
114                 op_data->flags = MDS_BFLAG_UNCOMMITTED_WRITES;
115                 op_data->valid |= OBD_MD_FLFLAGS;
116         }
117
118         if (dirty) {
119                 /* we modified data through this handle */
120                 op_data->io_epoch = epoch;
121                 op_data->flags |= MDS_BFLAG_DIRTY_EPOCH;
122                 op_data->valid |= OBD_MD_FLFLAGS | OBD_MD_FLEPOCH;
123                 if (ll_validate_size(inode, &op_data->size, &op_data->blocks))
124                         op_data->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
125         }
126
127         rc = md_close(md_exp, op_data, och, &req);
128         OBD_FREE(op_data, sizeof(*op_data));
129
130         if (rc == EAGAIN) {
131                 /* we are the last writer, so the MDS has instructed us to get
132                  * the file size and any write cookies, then close again. */
133
134                 //ll_queue_done_writing(inode);
135                 rc = 0;
136         } else if (rc) {
137                 CERROR("inode %lu mdc close failed: rc = %d\n",
138                        (unsigned long)inode->i_ino, rc);
139         }
140
141         ptlrpc_req_finished(req);
142         EXIT;
143 out:
144         mdc_clear_open_replay_data(md_exp, och);
145         och->och_fh.cookie = DEAD_HANDLE_MAGIC;
146         OBD_FREE(och, sizeof *och);
147         return rc;
148 }
149
150 int ll_md_real_close(struct obd_export *md_exp,
151                      struct inode *inode, int flags)
152 {
153         struct ll_inode_info *lli = ll_i2info(inode);
154         int freeing = inode->i_state & I_FREEING;
155         struct obd_client_handle **och_p;
156         struct obd_client_handle *och;
157         __u64 *och_usecount, epoch = 0;
158         int rc = 0, dirty = 0;
159         ENTRY;
160
161         if (flags & FMODE_WRITE) {
162                 och_p = &lli->lli_mds_write_och;
163                 och_usecount = &lli->lli_open_fd_write_count;
164         } else if (flags & FMODE_EXEC) {
165                 och_p = &lli->lli_mds_exec_och;
166                 och_usecount = &lli->lli_open_fd_exec_count;
167          } else {
168                 och_p = &lli->lli_mds_read_och;
169                 och_usecount = &lli->lli_open_fd_read_count;
170         }
171
172         down(&lli->lli_och_sem);
173         if (*och_usecount) { /* There are still users of this handle, so
174                                 skip freeing it. */
175                 up(&lli->lli_och_sem);
176                 RETURN(0);
177         }
178         if (ll_is_inode_dirty(inode)) {
179                 /* the inode still has dirty pages, let's close later */
180                 CDEBUG(D_INODE, "inode %lu/%u still has dirty pages\n",
181                        inode->i_ino, inode->i_generation);
182                 LASSERT(freeing == 0);
183                 ll_queue_done_writing(inode);
184                 up(&lli->lli_och_sem);
185                 RETURN(0);
186         }
187         
188         if (LLI_DIRTY_HANDLE(inode) && (flags & FMODE_WRITE)) {
189                 clear_bit(LLI_F_DIRTY_HANDLE,  &lli->lli_flags);
190                 dirty = 1;
191         } else if (0 && !(flags & FMODE_SYNC) && !freeing) {
192                 /* in order to speed up creation rate we pass
193                  * closing to dedicated thread so we don't need
194                  * to wait for close reply here -bzzz */
195                 ll_queue_done_writing(inode);
196                 up(&lli->lli_och_sem);
197                 RETURN(0);
198         }
199
200         och = *och_p;
201         *och_p = NULL;
202         if (flags & FMODE_WRITE) {
203                 epoch = lli->lli_io_epoch;
204                 lli->lli_io_epoch = 0;
205         }
206
207         up(&lli->lli_och_sem);
208
209         /*
210          * there might be a race and somebody have freed this och
211          * already. Another way to have this twice called is if file closing
212          * will fail due to netwok problems and on umount lock will be canceled
213          * and this will be called from block_ast callack.
214         */
215         if (och && och->och_fh.cookie != DEAD_HANDLE_MAGIC)
216                 rc = ll_md_och_close(md_exp, inode, och, dirty, epoch);
217         
218         RETURN(rc);
219 }
220
221 int ll_md_close(struct obd_export *md_exp, struct inode *inode,
222                 struct file *file)
223 {
224         struct ll_file_data *fd = file->private_data;
225         struct ll_inode_info *lli = ll_i2info(inode);
226         int rc = 0;
227         ENTRY;
228
229         /* clear group lock, if present */
230         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
231                 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
232                 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
233                 rc = ll_extent_unlock(fd, inode, lsm, LCK_GROUP,
234                                       &fd->fd_cwlockh);
235         }
236
237         /* Let's see if we have good enough OPEN lock on the file and if
238            we can skip talking to MDS */
239         if (file->f_dentry->d_inode) {
240                 int lockmode;
241                 struct obd_device *obddev;
242                 struct lustre_handle lockh;
243                 int flags = LDLM_FL_BLOCK_GRANTED;
244                 struct ldlm_res_id file_res_id = {.name = {id_fid(&lli->lli_id), 
245                                                            id_group(&lli->lli_id)}};
246                 ldlm_policy_data_t policy = {.l_inodebits={MDS_INODELOCK_OPEN}};
247
248                 down(&lli->lli_och_sem);
249                 if (fd->fd_omode & FMODE_WRITE) {
250                         lockmode = LCK_CW;
251                         LASSERT(lli->lli_open_fd_write_count);
252                         lli->lli_open_fd_write_count--;
253                 } else if (fd->fd_omode & FMODE_EXEC) {
254                         lockmode = LCK_PR;
255                         LASSERT(lli->lli_open_fd_exec_count);
256                         lli->lli_open_fd_exec_count--;
257                 } else {
258                         lockmode = LCK_CR;
259                         LASSERT(lli->lli_open_fd_read_count);
260                         lli->lli_open_fd_read_count--;
261                 }
262                 up(&lli->lli_och_sem);
263                 
264                 obddev = md_get_real_obd(md_exp, &lli->lli_id);
265                 if (!ldlm_lock_match(obddev->obd_namespace, flags, &file_res_id,
266                                      LDLM_IBITS, &policy, lockmode, &lockh))
267                 {
268                         rc = ll_md_real_close(md_exp, file->f_dentry->d_inode,
269                                               fd->fd_omode);
270                 } else {
271                         ldlm_lock_decref(&lockh, lockmode);
272                 }
273         }
274
275         file->private_data = NULL;
276         OBD_SLAB_FREE(fd, ll_file_data_slab, sizeof(*fd));
277         RETURN(rc);
278 }
279
280 /* While this returns an error code, fput() the caller does not, so we need
281  * to make every effort to clean up all of our state here.  Also, applications
282  * rarely check close errors and even if an error is returned they will not
283  * re-try the close call.
284  */
285 int ll_file_release(struct inode *inode, struct file *file)
286 {
287         struct ll_file_data *fd;
288         struct ll_sb_info *sbi = ll_i2sbi(inode);
289         int rc;
290
291         ENTRY;
292         CDEBUG(D_VFSTRACE, "VFS Op:inode="DLID4"(%p)\n",
293                OLID4(&ll_i2info(inode)->lli_id), inode);
294
295         /* don't do anything for / */
296         if (inode->i_sb->s_root == file->f_dentry)
297                 RETURN(0);
298
299         lprocfs_counter_incr(sbi->ll_stats, LPROC_LL_RELEASE);
300         fd = (struct ll_file_data *)file->private_data;
301         LASSERT(fd != NULL);
302
303         rc = ll_md_close(sbi->ll_md_exp, inode, file);
304         RETURN(rc);
305 }
306
307 static int ll_intent_file_open(struct file *file, void *lmm,
308                                int lmmsize, struct lookup_intent *itp)
309 {
310         struct ll_sb_info *sbi = ll_i2sbi(file->f_dentry->d_inode);
311         const char *name = (char *)file->f_dentry->d_name.name;
312         struct dentry *parent = file->f_dentry->d_parent;
313         const int len = file->f_dentry->d_name.len;
314         struct lustre_handle lockh;
315         struct mdc_op_data *op_data;
316         int rc;
317
318         if (!parent)
319                 RETURN(-ENOENT);
320
321         OBD_ALLOC(op_data, sizeof(*op_data));
322         if (op_data == NULL)
323                 RETURN(-ENOMEM);
324         
325         ll_prepare_mdc_data(op_data, parent->d_inode, NULL,
326                             name, len, O_RDWR);
327
328         rc = md_enqueue(sbi->ll_md_exp, LDLM_IBITS, itp, LCK_PR, op_data,
329                         &lockh, lmm, lmmsize, ldlm_completion_ast,
330                         ll_mdc_blocking_ast, NULL);
331         OBD_FREE(op_data, sizeof(*op_data));
332         if (rc == 0) {
333                 if (LUSTRE_IT(itp)->it_lock_mode)
334                         memcpy(&LUSTRE_IT(itp)->it_lock_handle,
335                                &lockh, sizeof(lockh));
336
337         } else if (rc < 0) {
338                 CERROR("lock enqueue: err: %d\n", rc);
339         }
340         RETURN(rc);
341 }
342
343 int ll_och_fill(struct inode *inode, struct lookup_intent *it,
344                  struct obd_client_handle *och)
345 {
346         struct ptlrpc_request *req = LUSTRE_IT(it)->it_data;
347         struct ll_inode_info *lli = ll_i2info(inode);
348         struct mds_body *body;
349         int rc = 0;
350         ENTRY;
351         LASSERT(och);
352
353         body = lustre_msg_buf (req->rq_repmsg, 1, sizeof (*body));
354         LASSERT (body != NULL);          /* reply already checked out */
355         LASSERT_REPSWABBED (req, 1);     /* and swabbed down */
356
357         memcpy(&och->och_fh, &body->handle, sizeof(body->handle));
358         och->och_magic = OBD_CLIENT_HANDLE_MAGIC;
359         if (it->it_flags & FMODE_WRITE) {
360                 if (lli->lli_io_epoch && lli->lli_io_epoch != body->io_epoch)
361                         CDEBUG(D_ERROR, "new epoch?! "LPD64" != "LPD64"\n",
362                                lli->lli_io_epoch, body->io_epoch);
363                 lli->lli_io_epoch = body->io_epoch;
364         }
365         mdc_set_open_replay_data(ll_i2mdexp(inode), och, 
366                                  LUSTRE_IT(it)->it_data);
367
368         RETURN(rc);
369 }
370
371 int ll_local_open(struct file *file, struct lookup_intent *it,
372                   struct obd_client_handle *och)
373 {
374         struct ll_file_data *fd;
375         struct inode *inode = file->f_dentry->d_inode;
376         int rc = 0;
377         ENTRY;
378
379         if (och) {
380                 rc = ll_och_fill(inode, it, och);
381                 if (rc)
382                         RETURN(rc);
383         }
384
385         LASSERTF(file->private_data == NULL, "file %.*s/%.*s ino %lu/%u (%o)\n",
386                  file->f_dentry->d_name.len, file->f_dentry->d_name.name,
387                  file->f_dentry->d_parent->d_name.len,
388                  file->f_dentry->d_parent->d_name.name,
389                  file->f_dentry->d_inode->i_ino,
390                  file->f_dentry->d_inode->i_generation,
391                  file->f_dentry->d_inode->i_mode);
392
393         OBD_SLAB_ALLOC(fd, ll_file_data_slab, SLAB_KERNEL, sizeof *fd);
394         
395         /* We can't handle this well without reorganizing ll_file_open and
396          * ll_md_close(), so don't even try right now. */
397         LASSERT(fd != NULL);
398
399         file->private_data = fd;
400         ll_readahead_init(inode, &fd->fd_ras);
401         fd->fd_omode = it->it_flags;
402
403         RETURN(rc);
404 }
405
406 /* Open a file, and (for the very first open) create objects on the OSTs at
407  * this time.  If opened with O_LOV_DELAY_CREATE, then we don't do the object
408  * creation or open until ll_lov_setstripe() ioctl is called.  We grab
409  * lli_open_sem to ensure no other process will create objects, send the
410  * stripe MD to the MDS, or try to destroy the objects if that fails.
411  *
412  * If we already have the stripe MD locally then we don't request it in
413  * mdc_open(), by passing a lmm_size = 0.
414  *
415  * It is up to the application to ensure no other processes open this file
416  * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
417  * used.  We might be able to avoid races of that sort by getting lli_open_sem
418  * before returning in the O_LOV_DELAY_CREATE case and dropping it here
419  * or in ll_file_release(), but I'm not sure that is desirable/necessary.
420  */
421 int ll_file_open(struct inode *inode, struct file *file)
422 {
423         struct ll_inode_info *lli = ll_i2info(inode);
424         struct lookup_intent *it, oit = { .it_op = IT_OPEN,
425                                           .it_flags = file->f_flags };
426         struct lov_stripe_md *lsm;
427         struct ptlrpc_request *req;
428         int rc = 0;
429         struct obd_client_handle **och_p = NULL;
430         __u64 *och_usecount = NULL;
431         ENTRY;
432
433         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), flags %o\n",
434                inode->i_ino, inode->i_generation, inode, file->f_flags);
435
436         /* don't do anything for / */
437         if (inode->i_sb->s_root == file->f_dentry)
438                 RETURN(0);
439
440         if ((file->f_flags+1) & O_ACCMODE)
441                 oit.it_flags++;
442         if (file->f_flags & O_TRUNC)
443                 oit.it_flags |= 2;
444
445         it = file->f_it;
446         
447         /* sometimes LUSTRE_IT(it) may not be allocated like opening file by
448          * dentry_open() from GNS stuff. */
449         if (!it || !LUSTRE_IT(it)) {
450                 it = &oit;
451                 rc = ll_intent_alloc(it);
452                 if (rc)
453                         GOTO(out, rc);
454         }
455
456         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_OPEN);
457         
458         /* mdc_intent_lock() didn't get a request ref if there was an open
459          * error, so don't do cleanup on the * request here (bug 3430) */
460         if (LUSTRE_IT(it)->it_disposition) {
461                 rc = it_open_error(DISP_OPEN_OPEN, it);
462                 if (rc)
463                         RETURN(rc);
464         }
465
466         /* Let's see if we have file open on MDS already. */
467         if (it->it_flags & FMODE_WRITE) {
468                 och_p = &lli->lli_mds_write_och;
469                 och_usecount = &lli->lli_open_fd_write_count;
470         } else if (it->it_flags & FMODE_EXEC) {
471                 och_p = &lli->lli_mds_exec_och;
472                 och_usecount = &lli->lli_open_fd_exec_count;
473         } else {
474                 och_p = &lli->lli_mds_read_och;
475                 och_usecount = &lli->lli_open_fd_read_count;
476         }
477
478         rc = ll_crypto_decrypt_key(inode, it);
479         if (rc)
480                 GOTO(out, rc);
481
482         down(&lli->lli_och_sem);
483         if (*och_p) { /* Open handle is present */
484                 if (it_disposition(it, DISP_LOOKUP_POS) && /* Positive lookup */
485                     it_disposition(it, DISP_OPEN_OPEN)) { /* & OPEN happened */
486                         struct obd_client_handle *och;
487                         /* Well, there's extra open request that we do not need,
488                            let's close it somehow*/
489                         OBD_ALLOC(och, sizeof (struct obd_client_handle));
490                         if (!och) {
491                                 up(&lli->lli_och_sem);
492                                 RETURN(-ENOMEM);
493                         }
494
495                         rc = ll_och_fill(inode, it, och);
496                         if (rc) {
497                                 up(&lli->lli_och_sem);
498                                 RETURN(rc);
499                         }
500                         
501                         /* ll_md_och_close() will free och */
502                         ll_md_och_close(ll_i2mdexp(inode), inode, och, 0, 0);
503                 }
504                 (*och_usecount)++;
505                         
506                 rc = ll_local_open(file, it, NULL);
507                 if (rc)
508                         LBUG();
509
510                 rc = ll_set_capa(inode, it, *och_p);
511                 if (rc) {
512                         up(&lli->lli_och_sem);
513                         RETURN(rc);
514                 }
515         } else {
516                 LASSERT(*och_usecount == 0);
517                 OBD_ALLOC(*och_p, sizeof (struct obd_client_handle));
518                 if (!*och_p)
519                         GOTO(out, rc = -ENOMEM);
520                 (*och_usecount)++;
521
522                 if (!it || !LUSTRE_IT(it) || !LUSTRE_IT(it)->it_disposition) {
523                         /*
524                          * we are going to replace intent here, and that may
525                          * possibly change access mode (FMODE_EXEC can only be
526                          * set in intent), but I hope it never happens (I was
527                          * not able to trigger it yet at least) -- green
528                          */
529                         
530                         /* FIXME: FMODE_EXEC is not covered by O_ACCMODE! */
531                         LASSERT(!(it->it_flags & FMODE_EXEC));
532                         LASSERTF((it->it_flags & O_ACCMODE) ==
533                                  (oit.it_flags & O_ACCMODE), "Changing intent "
534                                  "flags %x to incompatible %x\n", it->it_flags,
535                                  oit.it_flags);
536                         it = &oit;
537                         rc = ll_intent_file_open(file, NULL, 0, it);
538                         if (rc)
539                                 GOTO(out, rc);
540                         rc = it_open_error(DISP_OPEN_OPEN, it);
541                         if (rc)
542                                 GOTO(out_och_free, rc);
543
544                         mdc_set_lock_data(NULL, &LUSTRE_IT(it)->it_lock_handle,
545                                           file->f_dentry->d_inode);
546                 }
547                 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_OPEN);
548                 rc = ll_local_open(file, it, *och_p);
549                 LASSERTF(rc == 0, "rc = %d\n", rc);
550
551                 rc = ll_set_capa(inode, it, *och_p);
552                 if (rc) {
553                         up(&lli->lli_och_sem);
554                         RETURN(rc);
555                 }
556         }
557         up(&lli->lli_och_sem);
558         
559         /*
560          * must do this outside lli_och_sem lock to prevent deadlock where
561          * different kind of OPEN lock for this same inode gets cancelled by
562          * ldlm_cancel_lru
563          */
564         if (!S_ISREG(inode->i_mode))
565                 GOTO(out, rc);
566
567         lsm = lli->lli_smd;
568         if (lsm == NULL) {
569                 if (file->f_flags & O_LOV_DELAY_CREATE ||
570                     !(file->f_mode & FMODE_WRITE)) {
571                         CDEBUG(D_INODE, "object creation was delayed\n");
572                         GOTO(out, rc);
573                 }
574         }
575         file->f_flags &= ~O_LOV_DELAY_CREATE;
576         GOTO(out, rc);
577  out:
578         /* audit stuff if there was no RPC */
579         if (LUSTRE_IT(it)->it_data == 0)
580                 ll_audit_log(inode, AUDIT_OPEN, rc);
581
582         req = LUSTRE_IT(it)->it_data;
583         ll_intent_drop_lock(it);
584         ll_intent_release(it);
585
586         if (req)
587                 ptlrpc_req_finished(req);
588         if (rc == 0) {
589                 ll_open_complete(inode);
590         } else {
591 out_och_free:
592                 if (*och_p) {
593                         OBD_FREE(*och_p, sizeof (struct obd_client_handle));
594                         *och_p = NULL; /* OBD_FREE writes some magic there */
595                         (*och_usecount)--;
596                 }
597                 up(&lli->lli_och_sem);
598         }
599                 
600         return rc;
601 }
602
603 /* Fills the obdo with the attributes for the inode defined by lsm */
604 int ll_lsm_getattr(struct obd_export *exp, struct lov_stripe_md *lsm,
605                    struct obdo *oa)
606 {
607         struct ptlrpc_request_set *set;
608         int rc;
609         ENTRY;
610
611         LASSERT(lsm != NULL);
612
613         memset(oa, 0, sizeof *oa);
614         oa->o_id = lsm->lsm_object_id;
615         oa->o_gr = lsm->lsm_object_gr;
616         oa->o_mode = S_IFREG;
617         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
618                 OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
619                 OBD_MD_FLCTIME | OBD_MD_FLGROUP;
620
621         set = ptlrpc_prep_set();
622         if (set == NULL) {
623                 rc = -ENOMEM;
624         } else {
625                 rc = obd_getattr_async(exp, oa, lsm, set);
626                 if (rc == 0)
627                         rc = ptlrpc_set_wait(set);
628                 ptlrpc_set_destroy(set);
629         }
630         if (rc)
631                 RETURN(rc);
632
633         oa->o_valid &= (OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
634                         OBD_MD_FLCTIME | OBD_MD_FLSIZE);
635         RETURN(0);
636 }
637
638 static inline void ll_remove_suid(struct inode *inode)
639 {
640         unsigned int mode;
641
642         /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
643         mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
644
645         /* was any of the uid bits set? */
646         mode &= inode->i_mode;
647         if (mode && !capable(CAP_FSETID)) {
648                 inode->i_mode &= ~mode;
649                 // XXX careful here - we cannot change the size
650         }
651 }
652
653 static int ll_lock_to_stripe_offset(struct inode *inode, struct ldlm_lock *lock)
654 {
655         struct ll_inode_info *lli = ll_i2info(inode);
656         struct lov_stripe_md *lsm = lli->lli_smd;
657         struct obd_export *exp = ll_i2dtexp(inode);
658         struct {
659                 char name[16];
660                 struct ldlm_lock *lock;
661                 struct lov_stripe_md *lsm;
662         } key = { .name = "lock_to_stripe", .lock = lock, .lsm = lsm };
663         __u32 stripe, vallen = sizeof(stripe);
664         int rc;
665         ENTRY;
666
667         if (lsm->lsm_stripe_count == 1)
668                 GOTO(check, stripe = 0);
669
670         /* get our offset in the lov */
671         rc = obd_get_info(exp, sizeof(key), &key, &vallen, &stripe);
672         if (rc != 0) {
673                 CERROR("obd_get_info: rc = %d\n", rc);
674                 RETURN(rc);
675         }
676         LASSERT(stripe < lsm->lsm_stripe_count);
677         EXIT;
678 check:
679         if (lsm->lsm_oinfo[stripe].loi_id != lock->l_resource->lr_name.name[0]||
680             lsm->lsm_oinfo[stripe].loi_gr != lock->l_resource->lr_name.name[2]){
681                 LDLM_ERROR(lock, "resource doesn't match object "LPU64"/"LPU64
682                            " inode=%lu/%u (%p)\n",
683                            lsm->lsm_oinfo[stripe].loi_id,
684                            lsm->lsm_oinfo[stripe].loi_gr,
685                            inode->i_ino, inode->i_generation, inode);
686                 return -ELDLM_NO_LOCK_DATA;
687         }
688
689         return stripe;
690 }
691
692 /* Flush the page cache for an extent as its canceled.  When we're on an LOV,
693  * we get a lock cancellation for each stripe, so we have to map the obd's
694  * region back onto the stripes in the file that it held.
695  *
696  * No one can dirty the extent until we've finished our work and they can
697  * enqueue another lock.  The DLM protects us from ll_file_read/write here,
698  * but other kernel actors could have pages locked.
699  *
700  * Called with the DLM lock held. */
701 void ll_pgcache_remove_extent(struct inode *inode, struct lov_stripe_md *lsm,
702                               struct ldlm_lock *lock, __u32 stripe)
703 {
704         ldlm_policy_data_t tmpex;
705         unsigned long start, end, count, skip, i, j;
706         struct page *page;
707         int rc, rc2, discard = lock->l_flags & LDLM_FL_DISCARD_DATA;
708         struct lustre_handle lockh;
709         ENTRY;
710
711         memcpy(&tmpex, &lock->l_policy_data, sizeof(tmpex));
712         CDEBUG(D_INODE|D_PAGE, "inode %lu(%p) ["LPU64"->"LPU64"] size: %llu\n",
713                inode->i_ino, inode, tmpex.l_extent.start, tmpex.l_extent.end,
714                inode->i_size);
715
716         /* our locks are page granular thanks to osc_enqueue, we invalidate the
717          * whole page. */
718         LASSERT((tmpex.l_extent.start & ~PAGE_CACHE_MASK) == 0);
719         LASSERT(((tmpex.l_extent.end + 1) & ~PAGE_CACHE_MASK) == 0);
720
721         count = ~0;
722         skip = 0;
723         start = tmpex.l_extent.start >> PAGE_CACHE_SHIFT;
724         end = tmpex.l_extent.end >> PAGE_CACHE_SHIFT;
725         if (lsm->lsm_stripe_count > 1) {
726                 count = lsm->lsm_stripe_size >> PAGE_CACHE_SHIFT;
727                 skip = (lsm->lsm_stripe_count - 1) * count;
728                 start += start/count * skip + stripe * count;
729                 if (end != ~0)
730                         end += end/count * skip + stripe * count;
731         }
732         if (end < tmpex.l_extent.end >> PAGE_CACHE_SHIFT)
733                 end = ~0;
734
735         i = inode->i_size ? (inode->i_size - 1) >> PAGE_CACHE_SHIFT : 0;
736         if (i < end)
737                 end = i;
738
739         CDEBUG(D_INODE|D_PAGE, "walking page indices start: %lu j: %lu "
740                "count: %lu skip: %lu end: %lu%s\n", start, start % count,
741                count, skip, end, discard ? " (DISCARDING)" : "");
742         
743         /* walk through the vmas on the inode and tear down mmaped pages that
744          * intersect with the lock.  this stops immediately if there are no
745          * mmap()ed regions of the file.  This is not efficient at all and
746          * should be short lived. We'll associate mmap()ed pages with the lock
747          * and will be able to find them directly */
748         
749         for (i = start; i <= end; i += (j + skip)) {
750                 j = min(count - (i % count), end - i + 1);
751                 LASSERT(j > 0);
752                 LASSERT(inode->i_mapping);
753                 if (ll_teardown_mmaps(inode->i_mapping, 
754                                       (__u64)i << PAGE_CACHE_SHIFT,
755                                       ((__u64)(i+j) << PAGE_CACHE_SHIFT) - 1) )
756                         break;
757         }
758
759         /* this is the simplistic implementation of page eviction at
760          * cancelation.  It is careful to get races with other page
761          * lockers handled correctly.  fixes from bug 20 will make it
762          * more efficient by associating locks with pages and with
763          * batching writeback under the lock explicitly. */
764         for (i = start, j = start % count; i <= end;
765              j++, i++, tmpex.l_extent.start += PAGE_CACHE_SIZE) {
766                 if (j == count) {
767                         CDEBUG(D_PAGE, "skip index %lu to %lu\n", i, i + skip);
768                         i += skip;
769                         j = 0;
770                         if (i > end)
771                                 break;
772                 }
773                 LASSERTF(tmpex.l_extent.start< lock->l_policy_data.l_extent.end,
774                          LPU64" >= "LPU64" start %lu i %lu end %lu\n",
775                          tmpex.l_extent.start, lock->l_policy_data.l_extent.end,
776                          start, i, end);
777
778                 if (!mapping_has_pages(inode->i_mapping)) {
779                         CDEBUG(D_INODE|D_PAGE, "nothing left\n");
780                         break;
781                 }
782
783                 cond_resched();
784
785                 page = find_get_page(inode->i_mapping, i);
786                 if (page == NULL)
787                         continue;
788                 LL_CDEBUG_PAGE(D_PAGE, page, "lock page idx %lu ext "LPU64"\n",
789                                i, tmpex.l_extent.start);
790                 lock_page(page);
791
792                 /* page->mapping to check with racing against teardown */
793                 if (!discard && clear_page_dirty_for_io(page)) {
794                         rc = ll_call_writepage(inode, page);
795                         if (rc != 0)
796                                 CERROR("writepage of page %p failed: %d\n",
797                                        page, rc);
798                         /* either waiting for io to complete or reacquiring
799                          * the lock that the failed writepage released */
800                         lock_page(page);
801                 }
802
803                 tmpex.l_extent.end = tmpex.l_extent.start + PAGE_CACHE_SIZE - 1;
804                 /* check to see if another DLM lock covers this page */
805                 rc2 = ldlm_lock_match(lock->l_resource->lr_namespace,
806                                       LDLM_FL_BLOCK_GRANTED|LDLM_FL_CBPENDING |
807                                       LDLM_FL_TEST_LOCK,
808                                       &lock->l_resource->lr_name, LDLM_EXTENT,
809                                       &tmpex, LCK_PR | LCK_PW, &lockh);
810                 if (rc2 == 0 && page->mapping != NULL) {
811                         // checking again to account for writeback's lock_page()
812                         LL_CDEBUG_PAGE(D_PAGE, page, "truncating\n");
813                         ll_ra_accounting(page, inode->i_mapping);
814                         ll_truncate_complete_page(page);
815                 }
816                 unlock_page(page);
817                 page_cache_release(page);
818         }
819         LASSERTF(tmpex.l_extent.start <=
820                  (lock->l_policy_data.l_extent.end == ~0ULL ? ~0ULL :
821                   lock->l_policy_data.l_extent.end + 1),
822                  "loop too long "LPU64" > "LPU64" start %lu i %lu end %lu\n",
823                  tmpex.l_extent.start, lock->l_policy_data.l_extent.end,
824                  start, i, end);
825         EXIT;
826 }
827
828 static int ll_extent_lock_callback(struct ldlm_lock *lock,
829                                    struct ldlm_lock_desc *new, void *data,
830                                    int flag)
831 {
832         struct lustre_handle lockh = { 0 };
833         int rc;
834         ENTRY;
835
836         if ((unsigned long)data > 0 && (unsigned long)data < 0x1000) {
837                 LDLM_ERROR(lock, "cancelling lock with bad data %p", data);
838                 LBUG();
839         }
840
841         switch (flag) {
842         case LDLM_CB_BLOCKING:
843                 ldlm_lock2handle(lock, &lockh);
844                 rc = ldlm_cli_cancel(&lockh);
845                 if (rc != ELDLM_OK)
846                         CERROR("ldlm_cli_cancel failed: %d\n", rc);
847                 break;
848         case LDLM_CB_CANCELING: {
849                 struct inode *inode;
850                 struct ll_inode_info *lli;
851                 struct lov_stripe_md *lsm;
852                 __u32 stripe;
853                 __u64 kms;
854
855                 /* This lock wasn't granted, don't try to evict pages */
856                 if (lock->l_req_mode != lock->l_granted_mode)
857                         RETURN(0);
858
859                 inode = ll_inode_from_lock(lock);
860                 if (inode == NULL)
861                         RETURN(0);
862                 lli = ll_i2info(inode);
863                 if (lli == NULL)
864                         goto iput;
865                 if (lli->lli_smd == NULL)
866                         goto iput;
867                 lsm = lli->lli_smd;
868
869                 stripe = ll_lock_to_stripe_offset(inode, lock);
870                 if (stripe < 0) {
871                         CERROR("ll_lock_to_stripe_offset failed: %d\n", stripe);
872                         goto iput;
873                 }
874
875                 ll_pgcache_remove_extent(inode, lsm, lock, stripe);
876
877                 down(&lli->lli_size_sem);
878                 lock_res_and_lock(lock);
879                 kms = ldlm_extent_shift_kms(lock,
880                                             lsm->lsm_oinfo[stripe].loi_kms);
881                 
882                 if (lsm->lsm_oinfo[stripe].loi_kms != kms)
883                         LDLM_DEBUG(lock, "updating kms from "LPU64" to "LPU64,
884                                    lsm->lsm_oinfo[stripe].loi_kms, kms);
885                 lsm->lsm_oinfo[stripe].loi_kms = kms;
886                 unlock_res_and_lock(lock);
887                 up(&lli->lli_size_sem);
888                 //ll_try_done_writing(inode);
889         iput:
890                 iput(inode);
891                 break;
892         }
893         default:
894                 LBUG();
895         }
896
897         RETURN(0);
898 }
899
900 #if 0
901 int ll_async_completion_ast(struct ldlm_lock *lock, int flags, void *data)
902 {
903         /* XXX ALLOCATE - 160 bytes */
904         struct inode *inode = ll_inode_from_lock(lock);
905         struct ll_inode_info *lli = ll_i2info(inode);
906         struct lustre_handle lockh = { 0 };
907         struct ost_lvb *lvb;
908         __u32 stripe;
909         ENTRY;
910
911         if (flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
912                      LDLM_FL_BLOCK_CONV)) {
913                 LBUG(); /* not expecting any blocked async locks yet */
914                 LDLM_DEBUG(lock, "client-side async enqueue returned a blocked "
915                            "lock, returning");
916                 ldlm_lock_dump(D_OTHER, lock, 0);
917                 ldlm_reprocess_all(lock->l_resource);
918                 RETURN(0);
919         }
920
921         LDLM_DEBUG(lock, "client-side async enqueue: granted/glimpsed");
922
923         stripe = ll_lock_to_stripe_offset(inode, lock);
924         if (stripe < 0) {
925                 CERROR("ll_lock_to_stripe_offset failed: %d\n", stripe);
926                 goto iput;
927         }
928
929         if (lock->l_lvb_len) {
930                 struct lov_stripe_md *lsm = lli->lli_smd;
931                 __u64 kms;
932                 lvb = lock->l_lvb_data;
933                 lsm->lsm_oinfo[stripe].loi_rss = lvb->lvb_size;
934
935                 down(&inode->i_sem);
936                 lock_res_and_lock(lock);
937                 kms = MAX(lsm->lsm_oinfo[stripe].loi_kms, lvb->lvb_size);
938                 kms = ldlm_extent_shift_kms(NULL, kms);
939                 if (lsm->lsm_oinfo[stripe].loi_kms != kms)
940                         LDLM_DEBUG(lock, "updating kms from "LPU64" to "LPU64,
941                                    lsm->lsm_oinfo[stripe].loi_kms, kms);
942                 lsm->lsm_oinfo[stripe].loi_kms = kms;
943                 unlock_res_and_lock(lock);
944                 up(&inode->i_sem);
945         }
946
947 iput:
948         iput(inode);
949         wake_up(&lock->l_waitq);
950
951         ldlm_lock2handle(lock, &lockh);
952         ldlm_lock_decref(&lockh, LCK_PR);
953         RETURN(0);
954 }
955 #endif
956
957 static int ll_glimpse_callback(struct ldlm_lock *lock, void *reqp)
958 {
959         struct ptlrpc_request *req = reqp;
960         struct inode *inode = ll_inode_from_lock(lock);
961         struct ll_inode_info *lli;
962         struct ost_lvb *lvb;
963         struct lov_stripe_md *lsm;
964         int rc, size = sizeof(*lvb), stripe;
965         ENTRY;
966
967         if (inode == NULL)
968                 GOTO(out, rc = -ELDLM_NO_LOCK_DATA);
969         lli = ll_i2info(inode);
970         if (lli == NULL)
971                 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
972
973         lsm = lli->lli_smd;
974         if (lsm == NULL)
975                 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
976
977         /* First, find out which stripe index this lock corresponds to. */
978         stripe = ll_lock_to_stripe_offset(inode, lock);
979         if (stripe < 0) {
980                 CERROR("ll_lock_to_stripe_offset failed: %d\n", stripe);
981                 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
982         }
983
984         rc = lustre_pack_reply(req, 1, &size, NULL);
985         if (rc) {
986                 CERROR("lustre_pack_reply: %d\n", rc);
987                 GOTO(iput, rc);
988         }
989
990         lvb = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*lvb));
991         lvb->lvb_size = lli->lli_smd->lsm_oinfo[stripe].loi_kms;
992         lvb->lvb_mtime = LTIME_S(inode->i_mtime);
993         lvb->lvb_atime = LTIME_S(inode->i_atime);
994         lvb->lvb_ctime = LTIME_S(inode->i_ctime);
995
996         LDLM_DEBUG(lock, "i_size: %llu -> stripe number %u -> kms "LPU64,
997                    inode->i_size, stripe, lvb->lvb_size);
998         GOTO(iput, 0);
999  iput:
1000         iput(inode);
1001
1002  out:
1003         /* These errors are normal races, so we don't want to fill the console
1004          * with messages by calling ptlrpc_error() */
1005         if (rc == -ELDLM_NO_LOCK_DATA)
1006                 lustre_pack_reply(req, 0, NULL, NULL);
1007
1008         req->rq_status = rc;
1009         return rc;
1010 }
1011
1012 /* NB: lov_merge_size will prefer locally cached writes if they extend the
1013  * file (because it prefers KMS over RSS when larger) */
1014 int ll_glimpse_size(struct inode *inode)
1015 {
1016         struct ll_inode_info *lli = ll_i2info(inode);
1017         struct ll_sb_info *sbi = ll_i2sbi(inode);
1018         ldlm_policy_data_t policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
1019         struct lustre_handle lockh = { 0 };
1020         int rc, flags = LDLM_FL_HAS_INTENT;
1021         ENTRY;
1022
1023         CDEBUG(D_DLMTRACE, "Glimpsing inode %lu\n", inode->i_ino);
1024
1025         rc = obd_enqueue(sbi->ll_dt_exp, lli->lli_smd, LDLM_EXTENT, &policy,
1026                          LCK_PR, &flags, ll_extent_lock_callback,
1027                          ldlm_completion_ast, ll_glimpse_callback, inode,
1028                          sizeof(struct ost_lvb), lustre_swab_ost_lvb, &lockh);
1029         if (rc == -ENOENT)
1030                 RETURN(rc);
1031
1032         if (rc != 0) {
1033                 CERROR("obd_enqueue returned rc %d, returning -EIO\n", rc);
1034                 RETURN(rc > 0 ? -EIO : rc);
1035         }
1036
1037         down(&lli->lli_size_sem);
1038         inode->i_size = lov_merge_size(lli->lli_smd, 0);
1039         inode->i_blocks = lov_merge_blocks(lli->lli_smd);
1040         up(&lli->lli_size_sem);
1041
1042         LTIME_S(inode->i_mtime) = lov_merge_mtime(lli->lli_smd,
1043                                                   LTIME_S(inode->i_mtime));
1044
1045         CDEBUG(D_DLMTRACE, "glimpse: size: "LPU64", blocks: "LPU64"\n",
1046                (__u64)inode->i_size, (__u64)inode->i_blocks);
1047         
1048         obd_cancel(sbi->ll_dt_exp, lli->lli_smd, LCK_PR, &lockh);
1049         RETURN(rc);
1050 }
1051
1052 void ll_stime_record(struct ll_sb_info *sbi, struct timeval *start,
1053                     struct obd_service_time *stime)
1054 {
1055         struct timeval stop;
1056         do_gettimeofday(&stop);
1057                                                                                                                                                                                                      
1058         spin_lock(&sbi->ll_lock);
1059         lprocfs_stime_record(stime, &stop, start);
1060         spin_unlock(&sbi->ll_lock);
1061 }
1062
1063 int ll_extent_lock(struct ll_file_data *fd, struct inode *inode,
1064                    struct lov_stripe_md *lsm, int mode,
1065                    ldlm_policy_data_t *policy, struct lustre_handle *lockh,
1066                    int ast_flags, struct obd_service_time *stime)
1067 {
1068         struct ll_inode_info *lli = ll_i2info(inode);
1069         struct ll_sb_info *sbi = ll_i2sbi(inode);
1070         struct timeval start;
1071         int rc;
1072         ENTRY;
1073
1074         LASSERT(lockh->cookie == 0);
1075
1076         /* XXX phil: can we do this?  won't it screw the file size up? */
1077         if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
1078             (sbi->ll_flags & LL_SBI_NOLCK))
1079                 RETURN(0);
1080
1081         CDEBUG(D_DLMTRACE, "Locking inode %lu, start "LPU64" end "LPU64"\n",
1082                inode->i_ino, policy->l_extent.start, policy->l_extent.end);
1083
1084         do_gettimeofday(&start);        
1085         rc = obd_enqueue(sbi->ll_dt_exp, lsm, LDLM_EXTENT, policy, mode,
1086                          &ast_flags, ll_extent_lock_callback,
1087                          ldlm_completion_ast, ll_glimpse_callback, inode,
1088                          sizeof(struct ost_lvb), lustre_swab_ost_lvb, lockh);
1089         if (rc > 0)
1090                 rc = -EIO;
1091         
1092         ll_stime_record(sbi, &start, stime);
1093
1094         if (policy->l_extent.start == 0 &&
1095             policy->l_extent.end == OBD_OBJECT_EOF) {
1096                 /* vmtruncate()->ll_truncate() first sets the i_size and then
1097                  * the kms under both a DLM lock and the i_sem.  If we don't
1098                  * get the i_sem here we can match the DLM lock and reset
1099                  * i_size from the kms before the truncating path has updated
1100                  * the kms.  generic_file_write can then trust the stale i_size
1101                  * when doing appending writes and effectively cancel the
1102                  * result of the truncate.  Getting the i_sem after the enqueue
1103                  * maintains the DLM -> i_sem acquiry order. */
1104                 down(&lli->lli_size_sem);
1105                 inode->i_size = lov_merge_size(lsm, 1);
1106                 up(&lli->lli_size_sem);
1107         }
1108         
1109         if (rc == 0) {
1110                 LTIME_S(inode->i_mtime) =
1111                         lov_merge_mtime(lsm, LTIME_S(inode->i_mtime));
1112         }
1113
1114         RETURN(rc);
1115 }
1116
1117 int ll_extent_unlock(struct ll_file_data *fd, struct inode *inode,
1118                      struct lov_stripe_md *lsm, int mode,
1119                      struct lustre_handle *lockh)
1120 {
1121         struct ll_sb_info *sbi = ll_i2sbi(inode);
1122         int rc;
1123         ENTRY;
1124
1125         /* XXX phil: can we do this?  won't it screw the file size up? */
1126         if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
1127             (sbi->ll_flags & LL_SBI_NOLCK))
1128                 RETURN(0);
1129
1130         rc = obd_cancel(sbi->ll_dt_exp, lsm, mode, lockh);
1131
1132         RETURN(rc);
1133 }
1134
1135 static ssize_t ll_file_read(struct file *file, char *buf, size_t count,
1136                             loff_t *ppos)
1137 {
1138         struct inode *inode = file->f_dentry->d_inode;
1139         struct ll_inode_info *lli = ll_i2info(inode);
1140         struct lov_stripe_md *lsm = lli->lli_smd;
1141         struct ll_lock_tree tree;
1142         struct ll_lock_tree_node *node;
1143         int rc;
1144         ssize_t retval;
1145         __u64 kms;
1146         ENTRY;
1147         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),size="LPSZ",offset=%Ld\n",
1148                inode->i_ino, inode->i_generation, inode, count, *ppos);
1149
1150         /* "If nbyte is 0, read() will return 0 and have no other results."
1151          *                      -- Single Unix Spec */
1152         if (count == 0)
1153                 RETURN(0);
1154
1155         lprocfs_counter_add(ll_i2sbi(inode)->ll_stats, LPROC_LL_READ_BYTES,
1156                             count);
1157
1158         if (!lsm)
1159                 RETURN(0);
1160
1161         node = ll_node_from_inode(inode, *ppos, *ppos  + count - 1,
1162                                   LCK_PR);
1163
1164         tree.lt_fd = file->private_data;
1165         
1166         rc = ll_tree_lock(&tree, node, inode, buf, count,
1167                           file->f_flags & O_NONBLOCK ? LDLM_FL_BLOCK_NOWAIT :0);
1168         if (rc != 0)
1169                 RETURN(rc);
1170
1171         down(&lli->lli_size_sem);
1172         kms = lov_merge_size(lsm, 1);
1173         if (*ppos + count - 1 > kms) {
1174                 /* A glimpse is necessary to determine whether we return a short
1175                  * read or some zeroes at the end of the buffer */
1176                 up(&lli->lli_size_sem);
1177                 retval = ll_glimpse_size(inode);
1178                 if (retval)
1179                         goto out;
1180         } else {
1181                 inode->i_size = kms;
1182                 up(&lli->lli_size_sem);
1183         }
1184
1185         CDEBUG(D_INFO, "Read ino %lu, "LPSZ" bytes, offset %lld, i_size %llu\n",
1186                inode->i_ino, count, *ppos, inode->i_size);
1187         
1188         /* turn off the kernel's read-ahead */
1189 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1190         file->f_ramax = 0;
1191 #else
1192         file->f_ra.ra_pages = 0;
1193 #endif
1194         retval = generic_file_read(file, buf, count, ppos);
1195         ll_audit_log(inode, AUDIT_READ, retval);
1196
1197  out:
1198         ll_tree_unlock(&tree, inode);
1199         RETURN(retval);
1200 }
1201
1202 /*
1203  * Write to a file (through the page cache).
1204  */
1205 static ssize_t ll_file_write(struct file *file, const char *buf,
1206                              size_t count, loff_t *ppos)
1207 {
1208         struct inode *inode = file->f_dentry->d_inode;
1209         loff_t maxbytes = ll_file_maxbytes(inode);
1210         struct ll_lock_tree tree;
1211         struct ll_lock_tree_node *node;
1212         ssize_t retval;
1213         int rc;
1214
1215         ENTRY;
1216         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),size="LPSZ",offset=%Ld\n",
1217                inode->i_ino, inode->i_generation, inode, count, *ppos);
1218
1219         SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
1220
1221         /* POSIX, but surprised the VFS doesn't check this already */
1222         if (count == 0)
1223                 RETURN(0);
1224
1225         /* If file was opened for LL_IOC_LOV_SETSTRIPE but the ioctl wasn't
1226          * called on the file, don't fail the below assertion (bug 2388). */
1227         if (file->f_flags & O_LOV_DELAY_CREATE &&
1228             ll_i2info(inode)->lli_smd == NULL)
1229                 RETURN(-EBADF);
1230
1231         LASSERT(ll_i2info(inode)->lli_smd != NULL);
1232         
1233         if (file->f_flags & O_APPEND)
1234                 node = ll_node_from_inode(inode, 0, OBD_OBJECT_EOF, LCK_PW);
1235         else
1236                 node = ll_node_from_inode(inode, *ppos, *ppos  + count - 1,
1237                                           LCK_PW);
1238
1239         if (IS_ERR(node))
1240                 RETURN(PTR_ERR(node));
1241
1242         tree.lt_fd = file->private_data;
1243         
1244         rc = ll_tree_lock(&tree, node, inode, buf, count,
1245                           file->f_flags & O_NONBLOCK ? LDLM_FL_BLOCK_NOWAIT :0);
1246         if (rc != 0)
1247                 RETURN(rc);
1248
1249         /* this is ok, g_f_w will overwrite this under i_sem if it races
1250          * with a local truncate, it just makes our maxbyte checking easier */
1251         if (file->f_flags & O_APPEND)
1252                 *ppos = inode->i_size;
1253
1254         if (*ppos >= maxbytes) {
1255                 if (count || *ppos > maxbytes) {
1256                         send_sig(SIGXFSZ, current, 0);
1257                         GOTO(out, retval = -EFBIG);
1258                 }
1259         }
1260         if (*ppos + count > maxbytes)
1261                 count = maxbytes - *ppos;
1262
1263         CDEBUG(D_INFO, "Writing inode %lu, "LPSZ" bytes, offset %Lu\n",
1264                inode->i_ino, count, *ppos);
1265
1266         /* mark open handle dirty */
1267         set_bit(LLI_F_DIRTY_HANDLE, &(ll_i2info(inode)->lli_flags));
1268
1269         /* generic_file_write handles O_APPEND after getting i_sem */
1270         retval = generic_file_write(file, buf, count, ppos);
1271
1272         EXIT;
1273 out:
1274         /* ll_audit_log(inode, AUDIT_WRITE, retval); */
1275
1276         ll_tree_unlock(&tree, inode);
1277         /* serialize with mmap/munmap/mremap */
1278         lprocfs_counter_add(ll_i2sbi(inode)->ll_stats, LPROC_LL_WRITE_BYTES,
1279                             retval > 0 ? retval : 0);
1280         return retval;
1281 }
1282
1283 static int ll_lov_setstripe_ea_info(struct inode *inode, struct file *file,
1284                                     int flags, struct lov_user_md *lum,
1285                                     int lum_size)
1286 {
1287         struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
1288         struct ll_inode_info *lli = ll_i2info(inode);
1289         struct ptlrpc_request *req = NULL;
1290         struct obd_client_handle *och;
1291         struct lov_stripe_md *lsm;
1292         struct lustre_md md;
1293         struct file *f;
1294         int rc = 0;
1295         ENTRY;
1296
1297         
1298         if ((file->f_flags+1) & O_ACCMODE)
1299                 oit.it_flags++;
1300         if (file->f_flags & O_TRUNC)
1301                 oit.it_flags |= 2;
1302
1303         down(&lli->lli_open_sem);
1304         lsm = lli->lli_smd;
1305         if (lsm) {
1306                 up(&lli->lli_open_sem);
1307                 CDEBUG(D_IOCTL, "stripe already exists for ino %lu\n",
1308                        inode->i_ino);
1309                 RETURN(-EEXIST);
1310         }
1311
1312         f = get_empty_filp();
1313         if (!f)
1314                 GOTO(out, -ENOMEM);
1315
1316         f->f_dentry = file->f_dentry;
1317         f->f_vfsmnt = file->f_vfsmnt;
1318         f->f_flags = flags;
1319
1320         rc = ll_intent_alloc(&oit);
1321         if (rc)
1322                 GOTO(out, rc);
1323
1324         rc = ll_intent_file_open(f, lum, lum_size, &oit);
1325         if (rc)
1326                 GOTO(out, rc);
1327         if (it_disposition(&oit, DISP_LOOKUP_NEG))
1328                 GOTO(out, -ENOENT);
1329         
1330         req = LUSTRE_IT(&oit)->it_data;
1331         rc = LUSTRE_IT(&oit)->it_status;
1332
1333         if (rc < 0)
1334                 GOTO(out, rc);
1335
1336         rc = mdc_req2lustre_md(ll_i2mdexp(inode), req, 1,
1337                                ll_i2dtexp(inode), &md);
1338         if (rc)
1339                 GOTO(out, rc);
1340         ll_update_inode(f->f_dentry->d_inode, &md);
1341
1342         OBD_ALLOC(och, sizeof(struct obd_client_handle));
1343         if (!och)
1344                 GOTO(out, rc = -ENOMEM);
1345         
1346          /* actually ll_local_open() cannot fail! */
1347         rc = ll_local_open(f, &oit, och);
1348         if (rc)
1349                 GOTO(out, rc);
1350
1351         if (LUSTRE_IT(&oit)->it_lock_mode) {
1352                 ldlm_lock_decref_and_cancel((struct lustre_handle *)
1353                                             &LUSTRE_IT(&oit)->it_lock_handle,
1354                                             LUSTRE_IT(&oit)->it_lock_mode);
1355                 LUSTRE_IT(&oit)->it_lock_mode = 0;
1356         }
1357
1358         ll_intent_release(&oit);
1359
1360         /* ll_file_release() will decrease the count, but won't free anything
1361          * because we have at least one more reference coming from actual
1362          * open. */
1363         down(&lli->lli_och_sem);
1364         lli->lli_open_fd_write_count++;
1365         up(&lli->lli_och_sem);
1366         rc = ll_file_release(f->f_dentry->d_inode, f);
1367         
1368         /* Now also destroy our supplemental och */
1369         ll_md_och_close(ll_i2mdexp(inode), f->f_dentry->d_inode, och, 0, 0);
1370         EXIT;
1371 out:
1372         ll_intent_release(&oit);
1373         if (f)
1374                 put_filp(f);
1375         up(&lli->lli_open_sem);
1376         if (req != NULL)
1377                 ptlrpc_req_finished(req);
1378         return rc;
1379 }
1380
1381 static int ll_lov_setea(struct inode *inode, struct file *file,
1382                         unsigned long arg)
1383 {
1384         int flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
1385         int lum_size = sizeof(struct lov_user_md) +
1386                 sizeof(struct lov_user_ost_data);
1387         struct lov_user_md  *lump;
1388         int rc;
1389         ENTRY;
1390
1391         if (!capable(CAP_SYS_ADMIN))
1392                 RETURN(-EPERM);
1393
1394         OBD_ALLOC(lump, lum_size);
1395         if (lump == NULL)
1396                 RETURN(-ENOMEM);
1397         
1398         rc = copy_from_user(lump, (struct lov_user_md  *)arg, lum_size);
1399         if (rc)
1400                 GOTO(out_free_lump, rc = -EFAULT);
1401
1402         rc = ll_lov_setstripe_ea_info(inode, file, flags, lump, lum_size);
1403         EXIT;
1404 out_free_lump:
1405         OBD_FREE(lump, lum_size);
1406         return rc;
1407 }
1408
1409 static int ll_lov_setstripe(struct inode *inode, struct file *file,
1410                             unsigned long arg)
1411 {
1412         struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1413         int rc, flags = FMODE_WRITE;
1414         ENTRY;
1415
1416         /* Bug 1152: copy properly when this is no longer true */
1417         LASSERT(sizeof(lum) == sizeof(*lump));
1418         LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
1419         rc = copy_from_user(&lum, lump, sizeof(lum));
1420         if (rc)
1421                 RETURN(-EFAULT);
1422
1423         rc = ll_lov_setstripe_ea_info(inode, file, flags, &lum, sizeof(lum));
1424         if (rc == 0) {
1425                  put_user(0, &lump->lmm_stripe_count);
1426                  rc = obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2dtexp(inode),
1427                                     0, ll_i2info(inode)->lli_smd, lump);
1428         }
1429         RETURN(rc);
1430 }
1431
1432 static int ll_lov_getstripe(struct inode *inode, unsigned long arg)
1433 {
1434         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1435
1436         if (!lsm)
1437                 RETURN(-ENODATA);
1438
1439         return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2dtexp(inode), 0, lsm,
1440                              (void *)arg);
1441 }
1442
1443 static int ll_get_grouplock(struct inode *inode, struct file *file,
1444                          unsigned long arg)
1445 {
1446         struct ll_file_data *fd = file->private_data;
1447         ldlm_policy_data_t policy = { .l_extent = { .start = 0,
1448                                                     .end = OBD_OBJECT_EOF}};
1449         struct lustre_handle lockh = { 0 };
1450         struct ll_inode_info *lli = ll_i2info(inode);
1451         struct lov_stripe_md *lsm = lli->lli_smd;
1452         int flags = 0, rc;
1453         ENTRY;
1454
1455         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1456                 RETURN(-EINVAL);
1457         }
1458
1459         policy.l_extent.gid = arg;
1460         if (file->f_flags & O_NONBLOCK)
1461                 flags = LDLM_FL_BLOCK_NOWAIT;
1462
1463         rc = ll_extent_lock(fd, inode, lsm, LCK_GROUP, &policy, &lockh, flags,
1464                             &ll_i2sbi(inode)->ll_grouplock_stime);
1465         if (rc != 0)
1466                 RETURN(rc);
1467
1468         fd->fd_flags |= LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK;
1469         fd->fd_gid = arg;
1470         memcpy(&fd->fd_cwlockh, &lockh, sizeof(lockh));
1471
1472         RETURN(0);
1473 }
1474
1475 static int ll_put_grouplock(struct inode *inode, struct file *file,
1476                          unsigned long arg)
1477 {
1478         struct ll_file_data *fd = file->private_data;
1479         struct ll_inode_info *lli = ll_i2info(inode);
1480         struct lov_stripe_md *lsm = lli->lli_smd;
1481         int rc;
1482         ENTRY;
1483
1484         if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1485                 /* Ugh, it's already unlocked. */
1486                 RETURN(-EINVAL);
1487         }
1488
1489         if (fd->fd_gid != arg) /* Ugh? Unlocking with different gid? */
1490                 RETURN(-EINVAL);
1491
1492         fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
1493
1494         rc = ll_extent_unlock(fd, inode, lsm, LCK_GROUP, &fd->fd_cwlockh);
1495         if (rc)
1496                 RETURN(rc);
1497
1498         fd->fd_gid = 0;
1499         memset(&fd->fd_cwlockh, 0, sizeof(fd->fd_cwlockh));
1500
1501         RETURN(0);
1502 }
1503
1504 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
1505                   unsigned long arg)
1506 {
1507         struct ll_file_data *fd = file->private_data;
1508         struct ll_sb_info *sbi = ll_i2sbi(inode);
1509         int flags;
1510         ENTRY;
1511
1512         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),cmd=%x\n", inode->i_ino,
1513                inode->i_generation, inode, cmd);
1514
1515         if (_IOC_TYPE(cmd) == 'T') /* tty ioctls */
1516                 RETURN(-ENOTTY);
1517
1518         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_IOCTL);
1519         switch(cmd) {
1520         case LL_IOC_GETFLAGS:
1521                 /* Get the current value of the file flags */
1522                 return put_user(fd->fd_flags, (int *)arg);
1523         case LL_IOC_SETFLAGS:
1524         case LL_IOC_CLRFLAGS:
1525                 /* Set or clear specific file flags */
1526                 /* XXX This probably needs checks to ensure the flags are
1527                  *     not abused, and to handle any flag side effects.
1528                  */
1529                 if (get_user(flags, (int *) arg))
1530                         RETURN(-EFAULT);
1531
1532                 if (cmd == LL_IOC_SETFLAGS)
1533                         fd->fd_flags |= flags;
1534                 else
1535                         fd->fd_flags &= ~flags;
1536                 RETURN(0);
1537         case LL_IOC_LOV_SETSTRIPE:
1538                 RETURN(ll_lov_setstripe(inode, file, arg));
1539         case LL_IOC_LOV_SETEA:
1540                 RETURN(ll_lov_setea(inode, file, arg));
1541         case IOC_MDC_SHOWFID: {
1542                 struct lustre_id *idp = (struct lustre_id *)arg;
1543                 struct lustre_id id;
1544                 char *filename;
1545                 int rc;
1546
1547                 filename = getname((const char *)arg);
1548                 if (IS_ERR(filename))
1549                         RETURN(PTR_ERR(filename));
1550
1551                 ll_inode2id(&id, inode);
1552
1553                 rc = ll_get_fid(sbi->ll_md_exp, &id, filename, &id);
1554                 if (rc < 0)
1555                         GOTO(out_filename, rc);
1556
1557                 rc = copy_to_user(idp, &id, sizeof(*idp));
1558                 if (rc)
1559                         GOTO(out_filename, rc = -EFAULT);
1560
1561                 EXIT;
1562         out_filename:
1563                 putname(filename);
1564                 return rc;
1565         }
1566         case LL_IOC_KEY_TYPE: {
1567                 struct obd_ioctl_data *data;
1568                 char *buf = NULL;
1569                 char *type;
1570                 int typelen, rc, len = 0;
1571
1572                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1573                 if (rc)
1574                         RETURN(rc);
1575                 data = (void *)buf;
1576
1577                 type = data->ioc_inlbuf1;
1578                 typelen = data->ioc_inllen1;
1579
1580                 if (typelen < 1) {
1581                         CDEBUG(D_INFO, "LL_IOC_KEY_TYPE missing filename\n");
1582                         GOTO(out, rc = -EINVAL);
1583                 }
1584                 ll_set_sb_gksinfo(inode->i_sb, type);
1585                 EXIT;
1586         out:
1587                 obd_ioctl_freedata(buf, len);
1588                 return rc;
1589         }
1590
1591         case LL_IOC_LOV_GETSTRIPE:
1592                 RETURN(ll_lov_getstripe(inode, arg));
1593         case EXT3_IOC_GETFLAGS:
1594         case EXT3_IOC_SETFLAGS:
1595                 RETURN( ll_iocontrol(inode, file, cmd, arg) );
1596         case LL_IOC_GROUP_LOCK:
1597                 RETURN(ll_get_grouplock(inode, file, arg));
1598         case LL_IOC_GROUP_UNLOCK:
1599                 RETURN(ll_put_grouplock(inode, file, arg));
1600         case EXT3_IOC_GETVERSION_OLD:
1601         case EXT3_IOC_GETVERSION:
1602                 return put_user(inode->i_generation, (int *) arg);
1603         /* We need to special case any other ioctls we want to handle,
1604          * to send them to the MDS/OST as appropriate and to properly
1605          * network encode the arg field.
1606         case EXT2_IOC_GETVERSION_OLD:
1607         case EXT2_IOC_GETVERSION_NEW:
1608         case EXT2_IOC_SETVERSION_OLD:
1609         case EXT2_IOC_SETVERSION_NEW:
1610         case EXT3_IOC_SETVERSION_OLD:
1611         case EXT3_IOC_SETVERSION:
1612         */
1613         case LL_IOC_FLUSH_CRED:
1614                 RETURN(ll_flush_cred(inode));
1615         case LL_IOC_AUDIT:
1616                 RETURN(ll_set_audit(inode, arg));
1617         default:
1618                 RETURN( obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
1619                                       (void *)arg) );
1620         }
1621 }
1622
1623 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
1624 {
1625         struct inode *inode = file->f_dentry->d_inode;
1626         struct ll_file_data *fd = file->private_data;
1627         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1628         struct lustre_handle lockh = {0};
1629         loff_t retval;
1630         ENTRY;
1631         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),to=%llu\n", inode->i_ino,
1632                inode->i_generation, inode,
1633                offset + ((origin==2) ? inode->i_size : file->f_pos));
1634
1635         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_LLSEEK);
1636         if (origin == 2) { /* SEEK_END */
1637                 ldlm_policy_data_t policy = { .l_extent = {0, OBD_OBJECT_EOF }};
1638                 struct ll_inode_info *lli = ll_i2info(inode);
1639                 int nonblock = 0, rc;
1640
1641                 if (file->f_flags & O_NONBLOCK)
1642                         nonblock = LDLM_FL_BLOCK_NOWAIT;
1643
1644                 rc = ll_extent_lock(fd, inode, lsm, LCK_PR, &policy, &lockh,
1645                                     nonblock, &ll_i2sbi(inode)->ll_seek_stime);
1646                 if (rc != 0)
1647                         RETURN(rc);
1648
1649                 down(&lli->lli_size_sem);
1650                 offset += inode->i_size;
1651                 up(&lli->lli_size_sem);
1652         } else if (origin == 1) { /* SEEK_CUR */
1653                 offset += file->f_pos;
1654         }
1655
1656         retval = -EINVAL;
1657         if (offset >= 0 && offset <= ll_file_maxbytes(inode)) {
1658                 if (offset != file->f_pos) {
1659                         file->f_pos = offset;
1660 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1661                         file->f_reada = 0;
1662                         file->f_version = ++event;
1663 #endif
1664                 }
1665                 retval = offset;
1666         }
1667
1668         if (origin == 2)
1669                 ll_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
1670         RETURN(retval);
1671 }
1672
1673 int ll_fsync(struct file *file, struct dentry *dentry, int data)
1674 {
1675         struct inode *inode = dentry->d_inode;
1676         struct ll_inode_info *lli = ll_i2info(inode);
1677         struct address_space *mapping = inode->i_mapping;
1678         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1679         struct lustre_id id;
1680         struct ptlrpc_request *req;
1681         int rc, err;
1682         ENTRY;
1683         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1684                inode->i_generation, inode);
1685
1686         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_FSYNC);
1687
1688         /* fsync's caller has already called _fdata{sync,write}, we want
1689          * that IO to finish before calling the osc and mdc sync methods */
1690         rc = filemap_fdatawait(inode->i_mapping);
1691
1692         /* 2.6 implements filemap_fdatawait() using PG_writeback which we
1693          * don't support now. so, wait until all submited llaps are gone */
1694         wait_event(lli->lli_dirty_wait, !ll_is_inode_dirty(mapping->host));
1695
1696         ll_inode2id(&id, inode);
1697         err = md_sync(ll_i2sbi(inode)->ll_md_exp, &id, &req);
1698         if (!rc)
1699                 rc = err;
1700         if (!err)
1701                 ptlrpc_req_finished(req);
1702
1703         if (data && lsm) {
1704                 struct obdo *oa = obdo_alloc();
1705
1706                 if (!oa)
1707                         RETURN(rc ? rc : -ENOMEM);
1708
1709                 oa->o_id = lsm->lsm_object_id;
1710                 oa->o_gr = lsm->lsm_object_gr;
1711                 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1712
1713                 obdo_from_inode(oa, inode, (OBD_MD_FLTYPE | OBD_MD_FLATIME |
1714                                             OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1715                                             OBD_MD_FLGROUP));
1716
1717                 err = obd_sync(ll_i2sbi(inode)->ll_dt_exp, oa, lsm,
1718                                0, OBD_OBJECT_EOF);
1719                 if (!rc)
1720                         rc = err;
1721                 obdo_free(oa);
1722         }
1723
1724         RETURN(rc);
1725 }
1726
1727 int ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
1728 {
1729         struct inode *inode = file->f_dentry->d_inode;
1730         struct ll_inode_info *li = ll_i2info(inode);
1731         struct ll_sb_info *sbi = ll_i2sbi(inode);
1732         struct obd_device *obddev;
1733         struct ldlm_res_id res_id =
1734                 { .name = {id_fid(&li->lli_id), id_group(&li->lli_id), LDLM_FLOCK} };
1735         struct lustre_handle lockh = {0};
1736         ldlm_policy_data_t flock;
1737         ldlm_mode_t mode = 0;
1738         int flags = 0;
1739         int rc;
1740         ENTRY;
1741
1742         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu file_lock=%p\n",
1743                inode->i_ino, file_lock);
1744
1745         flock.l_flock.pid = file_lock->fl_pid;
1746         flock.l_flock.start = file_lock->fl_start;
1747         flock.l_flock.end = file_lock->fl_end;
1748
1749         switch (file_lock->fl_type) {
1750         case F_RDLCK:
1751                 mode = LCK_PR;
1752                 break;
1753         case F_UNLCK:
1754                 /* An unlock request may or may not have any relation to
1755                  * existing locks so we may not be able to pass a lock handle
1756                  * via a normal ldlm_lock_cancel() request. The request may even
1757                  * unlock a byte range in the middle of an existing lock. In
1758                  * order to process an unlock request we need all of the same
1759                  * information that is given with a normal read or write record
1760                  * lock request. To avoid creating another ldlm unlock (cancel)
1761                  * message we'll treat a LCK_NL flock request as an unlock. */
1762                 mode = LCK_NL;
1763                 break;
1764         case F_WRLCK:
1765                 mode = LCK_PW;
1766                 break;
1767         default:
1768                 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
1769                 LBUG();
1770         }
1771
1772         switch (cmd) {
1773         case F_SETLKW:
1774 #ifdef F_SETLKW64
1775         case F_SETLKW64:
1776 #endif
1777                 flags = 0;
1778                 break;
1779         case F_SETLK:
1780 #ifdef F_SETLK64
1781         case F_SETLK64:
1782 #endif
1783                 flags = LDLM_FL_BLOCK_NOWAIT;
1784                 break;
1785         case F_GETLK:
1786 #ifdef F_GETLK64
1787         case F_GETLK64:
1788 #endif
1789                 flags = LDLM_FL_TEST_LOCK;
1790                 /* Save the old mode so that if the mode in the lock changes we
1791                  * can decrement the appropriate reader or writer refcount. */
1792                 file_lock->fl_type = mode;
1793                 break;
1794         default:
1795                 CERROR("unknown fcntl lock command: %d\n", cmd);
1796                 LBUG();
1797         }
1798
1799         CDEBUG(D_DLMTRACE, "inode=%lu, pid="LPU64", flags=%#x, mode=%u, "
1800                "start="LPU64", end="LPU64"\n", inode->i_ino, flock.l_flock.pid,
1801                flags, mode, flock.l_flock.start, flock.l_flock.end);
1802
1803         obddev = md_get_real_obd(sbi->ll_md_exp, &li->lli_id);
1804         rc = ldlm_cli_enqueue(obddev->obd_self_export, NULL,
1805                               obddev->obd_namespace,
1806                               res_id, LDLM_FLOCK, &flock, mode, &flags,
1807                               NULL, ldlm_flock_completion_ast, NULL, file_lock,
1808                               NULL, 0, NULL, &lockh);
1809         RETURN(rc);
1810 }
1811
1812 int ll_inode_revalidate_it(struct dentry *dentry)
1813 {
1814         struct lookup_intent oit = { .it_op = IT_GETATTR };
1815         struct inode *inode = dentry->d_inode;
1816         struct ptlrpc_request *req = NULL;
1817         struct ll_inode_info *lli;
1818         struct lov_stripe_md *lsm;
1819         struct ll_sb_info *sbi;
1820         struct lustre_id id;
1821         int rc;
1822         ENTRY;
1823
1824         if (!inode) {
1825                 CERROR("REPORT THIS LINE TO PETER\n");
1826                 RETURN(0);
1827         }
1828         
1829         sbi = ll_i2sbi(inode);
1830         
1831         ll_inode2id(&id, inode);
1832         lli = ll_i2info(inode);
1833         LASSERT(id_fid(&id) != 0);
1834
1835         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), name=%s(%p)\n",
1836                inode->i_ino, inode->i_generation, inode, dentry->d_name.name,
1837                dentry);
1838
1839 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0))
1840         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_REVALIDATE);
1841 #endif
1842
1843         rc = ll_intent_alloc(&oit);
1844         if (rc)
1845                 RETURN(-ENOMEM);
1846         
1847         rc = ll_crypto_init_it_key(inode, &oit);
1848         if (rc)
1849                 GOTO(out, rc);
1850
1851         rc = md_intent_lock(sbi->ll_md_exp, &id, NULL, 0, NULL, 0, &id,
1852                             &oit, 0, &req, ll_mdc_blocking_ast);
1853         if (rc < 0)
1854                 GOTO(out, rc);
1855         
1856         rc = revalidate_it_finish(req, 1, &oit, dentry);
1857         if (rc) {
1858                 GOTO(out, rc);
1859         }
1860
1861         ll_lookup_finish_locks(&oit, dentry);
1862         
1863         if (!req && (oit.it_op & IT_GETATTR))
1864                 ll_audit_log(inode, AUDIT_STAT, 0);
1865
1866         if (!LLI_HAVE_FLSIZE(inode)) {
1867                 /* if object not yet allocated, don't validate size */
1868                 lsm = lli->lli_smd;
1869                 if (lsm != NULL) {
1870                         /* ll_glimpse_size() will prefer locally cached
1871                          * writes if they extend the file */
1872                         rc = ll_glimpse_size(inode);
1873                 }
1874         }
1875         EXIT;
1876 out:
1877         ll_intent_release(&oit);
1878         if (req)
1879                 ptlrpc_req_finished(req);
1880         
1881         return rc;
1882 }
1883
1884 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1885 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
1886 {
1887         int res = 0;
1888         struct inode *inode = de->d_inode;
1889         struct ll_inode_info *lli = ll_i2info(inode);
1890
1891         res = ll_inode_revalidate_it(de);
1892         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_GETATTR);
1893
1894         if (res)
1895                 return res;
1896
1897         stat->ino = inode->i_ino;
1898         stat->mode = inode->i_mode;
1899         stat->nlink = inode->i_nlink;
1900         stat->uid = inode->i_uid;
1901         stat->gid = inode->i_gid;
1902         stat->atime = inode->i_atime;
1903         stat->mtime = inode->i_mtime;
1904         stat->ctime = inode->i_ctime;
1905         stat->blksize = inode->i_blksize;
1906
1907         down(&lli->lli_size_sem);
1908         stat->size = inode->i_size;
1909         stat->blocks = inode->i_blocks;
1910         up(&lli->lli_size_sem);
1911         
1912         stat->rdev = kdev_t_to_nr(inode->i_rdev);
1913         stat->dev = id_group(&ll_i2info(inode)->lli_id);
1914         return 0;
1915 }
1916 #endif
1917
1918 static
1919 int ll_setxattr_internal(struct inode *inode, const char *name,
1920                          const void *value, size_t size, int flags, 
1921                          __u64 valid)
1922 {
1923         struct ll_sb_info *sbi = ll_i2sbi(inode);
1924         struct ptlrpc_request *request = NULL;
1925         struct mdc_op_data *op_data;
1926         struct iattr attr;
1927         void *key = NULL;
1928         int rc = 0, key_size = 0;
1929         ENTRY;
1930
1931         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", inode->i_ino);
1932         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_SETXATTR);
1933
1934         if (sbi->ll_remote && !strcmp(name, XATTR_NAME_ACL_ACCESS))
1935                 RETURN(-EOPNOTSUPP);
1936
1937         memset(&attr, 0x0, sizeof(attr));
1938         attr.ia_valid |= valid;
1939         attr.ia_attr_flags = flags;
1940
1941         if (strcmp(name, XATTR_NAME_ACL_ACCESS) == 0) {
1942                 rc = ll_crypto_get_mac(inode, &attr, (void *)value, size, 
1943                                        &key, &key_size);
1944                 if (rc) {
1945                         CERROR("can not get right mac, rc=%d\n", rc);
1946                         GOTO(out, rc);
1947                 }
1948         }
1949
1950         OBD_ALLOC(op_data, sizeof(*op_data));
1951         if (!op_data)
1952                 RETURN(-ENOMEM);
1953         
1954         ll_inode2mdc_data(op_data, inode, (OBD_MD_FLID | OBD_MD_MEA));
1955
1956         rc = md_setattr(sbi->ll_md_exp, op_data, &attr,
1957                         (void *)name, strnlen(name, XATTR_NAME_MAX) + 1, 
1958                         (void *)value,  size, key, key_size, &request);
1959         OBD_FREE(op_data, sizeof(*op_data));
1960         
1961         if (rc) {
1962                 CDEBUG(D_SEC, "md_setattr fails: rc = %d\n", rc);
1963                 GOTO(out, rc);
1964         }
1965 out:
1966         if (key && key_size) 
1967                 OBD_FREE(key, key_size);
1968         ptlrpc_req_finished(request);
1969         RETURN(rc);
1970 }
1971
1972 int ll_setxattr(struct dentry *dentry, const char *name, const void *value,
1973                 size_t size, int flags)
1974 {
1975         int rc, error;
1976         struct posix_acl *acl;
1977         struct ll_inode_info *lli;
1978         ENTRY;
1979
1980         rc = ll_setxattr_internal(dentry->d_inode, name, value, size, 
1981                                   flags, ATTR_EA);
1982
1983         /* update inode's acl info */
1984         if (rc == 0 && strcmp(name, XATTR_NAME_ACL_ACCESS) == 0) {
1985                 if (value) {
1986                         acl = posix_acl_from_xattr(value, size);
1987                         if (IS_ERR(acl)) {
1988                                 CERROR("convert from xattr to acl error: %ld",
1989                                         PTR_ERR(acl));
1990                                 GOTO(out, rc);
1991                         } else if (acl) {
1992                                 error = posix_acl_valid(acl);
1993                                 if (error) {
1994                                         CERROR("acl valid error: %d", error);
1995                                         posix_acl_release(acl);
1996                                         GOTO(out, rc);
1997                                 }
1998                         }
1999                 } else {
2000                         acl = NULL;
2001                 }
2002                                         
2003                 lli = ll_i2info(dentry->d_inode);
2004                 spin_lock(&lli->lli_lock);
2005                 if (lli->lli_posix_acl != NULL)
2006                         posix_acl_release(lli->lli_posix_acl);
2007                 lli->lli_posix_acl = acl;
2008                 spin_unlock(&lli->lli_lock);
2009         }
2010         EXIT;
2011 out:
2012         return(rc);
2013 }
2014
2015 int ll_removexattr(struct dentry *dentry, const char *name)
2016 {
2017         return ll_setxattr_internal(dentry->d_inode, name, NULL, 0, 0,
2018                                     ATTR_EA_RM);
2019 }
2020
2021 int ll_getxattr_internal(struct inode *inode, const char *name,
2022                          void *value, size_t size, __u64 valid)
2023 {
2024         struct ptlrpc_request *request = NULL;
2025         struct ll_sb_info *sbi = ll_i2sbi(inode);
2026         struct lustre_id id;
2027         struct mds_body *body;
2028         void *ea_data; 
2029         int rc, ea_size;
2030         ENTRY;
2031
2032         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_GETXATTR);
2033
2034         if (sbi->ll_remote && !strcmp(name, XATTR_NAME_ACL_ACCESS))
2035                 RETURN(-EOPNOTSUPP);
2036
2037         ll_inode2id(&id, inode);
2038         rc = md_getattr(sbi->ll_md_exp, &id, valid, name, NULL, 0,
2039                         size, NULL, &request);
2040         if (rc) {
2041                 if (rc != -ENODATA && rc != -EOPNOTSUPP)
2042                         CERROR("rc = %d\n", rc);
2043                 GOTO(out, rc);
2044         }
2045
2046         body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
2047         LASSERT(body != NULL);
2048         LASSERT_REPSWABBED(request, 0);
2049
2050         ea_size = body->eadatasize;
2051         if (size == 0) 
2052                 GOTO(out, rc = ea_size);
2053
2054         LASSERT(ea_size <= request->rq_repmsg->buflens[1]);
2055         ea_data = lustre_msg_buf(request->rq_repmsg, 1, ea_size);
2056         LASSERT(ea_data != NULL);
2057         LASSERT_REPSWABBED(request, 1);
2058
2059         if (value)
2060                 memcpy(value, ea_data, ea_size);
2061         rc = ea_size;
2062
2063  out:
2064         ptlrpc_req_finished(request);
2065         RETURN(rc);
2066 }
2067
2068 int ll_getxattr(struct dentry *dentry, const char *name, void *value,
2069                 size_t size)
2070 {
2071         return ll_getxattr_internal(dentry->d_inode, name,
2072                                     value, size, OBD_MD_FLXATTR);
2073 }
2074
2075 int ll_listxattr(struct dentry *dentry, char *list, size_t size)
2076 {
2077         return ll_getxattr_internal(dentry->d_inode, NULL, list, size,
2078                                     OBD_MD_FLXATTRLIST);
2079 }
2080
2081 /*
2082  * Here we hold DLM lock across permission check, to get better
2083  * conformance especially for remote acl. will introduce certain
2084  * overhead, maybe need a profile.
2085  */
2086 static int
2087 lustre_check_acl(struct inode *inode, int mask)
2088 {
2089         struct lookup_intent it = { .it_op = IT_GETATTR };
2090         struct dentry de = { .d_inode = inode };
2091         struct ll_sb_info *sbi;
2092         struct lustre_id id;
2093         struct ptlrpc_request *req = NULL;
2094         struct ll_inode_info *lli = ll_i2info(inode);
2095         struct posix_acl *acl;
2096         int rc = 0;
2097         ENTRY;
2098
2099         sbi = ll_i2sbi(inode);
2100         ll_inode2id(&id, inode);
2101
2102         if (ll_intent_alloc(&it))
2103                 return -EACCES;
2104
2105         rc = md_intent_lock(sbi->ll_md_exp, &id, NULL, 0, NULL, 0, &id,
2106                             &it, 0, &req, ll_mdc_blocking_ast);
2107         if (rc < 0) {
2108                 ll_intent_free(&it);
2109                 GOTO(out, rc);
2110         }
2111
2112         rc = revalidate_it_finish(req, 1, &it, &de);
2113         if (rc) {
2114                 ll_intent_release(&it);
2115                 GOTO(out, rc);
2116         }
2117
2118         if (sbi->ll_remote) {
2119                 rc = ll_remote_acl_permission(inode, mask);
2120         } else {
2121                 spin_lock(&lli->lli_lock);
2122                 acl = posix_acl_dup(ll_i2info(inode)->lli_posix_acl);
2123                 spin_unlock(&lli->lli_lock);
2124
2125                 if (!acl)
2126                         rc = -EAGAIN;
2127                 else {
2128                         rc = posix_acl_permission(inode, acl, mask);
2129                         posix_acl_release(acl);
2130                 }
2131         }
2132
2133         ll_lookup_finish_locks(&it, &de);
2134         ll_intent_free(&it);
2135
2136 out:
2137         if (req)
2138                 ptlrpc_req_finished(req);
2139
2140         RETURN(rc);
2141 }
2142
2143 int ll_inode_permission(struct inode *inode, int mask, struct nameidata *nd)
2144 {
2145         struct ll_sb_info *sbi = ll_i2sbi(inode);
2146
2147         /* for remote client, permission bits in inode doesn't
2148          * play a role anymore.
2149          */
2150         if (sbi->ll_remote)
2151                 return lustre_check_acl(inode, mask);
2152
2153         return generic_permission(inode, mask, lustre_check_acl);
2154 }
2155
2156 struct file_operations ll_file_operations = {
2157         .read           = ll_file_read,
2158         .write          = ll_file_write,
2159         .ioctl          = ll_file_ioctl,
2160         .open           = ll_file_open,
2161         .release        = ll_file_release,
2162         .mmap           = ll_file_mmap,
2163         .llseek         = ll_file_seek,
2164 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
2165         .sendfile       = generic_file_sendfile,
2166 #endif
2167         .fsync          = ll_fsync,
2168         .lock           = ll_file_flock
2169 };
2170
2171 struct inode_operations ll_file_inode_operations = {
2172         .setattr        = ll_setattr,
2173         .truncate       = ll_truncate,
2174 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2175         .getattr        = ll_getattr,
2176 #else
2177         .revalidate_it  = ll_inode_revalidate_it,
2178 #endif
2179         .setxattr       = ll_setxattr,
2180         .getxattr       = ll_getxattr,
2181         .listxattr      = ll_listxattr,
2182         .removexattr    = ll_removexattr,
2183         .permission     = ll_inode_permission,
2184 };
2185