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