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