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