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