Whamcloud - gitweb
land v0.9.1 on HEAD, in preparation for a 1.0.x branch
[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/obd_lov.h>      /* for lov_mds_md_size() in lov_setstripe() */
29 #include <linux/random.h>
30 #include <linux/pagemap.h>
31 #include <linux/file.h>
32 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
33 #include <linux/lustre_compat25.h>
34 #endif
35 #include "llite_internal.h"
36
37 int ll_mdc_close(struct obd_export *mdc_exp, struct inode *inode,
38                         struct file *file)
39 {
40         struct ll_file_data *fd = file->private_data;
41         struct ptlrpc_request *req = NULL;
42         struct obd_client_handle *och = &fd->fd_mds_och;
43         struct ll_inode_info *lli = ll_i2info(inode);
44         struct obdo obdo;
45         int rc, valid;
46         ENTRY;
47
48         valid = OBD_MD_FLID;
49         if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
50                 valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
51
52         memset(&obdo, 0, sizeof(obdo));
53         obdo.o_id = inode->i_ino;
54         obdo.o_mode = inode->i_mode;
55         obdo.o_size = inode->i_size;
56         obdo.o_blocks = inode->i_blocks;
57         if (0 /* ll_is_inode_dirty(inode) */) {
58                 obdo.o_flags = MDS_BFLAG_UNCOMMITTED_WRITES;
59                 valid |= OBD_MD_FLFLAGS;
60         }
61         obdo.o_valid = valid;
62         rc = mdc_close(mdc_exp, &obdo, och, &req);
63         if (rc == EAGAIN) {
64                 /* We are the last writer, so the MDS has instructed us to get
65                  * the file size and any write cookies, then close again. */
66                 //ll_queue_done_writing(inode);
67                 rc = 0;
68         } else if (rc) {
69                 CERROR("inode %lu mdc close failed: rc = %d\n",
70                        inode->i_ino, rc);
71         }
72         if (rc == 0) {
73                 rc = ll_objects_destroy(req, file->f_dentry->d_inode);
74                 if (rc)
75                         CERROR("inode %lu ll_objects destroy: rc = %d\n",
76                                inode->i_ino, rc);
77         }
78
79         mdc_clear_open_replay_data(och);
80         ptlrpc_req_finished(req);
81         och->och_fh.cookie = DEAD_HANDLE_MAGIC;
82         file->private_data = NULL;
83         OBD_SLAB_FREE(fd, ll_file_data_slab, sizeof *fd);
84
85         RETURN(rc);
86 }
87
88 /* While this returns an error code, fput() the caller does not, so we need
89  * to make every effort to clean up all of our state here.  Also, applications
90  * rarely check close errors and even if an error is returned they will not
91  * re-try the close call.
92  */
93 int ll_file_release(struct inode *inode, struct file *file)
94 {
95         struct ll_file_data *fd;
96         struct ll_sb_info *sbi = ll_i2sbi(inode);
97         int rc;
98
99         ENTRY;
100         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
101                inode->i_generation, inode);
102
103         /* don't do anything for / */
104         if (inode->i_sb->s_root == file->f_dentry)
105                 RETURN(0);
106
107         lprocfs_counter_incr(sbi->ll_stats, LPROC_LL_RELEASE);
108         fd = (struct ll_file_data *)file->private_data;
109         LASSERT(fd != NULL);
110
111         rc = ll_mdc_close(sbi->ll_mdc_exp, inode, file);
112         RETURN(rc);
113 }
114
115 static int ll_intent_file_open(struct file *file, void *lmm,
116                                int lmmsize, struct lookup_intent *itp)
117 {
118         struct ll_sb_info *sbi = ll_i2sbi(file->f_dentry->d_inode);
119         struct lustre_handle lockh;
120         struct mdc_op_data data;
121         struct dentry *parent = file->f_dentry->d_parent;
122         const char *name = file->f_dentry->d_name.name;
123         const int len = file->f_dentry->d_name.len;
124         int rc;
125
126         if (!parent)
127                 RETURN(-ENOENT);
128
129         ll_prepare_mdc_op_data(&data, parent->d_inode, NULL, name, len, O_RDWR);
130
131         rc = mdc_enqueue(sbi->ll_mdc_exp, LDLM_PLAIN, itp, LCK_PR, &data,
132                          &lockh, lmm, lmmsize, ldlm_completion_ast,
133                          ll_mdc_blocking_ast, parent->d_inode);
134         if (rc < 0)
135                 CERROR("lock enqueue: err: %d\n", rc);
136         RETURN(rc);
137 }
138
139 int ll_local_open(struct file *file, struct lookup_intent *it)
140 {
141         struct ptlrpc_request *req = it->d.lustre.it_data;
142         struct ll_inode_info *lli = ll_i2info(file->f_dentry->d_inode);
143         struct ll_file_data *fd;
144         struct mds_body *body;
145         ENTRY;
146
147         body = lustre_msg_buf (req->rq_repmsg, 1, sizeof (*body));
148         LASSERT (body != NULL);                 /* reply already checked out */
149         LASSERT_REPSWABBED (req, 1);            /* and swabbed down */
150
151         LASSERT(!file->private_data);
152
153         OBD_SLAB_ALLOC(fd, ll_file_data_slab, SLAB_KERNEL, sizeof *fd);
154         /* We can't handle this well without reorganizing ll_file_open and
155          * ll_mdc_close, so don't even try right now. */
156         LASSERT(fd != NULL);
157
158         memcpy(&fd->fd_mds_och.och_fh, &body->handle, sizeof(body->handle));
159         fd->fd_mds_och.och_magic = OBD_CLIENT_HANDLE_MAGIC;
160         file->private_data = fd;
161
162         lli->lli_io_epoch = body->io_epoch;
163
164         mdc_set_open_replay_data(&fd->fd_mds_och, it->d.lustre.it_data);
165
166         RETURN(0);
167 }
168
169 /* Open a file, and (for the very first open) create objects on the OSTs at
170  * this time.  If opened with O_LOV_DELAY_CREATE, then we don't do the object
171  * creation or open until ll_lov_setstripe() ioctl is called.  We grab
172  * lli_open_sem to ensure no other process will create objects, send the
173  * stripe MD to the MDS, or try to destroy the objects if that fails.
174  *
175  * If we already have the stripe MD locally then we don't request it in
176  * mdc_open(), by passing a lmm_size = 0.
177  *
178  * It is up to the application to ensure no other processes open this file
179  * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
180  * used.  We might be able to avoid races of that sort by getting lli_open_sem
181  * before returning in the O_LOV_DELAY_CREATE case and dropping it here
182  * or in ll_file_release(), but I'm not sure that is desirable/necessary.
183  */
184 int ll_file_open(struct inode *inode, struct file *file)
185 {
186         struct ll_inode_info *lli = ll_i2info(inode);
187         struct lookup_intent *it;
188         struct lov_stripe_md *lsm;
189         struct ptlrpc_request *req;
190         int rc = 0;
191         ENTRY;
192
193         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
194                inode->i_generation, inode);
195
196         /* don't do anything for / */
197         if (inode->i_sb->s_root == file->f_dentry)
198                 RETURN(0);
199
200         it = file->f_it;
201
202         if (!it->d.lustre.it_disposition) {
203                 struct lookup_intent oit = { .it_op = IT_OPEN,
204                                              .it_flags = file->f_flags };
205                 it = &oit;
206                 rc = ll_intent_file_open(file, NULL, 0, it);
207                 if (rc)
208                         GOTO(out, rc);
209         }
210
211         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_OPEN);
212         rc = it_open_error(DISP_OPEN_OPEN, it);
213         if (rc)
214                 GOTO(out, rc);
215
216         rc = ll_local_open(file, it);
217         if (rc)
218                 LBUG();
219
220         if (!S_ISREG(inode->i_mode))
221                 GOTO(out, rc);
222
223         lsm = lli->lli_smd;
224         if (lsm == NULL) {
225                 if (file->f_flags & O_LOV_DELAY_CREATE ||
226                     !(file->f_mode & FMODE_WRITE)) {
227                         CDEBUG(D_INODE, "object creation was delayed\n");
228                         GOTO(out, rc);
229                 }
230         }
231         file->f_flags &= ~O_LOV_DELAY_CREATE;
232         GOTO(out, rc);
233  out:
234         req = it->d.lustre.it_data;
235         ptlrpc_req_finished(req);
236         if (rc == 0)
237                 ll_open_complete(inode);
238         return rc;
239 }
240
241 /* Fills the obdo with the attributes for the inode defined by lsm */
242 int ll_lsm_getattr(struct obd_export *exp, struct lov_stripe_md *lsm,
243                    struct obdo *oa)
244 {
245         struct ptlrpc_request_set *set;
246         int rc;
247         ENTRY;
248
249         LASSERT(lsm != NULL);
250
251         memset(oa, 0, sizeof *oa);
252         oa->o_id = lsm->lsm_object_id;
253         oa->o_mode = S_IFREG;
254         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
255                 OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
256                 OBD_MD_FLCTIME;
257
258         set = ptlrpc_prep_set();
259         if (set == NULL) {
260                 CERROR ("ENOMEM allocing request set\n");
261                 rc = -ENOMEM;
262         } else {
263                 rc = obd_getattr_async(exp, oa, lsm, set);
264                 if (rc == 0)
265                         rc = ptlrpc_set_wait(set);
266                 ptlrpc_set_destroy(set);
267         }
268         if (rc)
269                 RETURN(rc);
270
271         oa->o_valid &= (OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME | 
272                         OBD_MD_FLCTIME | OBD_MD_FLSIZE);
273         RETURN(0);
274 }
275
276 static inline void ll_remove_suid(struct inode *inode)
277 {
278         unsigned int mode;
279
280         /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
281         mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
282
283         /* was any of the uid bits set? */
284         mode &= inode->i_mode;
285         if (mode && !capable(CAP_FSETID)) {
286                 inode->i_mode &= ~mode;
287                 // XXX careful here - we cannot change the size
288         }
289 }
290
291 /* Flush the page cache for an extent as its canceled.  No one can dirty the
292  * extent until we've finished our work and they can enqueue another lock.
293  * The DLM protects us from ll_file_read/write here, but other kernel actors
294  * could have pages locked */
295 void ll_pgcache_remove_extent(struct inode *inode, struct lov_stripe_md *lsm,
296                               struct ldlm_lock *lock)
297 {
298         struct ldlm_extent *extent = &lock->l_policy_data.l_extent;
299         struct obd_export *exp = ll_i2obdexp(inode);
300         struct ll_inode_info *lli = ll_i2info(inode);
301         unsigned long start, end, i;
302         struct page *page;
303         int rc, discard = lock->l_flags & LDLM_FL_DISCARD_DATA;
304         ENTRY;
305
306         CDEBUG(D_INODE, "obdo %lu inode %p ["LPU64"->"LPU64"] size: %llu\n",
307                inode->i_ino, inode, extent->start, extent->end, inode->i_size);
308
309         start = extent->start >> PAGE_CACHE_SHIFT;
310         end = (extent->end >> PAGE_CACHE_SHIFT) + 1;
311         if ((end << PAGE_CACHE_SHIFT) < extent->end)
312                 end = ~0;
313
314         i = (inode->i_size + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
315         if (end >= i)
316                 clear_bit(LLI_F_HAVE_OST_SIZE_LOCK,
317                           &(ll_i2info(inode)->lli_flags));
318         if (i < end)
319                 end = i;
320
321         CDEBUG(D_INODE, "walking page indices start: %lu end: %lu\n", start,
322                end);
323
324         for (i = start; i < end; i++) {
325                 ll_pgcache_lock(inode->i_mapping);
326                 if (list_empty(&inode->i_mapping->dirty_pages) &&
327                      list_empty(&inode->i_mapping->clean_pages) &&
328                      list_empty(&inode->i_mapping->locked_pages)) {
329                         CDEBUG(D_INODE, "nothing left\n");
330                         ll_pgcache_unlock(inode->i_mapping);
331                         break;
332                 }
333                 ll_pgcache_unlock(inode->i_mapping);
334
335                 conditional_schedule();
336
337                 page = find_get_page(inode->i_mapping, i);
338                 if (page == NULL)
339                         continue;
340
341                 LL_CDEBUG_PAGE(page, "locking\n");
342                 lock_page(page);
343
344                 /* page->mapping to check with racing against teardown */
345                 if (page->mapping && PageDirty(page) && !discard) {
346                         ClearPageDirty(page);
347                         LL_CDEBUG_PAGE(page, "found dirty\n");
348                         ll_pgcache_lock(inode->i_mapping);
349                         list_del(&page->list);
350                         list_add(&page->list, &inode->i_mapping->locked_pages);
351                         ll_pgcache_unlock(inode->i_mapping);
352
353 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
354                         rc = inode->i_mapping->a_ops->writepage(page);
355 #else
356                         rc = inode->i_mapping->a_ops->writepage(page, NULL);
357 #endif
358                         if (rc != 0) {
359                                 CERROR("writepage of page %p failed: %d\n",
360                                        page, rc);
361                         } else {
362                                 lock_page(page); /* wait for io to complete */
363                         }
364                 }
365
366                 /* checking again to account for writeback's lock_page() */
367                 if (page->mapping != NULL) {
368                         LL_CDEBUG_PAGE(page, "truncating\n");
369 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
370                         truncate_complete_page(page);
371 #else
372                         truncate_complete_page(page->mapping, page);
373 #endif
374                 }
375                 unlock_page(page);
376                 page_cache_release(page);
377         }
378
379         if (test_bit(LLI_F_PREFER_EXTENDED_SIZE, &lli->lli_flags)) {
380                 rc = obd_lock_contains(exp, lsm, lock, inode->i_size - 1);
381                 if (rc != 0) {
382                         if (rc < 0)
383                                 CERROR("obd_lock_contains: rc = %d\n", rc);
384                         clear_bit(LLI_F_PREFER_EXTENDED_SIZE, &lli->lli_flags);
385                 }
386         }
387
388         EXIT;
389 }
390
391 static int ll_extent_lock_callback(struct ldlm_lock *lock,
392                                    struct ldlm_lock_desc *new, void *data,
393                                    int flag)
394 {
395         struct lustre_handle lockh = { 0 };
396         int rc;
397         ENTRY;
398
399
400         if ((unsigned long)data > 0 && (unsigned long)data < 0x1000) {
401                 LDLM_ERROR(lock, "cancelling lock with bad data %p", data);
402                 LBUG();
403         }
404
405         switch (flag) {
406         case LDLM_CB_BLOCKING:
407                 ldlm_lock2handle(lock, &lockh);
408                 rc = ldlm_cli_cancel(&lockh);
409                 if (rc != ELDLM_OK)
410                         CERROR("ldlm_cli_cancel failed: %d\n", rc);
411                 break;
412         case LDLM_CB_CANCELING: {
413                 struct inode *inode = ll_inode_from_lock(lock);
414                 struct ll_inode_info *lli;
415
416                 if (!inode)
417                         RETURN(0);
418                 lli= ll_i2info(inode);
419                 if (!lli)
420                         RETURN(0);
421                 if (!lli->lli_smd)
422                         RETURN(0);
423
424                 ll_pgcache_remove_extent(inode, lli->lli_smd, lock);
425                 //ll_try_done_writing(inode);
426                 iput(inode);
427                 break;
428         }
429         default:
430                 LBUG();
431         }
432
433         RETURN(0);
434 }
435
436 /*
437  * some callers, notably truncate, really don't want i_size set based
438  * on the the size returned by the getattr, or lock acquisition in
439  * the future.
440  */
441 int ll_extent_lock_no_validate(struct ll_file_data *fd, struct inode *inode,
442                    struct lov_stripe_md *lsm,
443                    int mode, struct ldlm_extent *extent,
444                    struct lustre_handle *lockh, int ast_flags)
445 {
446         struct ll_sb_info *sbi = ll_i2sbi(inode);
447         int rc;
448         ENTRY;
449
450         LASSERT(lockh->cookie == 0);
451
452         /* XXX phil: can we do this?  won't it screw the file size up? */
453         if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
454             (sbi->ll_flags & LL_SBI_NOLCK))
455                 RETURN(0);
456
457         CDEBUG(D_DLMTRACE, "Locking inode %lu, start "LPU64" end "LPU64"\n",
458                inode->i_ino, extent->start, extent->end);
459
460         rc = obd_enqueue(sbi->ll_osc_exp, lsm, NULL, LDLM_EXTENT, extent,
461                          sizeof(extent), mode, &ast_flags,
462                          ll_extent_lock_callback, inode, lockh);
463         if (rc > 0)
464                 rc = -EIO;
465         RETURN(rc);
466 }
467
468 /*
469  * this grabs a lock and manually implements behaviour that makes it look like
470  * the OST is returning the file size with each lock acquisition.
471  */
472 int ll_extent_lock(struct ll_file_data *fd, struct inode *inode,
473                    struct lov_stripe_md *lsm, int mode,
474                    struct ldlm_extent *extent, struct lustre_handle *lockh)
475 {
476         struct ll_inode_info *lli = ll_i2info(inode);
477         struct obd_export *exp = ll_i2obdexp(inode);
478         struct ldlm_extent size_lock;
479         struct lustre_handle match_lockh = {0};
480         struct obdo oa;
481         obd_flag refresh_valid;
482         int flags, rc, matched;
483         ENTRY;
484
485         rc = ll_extent_lock_no_validate(fd, inode, lsm, mode, extent, lockh, 0);
486         if (rc != ELDLM_OK)
487                 RETURN(rc);
488
489         if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
490                 RETURN(0);
491
492         rc = ll_lsm_getattr(exp, lsm, &oa);
493         if (rc) {
494                 ll_extent_unlock(fd, inode, lsm, mode, lockh);
495                 RETURN(rc);
496         }
497
498         /* We set this flag in commit write as we extend the file size.  When
499          * the bit is set and the lock is canceled that covers the file size,
500          * we clear the bit.  This is enough to protect the window where our
501          * local size extension is needed for writeback.  However, it relies on
502          * behaviour that won't be true in the near future.  This assumes that
503          * all getattr callers get extent locks, which they currnetly do.  It
504          * also assumes that we only send discarding asts for {0,eof} truncates
505          * as is currently the case.  This will have to be replaced by the
506          * proper eoc communication between clients and the ost, which is on
507          * its way. */
508         refresh_valid = (OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME | 
509                          OBD_MD_FLCTIME | OBD_MD_FLSIZE);
510         if (test_bit(LLI_F_PREFER_EXTENDED_SIZE, &lli->lli_flags)) {
511                 if (oa.o_size < inode->i_size)
512                         refresh_valid &= ~OBD_MD_FLSIZE;
513                 else 
514                         clear_bit(LLI_F_PREFER_EXTENDED_SIZE, &lli->lli_flags);
515         }
516         obdo_refresh_inode(inode, &oa, refresh_valid);
517
518         CDEBUG(D_INODE, "objid "LPX64" size %Lu, blocks %lu, blksize %lu\n",
519                lsm->lsm_object_id, inode->i_size, inode->i_blocks,
520                inode->i_blksize);
521
522         size_lock.start = inode->i_size;
523         size_lock.end = OBD_OBJECT_EOF;
524
525         /* XXX I bet we should be checking the lock ignore flags.. */
526         flags = LDLM_FL_CBPENDING | LDLM_FL_BLOCK_GRANTED;
527         matched = obd_match(exp, lsm, LDLM_EXTENT, &size_lock,
528                             sizeof(size_lock), LCK_PR, &flags, inode,
529                             &match_lockh);
530
531         /* hey, alright, we hold a size lock that covers the size we
532          * just found, its not going to change for a while.. */
533         if (matched == 1) {
534                 set_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags);
535                 obd_cancel(exp, lsm, LCK_PR, &match_lockh);
536         }
537
538         RETURN(0);
539 }
540
541 int ll_extent_unlock(struct ll_file_data *fd, struct inode *inode,
542                      struct lov_stripe_md *lsm, int mode,
543                      struct lustre_handle *lockh)
544 {
545         struct ll_sb_info *sbi = ll_i2sbi(inode);
546         int rc;
547         ENTRY;
548
549         /* XXX phil: can we do this?  won't it screw the file size up? */
550         if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
551             (sbi->ll_flags & LL_SBI_NOLCK))
552                 RETURN(0);
553
554         rc = obd_cancel(sbi->ll_osc_exp, lsm, mode, lockh);
555
556         RETURN(rc);
557 }
558
559 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
560                             loff_t *ppos)
561 {
562         struct ll_file_data *fd = filp->private_data;
563         struct inode *inode = filp->f_dentry->d_inode;
564         struct ll_inode_info *lli = ll_i2info(inode);
565         struct lov_stripe_md *lsm = lli->lli_smd;
566         struct lustre_handle lockh = { 0 };
567         struct ldlm_extent extent;
568         ldlm_error_t err;
569         ssize_t retval;
570         ENTRY;
571         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),size="LPSZ",offset=%Ld\n",
572                inode->i_ino, inode->i_generation, inode, count, *ppos);
573
574         /* "If nbyte is 0, read() will return 0 and have no other results."
575          *                      -- Single Unix Spec */
576         if (count == 0)
577                 RETURN(0);
578
579         lprocfs_counter_add(ll_i2sbi(inode)->ll_stats, LPROC_LL_READ_BYTES,
580                             count);
581
582         if (!lsm)
583                 RETURN(0);
584
585         /* grab a -> eof extent to push extending writes out of node's caches
586          * so we can see them at the getattr after lock acquisition.  this will
587          * turn into a seperate [*ppos + count, EOF] 'size intent' lock attempt
588          * in the future. */
589         extent.start = *ppos;
590         extent.end = OBD_OBJECT_EOF;
591
592         err = ll_extent_lock(fd, inode, lsm, LCK_PR, &extent, &lockh);
593         if (err != ELDLM_OK)
594                 RETURN(err);
595
596         CDEBUG(D_INFO, "Reading inode %lu, "LPSZ" bytes, offset %Ld\n",
597                inode->i_ino, count, *ppos);
598 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
599         filp->f_ramax = 0; /* turn off generic_file_readahead() */
600 #else
601         filp->f_ra.ra_pages = 0;
602 #endif
603         retval = generic_file_read(filp, buf, count, ppos);
604
605         /* XXX errors? */
606         ll_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
607         RETURN(retval);
608 }
609
610 /*
611  * Write to a file (through the page cache).
612  */
613 static ssize_t ll_file_write(struct file *file, const char *buf, size_t count,
614                              loff_t *ppos)
615 {
616         struct ll_file_data *fd = file->private_data;
617         struct inode *inode = file->f_dentry->d_inode;
618         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
619         struct lustre_handle lockh = { 0 };
620         struct ldlm_extent extent;
621         loff_t maxbytes = ll_file_maxbytes(inode);
622         ldlm_error_t err;
623         ssize_t retval;
624         char should_validate = 1;
625         ENTRY;
626         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),size="LPSZ",offset=%Ld\n",
627                inode->i_ino, inode->i_generation, inode, count, *ppos);
628
629         SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
630
631         /* POSIX, but surprised the VFS doesn't check this already */
632         if (count == 0)
633                 RETURN(0);
634
635         LASSERT(lsm);
636
637         if (file->f_flags & O_APPEND) {
638                 extent.start = 0;
639                 extent.end = OBD_OBJECT_EOF;
640         } else  {
641                 extent.start = *ppos;
642                 extent.end = *ppos + count - 1;
643                 /* we really don't care what i_size is if we're doing
644                  * fully page aligned writes */
645                 if ((*ppos & ~PAGE_CACHE_MASK) == 0 &&
646                     (count & ~PAGE_CACHE_MASK) == 0)
647                         should_validate = 0;
648         }
649
650         if (should_validate)
651                 err = ll_extent_lock(fd, inode, lsm, LCK_PW, &extent, &lockh);
652         else
653                 err = ll_extent_lock_no_validate(fd, inode, lsm, LCK_PW,
654                                                  &extent, &lockh, 0);
655         if (err != ELDLM_OK)
656                 RETURN(err);
657
658         /* this is ok, g_f_w will overwrite this under i_sem if it races
659          * with a local truncate, it just makes our maxbyte checking easier */
660         if (file->f_flags & O_APPEND)
661                 *ppos = inode->i_size;
662
663         if (*ppos >= maxbytes) {
664                 if (count || *ppos > maxbytes) {
665                         send_sig(SIGXFSZ, current, 0);
666                         GOTO(out, retval = -EFBIG);
667                 }
668         }
669         if (*ppos + count > maxbytes)
670                 count = maxbytes - *ppos;
671
672         CDEBUG(D_INFO, "Writing inode %lu, "LPSZ" bytes, offset %Lu\n",
673                inode->i_ino, count, *ppos);
674
675         /* generic_file_write handles O_APPEND after getting i_sem */
676         retval = generic_file_write(file, buf, count, ppos);
677
678 out:
679         /* XXX errors? */
680         lprocfs_counter_add(ll_i2sbi(inode)->ll_stats, LPROC_LL_WRITE_BYTES,
681                             retval);
682         ll_extent_unlock(fd, inode, lsm, LCK_PW, &lockh);
683         RETURN(retval);
684 }
685
686 static int ll_lov_setstripe(struct inode *inode, struct file *file,
687                             unsigned long arg)
688 {
689         struct ll_inode_info *lli = ll_i2info(inode);
690         struct file *f;
691         struct obd_export *exp = ll_i2obdexp(inode);
692         struct lov_stripe_md *lsm;
693         struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = FMODE_WRITE};
694         struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
695         struct ptlrpc_request *req = NULL;
696         struct lustre_md md;
697         int rc;
698         ENTRY;
699
700         /* Bug 1152: copy properly when this is no longer true */
701         LASSERT(sizeof(lum) == sizeof(*lump));
702         LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
703         rc = copy_from_user(&lum, lump, sizeof(lum));
704         if (rc)
705                 RETURN(-EFAULT);
706
707         down(&lli->lli_open_sem);
708         lsm = lli->lli_smd;
709         if (lsm) {
710                 up(&lli->lli_open_sem);
711                 CDEBUG(D_IOCTL, "stripe already exists for ino %lu\n",
712                        inode->i_ino);
713                 RETURN(-EEXIST);
714         }
715
716         f = get_empty_filp();
717         if (!f)
718                 GOTO(out, -ENOMEM);
719
720         f->f_dentry = file->f_dentry;
721         f->f_vfsmnt = file->f_vfsmnt;
722
723         rc = ll_intent_file_open(f, &lum, sizeof(lum), &oit);
724         if (rc)
725                 GOTO(out, rc);
726         if (it_disposition(&oit, DISP_LOOKUP_NEG))
727                 GOTO(out, -ENOENT);
728         req = oit.d.lustre.it_data;
729         rc = oit.d.lustre.it_status;
730
731         if (rc < 0)
732                 GOTO(out, rc);
733
734         rc = mdc_req2lustre_md(req, 1, exp, &md);
735         if (rc)
736                 GOTO(out, rc);
737         ll_update_inode(f->f_dentry->d_inode, md.body, md.lsm);
738
739         rc = ll_local_open(f, &oit);
740         if (rc)
741                 GOTO(out, rc);
742         ll_intent_release(&oit);
743
744         rc = ll_file_release(f->f_dentry->d_inode, f);
745
746  out:
747         if (f)
748                 put_filp(f);
749         up(&lli->lli_open_sem);
750         if (req != NULL)
751                 ptlrpc_req_finished(req);
752         RETURN(rc);
753 }
754
755 static int ll_lov_getstripe(struct inode *inode, unsigned long arg)
756 {
757         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
758
759         if (!lsm)
760                 RETURN(-ENODATA);
761
762         return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2obdexp(inode), 0, lsm,
763                             (void *)arg);
764 }
765
766 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
767                   unsigned long arg)
768 {
769         struct ll_file_data *fd = file->private_data;
770         int flags;
771         ENTRY;
772
773         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),cmd=%x\n", inode->i_ino,
774                inode->i_generation, inode, cmd);
775
776         if (_IOC_TYPE(cmd) == 'T') /* tty ioctls */
777                 RETURN(-ENOTTY);
778
779         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_IOCTL);
780         switch(cmd) {
781         case LL_IOC_GETFLAGS:
782                 /* Get the current value of the file flags */
783                 return put_user(fd->fd_flags, (int *)arg);
784         case LL_IOC_SETFLAGS:
785         case LL_IOC_CLRFLAGS:
786                 /* Set or clear specific file flags */
787                 /* XXX This probably needs checks to ensure the flags are
788                  *     not abused, and to handle any flag side effects.
789                  */
790                 if (get_user(flags, (int *) arg))
791                         RETURN(-EFAULT);
792
793                 if (cmd == LL_IOC_SETFLAGS)
794                         fd->fd_flags |= flags;
795                 else
796                         fd->fd_flags &= ~flags;
797                 RETURN(0);
798         case LL_IOC_LOV_SETSTRIPE:
799                 RETURN(ll_lov_setstripe(inode, file, arg));
800         case LL_IOC_LOV_GETSTRIPE:
801                 RETURN(ll_lov_getstripe(inode, arg));
802         case EXT3_IOC_GETFLAGS:
803         case EXT3_IOC_SETFLAGS:
804                 RETURN( ll_iocontrol(inode, file, cmd, arg) );
805         /* We need to special case any other ioctls we want to handle,
806          * to send them to the MDS/OST as appropriate and to properly
807          * network encode the arg field.
808         case EXT2_IOC_GETVERSION_OLD:
809         case EXT2_IOC_GETVERSION_NEW:
810         case EXT2_IOC_SETVERSION_OLD:
811         case EXT2_IOC_SETVERSION_NEW:
812         */
813         default:
814                 RETURN( obd_iocontrol(cmd, ll_i2obdexp(inode), 0, NULL,
815                                       (void *)arg) );
816         }
817 }
818
819 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
820 {
821         struct inode *inode = file->f_dentry->d_inode;
822         struct ll_file_data *fd = file->private_data;
823         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
824         struct lustre_handle lockh = {0};
825         loff_t retval;
826         ENTRY;
827         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),to=%llu\n", inode->i_ino,
828                inode->i_generation, inode,
829                offset + ((origin==2) ? inode->i_size : file->f_pos));
830
831         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_LLSEEK);
832         if (origin == 2) { /* SEEK_END */
833                 ldlm_error_t err;
834                 struct ldlm_extent extent = {0, OBD_OBJECT_EOF};
835                 err = ll_extent_lock(fd, inode, lsm, LCK_PR, &extent, &lockh);
836                 if (err != ELDLM_OK)
837                         RETURN(err);
838
839                 offset += inode->i_size;
840         } else if (origin == 1) { /* SEEK_CUR */
841                 offset += file->f_pos;
842         }
843
844         retval = -EINVAL;
845         if (offset >= 0 && offset <= ll_file_maxbytes(inode)) {
846                 if (offset != file->f_pos) {
847                         file->f_pos = offset;
848 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
849                         file->f_reada = 0;
850                         file->f_version = ++event;
851 #endif
852                 }
853                 retval = offset;
854         }
855
856         if (origin == 2)
857                 ll_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
858         RETURN(retval);
859 }
860
861 int ll_fsync(struct file *file, struct dentry *dentry, int data)
862 {
863         struct inode *inode = dentry->d_inode;
864         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
865         struct ll_fid fid;
866         struct ptlrpc_request *req;
867         int rc, err;
868         ENTRY;
869         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
870                inode->i_generation, inode);
871
872         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_FSYNC);
873
874         /* fsync's caller has already called _fdata{sync,write}, we want
875          * that IO to finish before calling the osc and mdc sync methods */
876         rc = filemap_fdatawait(inode->i_mapping);
877
878         ll_inode2fid(&fid, inode);
879         err = mdc_sync(ll_i2sbi(inode)->ll_mdc_exp, &fid, &req);
880         if (!rc)
881                 rc = err;
882         if (!err)
883                 ptlrpc_req_finished(req);
884
885         if (data && lsm) {
886                 struct obdo *oa = obdo_alloc();
887
888                 if (!oa)
889                         RETURN(rc ? rc : -ENOMEM);
890
891                 oa->o_id = lsm->lsm_object_id;
892                 oa->o_valid = OBD_MD_FLID;
893                 obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
894                                            OBD_MD_FLMTIME | OBD_MD_FLCTIME);
895
896                 err = obd_sync(ll_i2sbi(inode)->ll_osc_exp, oa, lsm,
897                                0, OBD_OBJECT_EOF);
898                 if (!rc)
899                         rc = err;
900                 obdo_free(oa);
901         }
902
903         RETURN(rc);
904 }
905
906 int ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
907 {
908         struct inode *inode = file->f_dentry->d_inode;
909         struct ll_sb_info *sbi = ll_i2sbi(inode);
910         struct obd_device *obddev;
911         struct ldlm_res_id res_id =
912                     { .name = {inode->i_ino, inode->i_generation, LDLM_FLOCK} };
913         struct lustre_handle lockh = {0};
914         struct ldlm_flock flock;
915         ldlm_mode_t mode = 0;
916         int flags = 0;
917         int rc;
918         ENTRY;
919
920         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu file_lock=%p\n",
921                inode->i_ino, file_lock);
922
923         flock.pid = file_lock->fl_pid;
924         flock.start = file_lock->fl_start;
925         flock.end = file_lock->fl_end;
926
927         switch (file_lock->fl_type) {
928         case F_RDLCK:
929                 mode = LCK_PR;
930                 break;
931         case F_UNLCK:
932                 /* An unlock request may or may not have any relation to
933                  * existing locks so we may not be able to pass a lock handle
934                  * via a normal ldlm_lock_cancel() request. The request may even
935                  * unlock a byte range in the middle of an existing lock. In
936                  * order to process an unlock request we need all of the same
937                  * information that is given with a normal read or write record
938                  * lock request. To avoid creating another ldlm unlock (cancel)
939                  * message we'll treat a LCK_NL flock request as an unlock. */
940                 mode = LCK_NL;
941                 break;
942         case F_WRLCK:
943                 mode = LCK_PW;
944                 break;
945         default:
946                 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
947                 LBUG();
948         }
949
950         switch (cmd) {
951         case F_SETLKW:
952                 flags = 0;
953                 break;
954         case F_SETLK:
955                 flags = LDLM_FL_BLOCK_NOWAIT;
956                 break;
957         case F_GETLK:
958                 flags = LDLM_FL_TEST_LOCK;
959                 /* Save the old mode so that if the mode in the lock changes we
960                  * can decrement the appropriate reader or writer refcount. */
961                 file_lock->fl_type = mode;
962                 break;
963         default:
964                 CERROR("unknown fcntl lock command: %d\n", cmd);
965                 LBUG();
966         }
967
968         CDEBUG(D_DLMTRACE, "inode=%lu, pid=%u, flags=%#x, mode=%u, "
969                "start="LPU64", end="LPU64"\n", inode->i_ino, flock.pid,
970                flags, mode, flock.start, flock.end);
971
972         obddev = sbi->ll_mdc_exp->exp_obd;
973         rc = ldlm_cli_enqueue(sbi->ll_mdc_exp, NULL, obddev->obd_namespace,
974                               NULL, res_id, LDLM_FLOCK, &flock, sizeof(flock),
975                               mode, &flags, ldlm_flock_completion_ast, NULL,
976                               file_lock, &lockh);
977         RETURN(rc);
978 }
979
980 static int ll_have_md_lock(struct dentry *de)
981 {
982         struct ll_sb_info *sbi = ll_s2sbi(de->d_sb);
983         struct lustre_handle lockh;
984         struct ldlm_res_id res_id = { .name = {0} };
985         struct obd_device *obddev;
986         int flags;
987         ENTRY;
988
989         if (!de->d_inode)
990                RETURN(0);
991
992         obddev = sbi->ll_mdc_exp->exp_obd;
993         res_id.name[0] = de->d_inode->i_ino;
994         res_id.name[1] = de->d_inode->i_generation;
995
996         CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id.name[0]);
997
998         flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
999         if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_PLAIN,
1000                             NULL, 0, LCK_PR, &lockh)) {
1001                 ldlm_lock_decref(&lockh, LCK_PR);
1002                 RETURN(1);
1003         }
1004
1005         if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_PLAIN,
1006                             NULL, 0, LCK_PW, &lockh)) {
1007                 ldlm_lock_decref(&lockh, LCK_PW);
1008                 RETURN(1);
1009         }
1010         RETURN(0);
1011 }
1012
1013 int ll_inode_revalidate_it(struct dentry *dentry, struct lookup_intent *it)
1014 {
1015         struct inode *inode = dentry->d_inode;
1016         struct lov_stripe_md *lsm;
1017         ENTRY;
1018
1019         if (!inode) {
1020                 CERROR("REPORT THIS LINE TO PETER\n");
1021                 RETURN(0);
1022         }
1023         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),name=%s\n",
1024                inode->i_ino, inode->i_generation, inode, dentry->d_name.name);
1025 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0))
1026         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_REVALIDATE);
1027 #endif
1028
1029         if (!ll_have_md_lock(dentry)) {
1030                 struct ptlrpc_request *req = NULL;
1031                 struct ll_sb_info *sbi = ll_i2sbi(dentry->d_inode);
1032                 struct ll_fid fid;
1033                 unsigned long valid = 0;
1034                 int rc, ealen = 0;
1035
1036                 if (S_ISREG(inode->i_mode)) {
1037                         ealen = obd_size_diskmd(sbi->ll_osc_exp, NULL);
1038                         valid |= OBD_MD_FLEASIZE;
1039                 }
1040                 ll_inode2fid(&fid, inode);
1041                 rc = mdc_getattr(sbi->ll_mdc_exp, &fid, valid, ealen, &req);
1042                 if (rc) {
1043                         CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1044                         RETURN(-abs(rc));
1045                 }
1046                 rc = ll_prep_inode(sbi->ll_osc_exp, &inode, req, 0, NULL);
1047                 if (rc) {
1048                         ptlrpc_req_finished(req);
1049                         RETURN(rc);
1050                 }
1051                 ptlrpc_req_finished(req);
1052         }
1053
1054 #if 0
1055         if (ll_have_md_lock(dentry) &&
1056             test_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &ll_i2info(inode)->lli_flags))
1057                 RETURN(0);
1058 #endif
1059
1060         lsm = ll_i2info(inode)->lli_smd;
1061         if (!lsm)       /* object not yet allocated, don't validate size */
1062                 RETURN(0);
1063
1064         /* unfortunately stat comes in through revalidate and we don't
1065          * differentiate this use from initial instantiation.  we're
1066          * also being wildly conservative and flushing write caches
1067          * so that stat really returns the proper size. */
1068         {
1069                 struct ldlm_extent extent = {0, OBD_OBJECT_EOF};
1070                 struct lustre_handle lockh = {0};
1071                 ldlm_error_t err;
1072
1073                 err = ll_extent_lock(NULL, inode, lsm, LCK_PR, &extent, &lockh);
1074                 if (err != ELDLM_OK)
1075                         RETURN(err);
1076
1077                 ll_extent_unlock(NULL, inode, lsm, LCK_PR, &lockh);
1078         }
1079         RETURN(0);
1080 }
1081
1082 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1083 int ll_getattr(struct vfsmount *mnt, struct dentry *de,
1084                struct lookup_intent *it, struct kstat *stat)
1085 {
1086         int res = 0;
1087         struct inode *inode = de->d_inode;
1088
1089         res = ll_inode_revalidate_it(de, it);
1090         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_GETATTR);
1091
1092         if (res)
1093                 return res;
1094
1095         stat->dev = inode->i_sb->s_dev;
1096         stat->ino = inode->i_ino;
1097         stat->mode = inode->i_mode;
1098         stat->nlink = inode->i_nlink;
1099         stat->uid = inode->i_uid;
1100         stat->gid = inode->i_gid;
1101         stat->rdev = kdev_t_to_nr(inode->i_rdev);
1102         stat->atime = inode->i_atime;
1103         stat->mtime = inode->i_mtime;
1104         stat->ctime = inode->i_ctime;
1105         stat->size = inode->i_size;
1106         stat->blksize = inode->i_blksize;
1107         stat->blocks = inode->i_blocks;
1108         return 0;
1109 }
1110 #endif
1111
1112
1113 struct file_operations ll_file_operations = {
1114         read:           ll_file_read,
1115         write:          ll_file_write,
1116         ioctl:          ll_file_ioctl,
1117         open:           ll_file_open,
1118         release:        ll_file_release,
1119         mmap:           generic_file_mmap,
1120         llseek:         ll_file_seek,
1121         fsync:          ll_fsync,
1122         //lock:           ll_file_flock
1123 };
1124
1125 struct inode_operations ll_file_inode_operations = {
1126         setattr_raw:    ll_setattr_raw,
1127         setattr:        ll_setattr,
1128         truncate:       ll_truncate,
1129 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1130         getattr_it:     ll_getattr,
1131 #else
1132         revalidate_it:  ll_inode_revalidate_it,
1133 #endif
1134 };
1135