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