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