Whamcloud - gitweb
file lustre_types.h was added on branch b1_4_mountconf on 2006-04-26 18:45:44 +0000
[fs/lustre-release.git] / lustre / obdfilter / filter_io_26.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  linux/fs/obdfilter/filter_io.c
5  *
6  *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
7  *   Author: Peter Braam <braam@clusterfs.com>
8  *   Author: Andreas Dilger <adilger@clusterfs.com>
9  *   Author: Phil Schwan <phil@clusterfs.com>
10  *
11  *   This file is part of the Lustre file system, http://www.lustre.org
12  *   Lustre is a trademark of Cluster File Systems, Inc.
13  *
14  *   You may have signed or agreed to another license before downloading
15  *   this software.  If so, you are bound by the terms and conditions
16  *   of that agreement, and the following does not apply to you.  See the
17  *   LICENSE file included with this distribution for more information.
18  *
19  *   If you did not agree to a different license, then this copy of Lustre
20  *   is open source software; you can redistribute it and/or modify it
21  *   under the terms of version 2 of the GNU General Public License as
22  *   published by the Free Software Foundation.
23  *
24  *   In either case, Lustre is distributed in the hope that it will be
25  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
26  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  *   license text for more details.
28  */
29
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/pagemap.h> // XXX kill me soon
33 #include <linux/version.h>
34 #include <linux/buffer_head.h>
35
36 #define DEBUG_SUBSYSTEM S_FILTER
37
38 #include <linux/obd_class.h>
39 #include <linux/lustre_fsfilt.h>
40 #include <linux/lustre_quota.h>
41 #include "filter_internal.h"
42
43 /* 512byte block min */
44 #define MAX_BLOCKS_PER_PAGE (PAGE_SIZE / 512)
45 struct filter_iobuf {
46         atomic_t          dr_numreqs;  /* number of reqs being processed */
47         struct bio       *dr_bios;     /* list of completed bios */
48         wait_queue_head_t dr_wait;
49         int               dr_max_pages;
50         int               dr_npages;
51         int               dr_error;
52         struct page     **dr_pages;
53         unsigned long    *dr_blocks;
54         spinlock_t        dr_lock;
55         unsigned long     dr_start_time; /* jiffies */
56         unsigned int      dr_ignore_quota:1;
57         struct filter_obd *dr_filter;
58 };
59
60 static void record_start_io(struct filter_iobuf *iobuf, int rw, int size)
61 {
62         struct filter_obd *filter = iobuf->dr_filter;
63         unsigned long flags;
64
65         atomic_inc(&iobuf->dr_numreqs);
66
67         if (rw == OBD_BRW_READ) {
68                 lprocfs_oh_tally(&filter->fo_read_rpc_hist,
69                                  filter->fo_r_in_flight);
70                 lprocfs_oh_tally_log2(&filter->fo_r_disk_iosize, size);
71         } else {
72                 lprocfs_oh_tally(&filter->fo_write_rpc_hist,
73                                  filter->fo_w_in_flight);
74                 lprocfs_oh_tally_log2(&filter->fo_w_disk_iosize, size);
75         }
76         spin_lock_irqsave(&filter->fo_stats_lock, flags);
77         if (rw == OBD_BRW_READ)
78                 filter->fo_r_in_flight++;
79         else
80                 filter->fo_w_in_flight++;
81         spin_unlock_irqrestore(&filter->fo_stats_lock, flags);
82         iobuf->dr_start_time = jiffies;
83 }
84
85 static void record_finish_io(struct filter_iobuf *iobuf, int rw, int rc)
86 {
87         struct filter_obd *filter = iobuf->dr_filter;
88         unsigned long flags, stop_time = jiffies;
89
90         spin_lock_irqsave(&filter->fo_stats_lock, flags);
91         if (rw == OBD_BRW_READ)
92                 filter->fo_r_in_flight--;
93         else
94                 filter->fo_w_in_flight--;
95         spin_unlock_irqrestore(&filter->fo_stats_lock, flags);
96
97         if (atomic_dec_and_test(&iobuf->dr_numreqs))
98                 wake_up(&iobuf->dr_wait);
99
100         if (rc != 0)
101                 return;
102
103         if (rw == OBD_BRW_READ) {
104                 lprocfs_oh_tally_log2(&filter->fo_r_io_time,
105                                       stop_time - iobuf->dr_start_time);
106         } else {
107                 lprocfs_oh_tally_log2(&filter->fo_w_io_time,
108                                       stop_time - iobuf->dr_start_time);
109         }
110 }
111
112 static int dio_complete_routine(struct bio *bio, unsigned int done, int error)
113 {
114         struct filter_iobuf *iobuf = bio->bi_private;
115         unsigned long flags;
116
117         if (bio->bi_size) {
118                 CWARN("gets called against non-complete bio 0x%p: %d/%d/%d\n",
119                       bio, bio->bi_size, done, error);
120                 return 1;
121         }
122
123         if (iobuf == NULL) {
124                 CERROR("***** bio->bi_private is NULL!  This should never "
125                        "happen.  Normally, I would crash here, but instead I "
126                        "will dump the bio contents to the console.  Please "
127                        "report this to CFS, along with any interesting "
128                        "messages leading up to this point (like SCSI errors, "
129                        "perhaps).  Because bi_private is NULL, I can't wake up "
130                        "the thread that initiated this I/O -- so you will "
131                        "probably have to reboot this node.\n");
132                 CERROR("bi_next: %p, bi_flags: %lx, bi_rw: %lu, bi_vcnt: %d, "
133                        "bi_idx: %d, bi->size: %d, bi_end_io: %p, bi_cnt: %d, "
134                        "bi_private: %p\n", bio->bi_next, bio->bi_flags,
135                        bio->bi_rw, bio->bi_vcnt, bio->bi_idx, bio->bi_size,
136                        bio->bi_end_io, atomic_read(&bio->bi_cnt),
137                        bio->bi_private);
138                 return 0;
139         }
140
141         spin_lock_irqsave(&iobuf->dr_lock, flags);
142         bio->bi_private = iobuf->dr_bios;
143         iobuf->dr_bios = bio;
144         if (iobuf->dr_error == 0)
145                 iobuf->dr_error = error;
146         spin_unlock_irqrestore(&iobuf->dr_lock, flags);
147
148         record_finish_io(iobuf, test_bit(BIO_RW, &bio->bi_rw) ?
149                          OBD_BRW_WRITE : OBD_BRW_READ, error);
150
151         return 0;
152 }
153
154 static int can_be_merged(struct bio *bio, sector_t sector)
155 {
156         unsigned int size;
157
158         if (!bio)
159                 return 0;
160
161         size = bio->bi_size >> 9;
162         return bio->bi_sector + size == sector ? 1 : 0;
163 }
164
165 struct filter_iobuf *filter_alloc_iobuf(struct filter_obd *filter,
166                                         int rw, int num_pages)
167 {
168         struct filter_iobuf *iobuf;
169
170         LASSERTF(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ, "%x\n", rw);
171
172         OBD_ALLOC(iobuf, sizeof(*iobuf));
173         if (iobuf == NULL)
174                 goto failed_0;
175
176         OBD_ALLOC(iobuf->dr_pages, num_pages * sizeof(*iobuf->dr_pages));
177         if (iobuf->dr_pages == NULL)
178                 goto failed_1;
179
180         OBD_ALLOC(iobuf->dr_blocks,
181                   MAX_BLOCKS_PER_PAGE * num_pages * sizeof(*iobuf->dr_blocks));
182         if (iobuf->dr_blocks == NULL)
183                 goto failed_2;
184
185         iobuf->dr_filter = filter;
186         iobuf->dr_bios = NULL;
187         init_waitqueue_head(&iobuf->dr_wait);
188         atomic_set(&iobuf->dr_numreqs, 0);
189         spin_lock_init(&iobuf->dr_lock);
190         iobuf->dr_max_pages = num_pages;
191         iobuf->dr_npages = 0;
192
193         RETURN(iobuf);
194
195  failed_2:
196         OBD_FREE(iobuf->dr_pages,
197                  num_pages * sizeof(*iobuf->dr_pages));
198  failed_1:
199         OBD_FREE(iobuf, sizeof(*iobuf));
200  failed_0:
201         RETURN(ERR_PTR(-ENOMEM));
202 }
203
204 static void filter_clear_iobuf(struct filter_iobuf *iobuf)
205 {
206         /* free all bios */
207         while (iobuf->dr_bios) {
208                 struct bio *bio = iobuf->dr_bios;
209                 iobuf->dr_bios = bio->bi_private;
210                 bio_put(bio);
211         }
212         iobuf->dr_npages = 0;
213         atomic_set(&iobuf->dr_numreqs, 0);
214 }
215
216 void filter_free_iobuf(struct filter_iobuf *iobuf)
217 {
218         int num_pages = iobuf->dr_max_pages;
219
220         filter_clear_iobuf(iobuf);
221
222         OBD_FREE(iobuf->dr_blocks,
223                  MAX_BLOCKS_PER_PAGE * num_pages * sizeof(*iobuf->dr_blocks));
224         OBD_FREE(iobuf->dr_pages,
225                  num_pages * sizeof(*iobuf->dr_pages));
226         OBD_FREE_PTR(iobuf);
227 }
228
229 void filter_iobuf_put(struct filter_obd *filter, struct filter_iobuf *iobuf,
230                       struct obd_trans_info *oti)
231 {
232         int thread_id = oti ? oti->oti_thread_id : -1;
233
234         if (unlikely(thread_id < 0)) {
235                 filter_free_iobuf(iobuf);
236                 return;
237         }
238
239         LASSERTF(filter->fo_iobuf_pool[thread_id] == iobuf,
240                  "iobuf mismatch for thread %d: pool %p iobuf %p\n",
241                  thread_id, filter->fo_iobuf_pool[thread_id], iobuf);
242         filter_clear_iobuf(iobuf);
243 }
244
245 int filter_iobuf_add_page(struct obd_device *obd, struct filter_iobuf *iobuf,
246                           struct inode *inode, struct page *page)
247 {
248         LASSERT(iobuf->dr_npages < iobuf->dr_max_pages);
249         iobuf->dr_pages[iobuf->dr_npages++] = page;
250
251         return 0;
252 }
253
254 int filter_do_bio(struct obd_device *obd, struct inode *inode,
255                   struct filter_iobuf *iobuf, int rw)
256 {
257         int            blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
258         struct page  **pages = iobuf->dr_pages;
259         int            npages = iobuf->dr_npages;
260         unsigned long *blocks = iobuf->dr_blocks;
261         int            total_blocks = npages * blocks_per_page;
262         int            sector_bits = inode->i_sb->s_blocksize_bits - 9;
263         unsigned int   blocksize = inode->i_sb->s_blocksize;
264         struct bio    *bio = NULL;
265         struct page   *page;
266         unsigned int   page_offset;
267         sector_t       sector;
268         int            nblocks;
269         int            block_idx;
270         int            page_idx;
271         int            i;
272         int            rc = 0;
273         ENTRY;
274
275         LASSERT(iobuf->dr_npages == npages);
276         LASSERT(total_blocks <= OBDFILTER_CREATED_SCRATCHPAD_ENTRIES);
277
278         for (page_idx = 0, block_idx = 0;
279              page_idx < npages;
280              page_idx++, block_idx += blocks_per_page) {
281
282                 page = pages[page_idx];
283                 LASSERT (block_idx + blocks_per_page <= total_blocks);
284
285                 for (i = 0, page_offset = 0;
286                      i < blocks_per_page;
287                      i += nblocks, page_offset += blocksize * nblocks) {
288
289                         nblocks = 1;
290
291                         if (blocks[block_idx + i] == 0) {  /* hole */
292                                 LASSERT(rw == OBD_BRW_READ);
293                                 memset(kmap(page) + page_offset, 0, blocksize);
294                                 kunmap(page);
295                                 continue;
296                         }
297
298                         sector = blocks[block_idx + i] << sector_bits;
299
300                         /* Additional contiguous file blocks? */
301                         while (i + nblocks < blocks_per_page &&
302                                (sector + nblocks*(blocksize>>9)) ==
303                                (blocks[block_idx + i + nblocks] << sector_bits))
304                                 nblocks++;
305
306                         if (bio != NULL &&
307                             can_be_merged(bio, sector) &&
308                             bio_add_page(bio, page,
309                                          blocksize * nblocks, page_offset) != 0)
310                                 continue;       /* added this frag OK */
311
312                         if (bio != NULL) {
313                                 request_queue_t *q =
314                                         bdev_get_queue(bio->bi_bdev);
315
316                                 /* Dang! I have to fragment this I/O */
317                                 CDEBUG(D_INODE, "bio++ sz %d vcnt %d(%d) "
318                                        "sectors %d(%d) psg %d(%d) hsg %d(%d)\n",
319                                        bio->bi_size,
320                                        bio->bi_vcnt, bio->bi_max_vecs,
321                                        bio->bi_size >> 9, q->max_sectors,
322                                        bio_phys_segments(q, bio),
323                                        q->max_phys_segments,
324                                        bio_hw_segments(q, bio),
325                                        q->max_hw_segments);
326
327                                 record_start_io(iobuf, rw, bio->bi_size);
328                                 rc = fsfilt_send_bio(rw, obd, inode, bio);
329                                 if (rc < 0) {
330                                         CERROR("Can't send bio: %d\n", rc);
331                                         record_finish_io(iobuf, rw, rc);
332                                         goto out;
333                                 }
334                         }
335
336                         /* allocate new bio */
337                         bio = bio_alloc(GFP_NOIO,
338                                         (npages - page_idx) * blocks_per_page);
339                         if (bio == NULL) {
340                                 CERROR ("Can't allocate bio\n");
341                                 rc = -ENOMEM;
342                                 goto out;
343                         }
344
345                         bio->bi_bdev = inode->i_sb->s_bdev;
346                         bio->bi_sector = sector;
347                         bio->bi_end_io = dio_complete_routine;
348                         bio->bi_private = iobuf;
349
350                         rc = bio_add_page(bio, page,
351                                           blocksize * nblocks, page_offset);
352                         LASSERT (rc != 0);
353                 }
354         }
355
356         if (bio != NULL) {
357                 record_start_io(iobuf, rw, bio->bi_size);
358                 rc = fsfilt_send_bio(rw, obd, inode, bio);
359                 if (rc >= 0) {
360                         rc = 0;
361                 } else {
362                         CERROR("Can't send bio: %d\n", rc);
363                         record_finish_io(iobuf, rw, rc);
364                 }
365         }
366
367  out:
368         wait_event(iobuf->dr_wait, atomic_read(&iobuf->dr_numreqs) == 0);
369
370         if (rc == 0)
371                 rc = iobuf->dr_error;
372         RETURN(rc);
373 }
374
375 /* These are our hacks to keep our directio/bh IO coherent with ext3's
376  * page cache use.  Most notably ext3 reads file data into the page
377  * cache when it is zeroing the tail of partial-block truncates and
378  * leaves it there, sometimes generating io from it at later truncates.
379  * This removes the partial page and its buffers from the page cache,
380  * so it should only ever cause a wait in rare cases, as otherwise we
381  * always do full-page IO to the OST.
382  *
383  * The call to truncate_complete_page() will call journal_invalidatepage()
384  * to free the buffers and drop the page from cache.  The buffers should
385  * not be dirty, because we already called fdatasync/fdatawait on them.
386  */
387 static int filter_clear_page_cache(struct inode *inode,
388                                     struct filter_iobuf *iobuf)
389 {
390         struct page *page;
391         int i, rc, rc2;
392
393         /* This is nearly generic_osync_inode, without the waiting on the inode
394         rc = generic_osync_inode(inode, inode->i_mapping,
395                                  OSYNC_DATA|OSYNC_METADATA);
396          */
397         rc = filemap_fdatawrite(inode->i_mapping);
398         rc2 = sync_mapping_buffers(inode->i_mapping);
399         if (rc == 0)
400                 rc = rc2;
401         rc2 = filemap_fdatawait(inode->i_mapping);
402         if (rc == 0)
403                 rc = rc2;
404         if (rc != 0)
405                 RETURN(rc);
406
407         /* be careful to call this after fsync_inode_data_buffers has waited
408          * for IO to complete before we evict it from the cache */
409         for (i = 0; i < iobuf->dr_npages; i++) {
410                 page = find_lock_page(inode->i_mapping,
411                                       iobuf->dr_pages[i]->index);
412                 if (page == NULL)
413                         continue;
414                 if (page->mapping != NULL) {
415                         wait_on_page_writeback(page);
416                         ll_truncate_complete_page(page);
417                 }
418
419                 unlock_page(page);
420                 page_cache_release(page);
421         }
422
423         return 0;
424 }
425
426 /* Must be called with i_sem taken for writes; this will drop it */
427 int filter_direct_io(int rw, struct dentry *dchild, struct filter_iobuf *iobuf,
428                      struct obd_export *exp, struct iattr *attr,
429                      struct obd_trans_info *oti, void **wait_handle)
430 {
431         struct obd_device *obd = exp->exp_obd;
432         struct inode *inode = dchild->d_inode;
433         int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
434         int rc, rc2, create;
435         struct semaphore *sem;
436         ENTRY;
437
438         LASSERTF(iobuf->dr_npages <= iobuf->dr_max_pages, "%d,%d\n",
439                  iobuf->dr_npages, iobuf->dr_max_pages);
440         LASSERT(iobuf->dr_npages <= OBDFILTER_CREATED_SCRATCHPAD_ENTRIES);
441
442         if (rw == OBD_BRW_READ) {
443                 if (iobuf->dr_npages == 0)
444                         RETURN(0);
445                 create = 0;
446                 sem = NULL;
447         } else {
448                 LASSERTF(rw == OBD_BRW_WRITE, "%x\n", rw);
449                 LASSERT(iobuf->dr_npages > 0);
450                 create = 1;
451                 sem = &obd->u.filter.fo_alloc_lock;
452                 
453                 lquota_enforce(quota_interface, obd, iobuf->dr_ignore_quota);
454         }
455 remap:
456         rc = fsfilt_map_inode_pages(obd, inode, iobuf->dr_pages,
457                                     iobuf->dr_npages, iobuf->dr_blocks,
458                                     obdfilter_created_scratchpad, create, sem);
459
460         if (rc == -EDQUOT) {
461                 LASSERT(rw == OBD_BRW_WRITE &&
462                         !cap_raised(current->cap_effective, CAP_SYS_RESOURCE));
463
464                 /* Unfortunately, if quota master is too busy to handle the
465                  * pre-dqacq in time or this user has exceeded quota limit, we
466                  * have to wait for the completion of in flight dqacq/dqrel,
467                  * then try again */
468                 if (lquota_acquire(quota_interface, obd, inode->i_uid,
469                                    inode->i_gid))
470                         goto remap;
471         }
472
473         if (rw == OBD_BRW_WRITE) {
474                 if (rc == 0) {
475                         filter_tally_write(&obd->u.filter,
476                                            iobuf->dr_pages,
477                                            iobuf->dr_npages,
478                                            iobuf->dr_blocks,
479                                            blocks_per_page);
480                         if (attr->ia_size > inode->i_size)
481                                 attr->ia_valid |= ATTR_SIZE;
482                         rc = fsfilt_setattr(obd, dchild,
483                                             oti->oti_handle, attr, 0);
484                 }
485
486                 up(&inode->i_sem);
487
488                 rc2 = filter_finish_transno(exp, oti, 0);
489                 if (rc2 != 0) {
490                         CERROR("can't close transaction: %d\n", rc2);
491                         if (rc == 0)
492                                 rc = rc2;
493                 }
494
495                 rc2 =fsfilt_commit_async(obd,inode,oti->oti_handle,wait_handle);
496                 if (rc == 0)
497                         rc = rc2;
498                 if (rc != 0)
499                         RETURN(rc);
500         }
501
502         rc = filter_clear_page_cache(inode, iobuf);
503         if (rc != 0)
504                 RETURN(rc);
505
506         RETURN(filter_do_bio(obd, inode, iobuf, rw));
507 }
508
509 /* See if there are unallocated parts in given file region */
510 static int filter_range_is_mapped(struct inode *inode, obd_size offset, int len)
511 {
512         sector_t (*fs_bmap)(struct address_space *, sector_t) =
513                 inode->i_mapping->a_ops->bmap;
514         int j;
515
516         /* We can't know if we are overwriting or not */
517         if (fs_bmap == NULL)
518                 return 0;
519
520         offset >>= inode->i_blkbits;
521         len >>= inode->i_blkbits;
522
523         for (j = 0; j <= len; j++)
524                 if (fs_bmap(inode->i_mapping, offset + j) == 0)
525                         return 0;
526
527         return 1;
528 }
529
530 int filter_commitrw_write(struct obd_export *exp, struct obdo *oa,
531                           int objcount, struct obd_ioobj *obj, int niocount,
532                           struct niobuf_local *res, struct obd_trans_info *oti,
533                           int rc)
534 {
535         struct niobuf_local *lnb;
536         struct filter_iobuf *iobuf = NULL;
537         struct lvfs_run_ctxt saved;
538         struct fsfilt_objinfo fso;
539         struct iattr iattr = { 0 };
540         struct inode *inode = NULL;
541         unsigned long now = jiffies;
542         int i, err, cleanup_phase = 0;
543         struct obd_device *obd = exp->exp_obd;
544         void *wait_handle;
545         int   total_size = 0;
546         unsigned int qcids[MAXQUOTAS] = {0, 0};
547         ENTRY;
548
549         LASSERT(oti != NULL);
550         LASSERT(objcount == 1);
551         LASSERT(current->journal_info == NULL);
552
553         if (rc != 0)
554                 GOTO(cleanup, rc);
555
556         iobuf = filter_iobuf_get(&obd->u.filter, oti);
557         cleanup_phase = 1;
558
559         fso.fso_dentry = res->dentry;
560         fso.fso_bufcnt = obj->ioo_bufcnt;
561         inode = res->dentry->d_inode;
562
563         iobuf->dr_ignore_quota = 0;
564         for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++) {
565                 loff_t this_size;
566
567                 /* If overwriting an existing block, we don't need a grant */
568                 if (!(lnb->flags & OBD_BRW_GRANTED) && lnb->rc == -ENOSPC &&
569                     filter_range_is_mapped(inode, lnb->offset, lnb->len))
570                         lnb->rc = 0;
571
572                 if (lnb->rc) { /* ENOSPC, network RPC error, etc. */
573                         CDEBUG(D_INODE, "Skipping [%d] == %d\n", i, lnb->rc);
574                         continue;
575                 }
576
577                 err = filter_iobuf_add_page(obd, iobuf, inode, lnb->page);
578                 LASSERT (err == 0);
579
580                 total_size += lnb->len;
581
582                 /* we expect these pages to be in offset order, but we'll
583                  * be forgiving */
584                 this_size = lnb->offset + lnb->len;
585                 if (this_size > iattr.ia_size)
586                         iattr.ia_size = this_size;
587                 
588                 /* if one page is a write-back page from client cache, or it's
589                  * written by root, then mark the whole io request as ignore 
590                  * quota request */
591                 if (lnb->flags & (OBD_BRW_FROM_GRANT | OBD_BRW_NOQUOTA))
592                         iobuf->dr_ignore_quota = 1;
593         }
594
595         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
596         cleanup_phase = 2;
597
598         down(&inode->i_sem);
599         fsfilt_check_slow(now, obd_timeout, "i_sem");
600         oti->oti_handle = fsfilt_brw_start(obd, objcount, &fso, niocount, res,
601                                            oti);
602         if (IS_ERR(oti->oti_handle)) {
603                 up(&inode->i_sem);
604                 rc = PTR_ERR(oti->oti_handle);
605                 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR,
606                        "error starting transaction: rc = %d\n", rc);
607                 oti->oti_handle = NULL;
608                 GOTO(cleanup, rc);
609         }
610         /* have to call fsfilt_commit() from this point on */
611
612         fsfilt_check_slow(now, obd_timeout, "brw_start");
613
614         i = OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME;
615
616         /* If the inode still has SUID+SGID bits set (see filter_precreate())
617          * then we will accept the UID+GID if sent by the client for
618          * initializing the ownership of this inode.  We only allow this to
619          * happen once (so clear these bits) and later only allow setattr. */
620         if (inode->i_mode & S_ISUID)
621                 i |= OBD_MD_FLUID;
622         if (inode->i_mode & S_ISGID)
623                 i |= OBD_MD_FLGID;
624
625         iattr_from_obdo(&iattr, oa, i);
626         if (iattr.ia_valid & (ATTR_UID | ATTR_GID)) {
627                 CDEBUG(D_INODE, "update UID/GID to %lu/%lu\n",
628                        (unsigned long)oa->o_uid, (unsigned long)oa->o_gid);
629                 
630                 cap_raise(current->cap_effective, CAP_SYS_RESOURCE);
631                 
632                 iattr.ia_valid |= ATTR_MODE;
633                 iattr.ia_mode = inode->i_mode;
634                 if (iattr.ia_valid & ATTR_UID)
635                         iattr.ia_mode &= ~S_ISUID;
636                 if (iattr.ia_valid & ATTR_GID)
637                         iattr.ia_mode &= ~S_ISGID;
638
639                 rc = filter_update_fidea(exp, inode, oti->oti_handle, oa);
640         }
641
642         /* filter_direct_io drops i_sem */
643         rc = filter_direct_io(OBD_BRW_WRITE, res->dentry, iobuf, exp, &iattr,
644                               oti, &wait_handle);
645         if (rc == 0)
646                 obdo_from_inode(oa, inode,
647                                 FILTER_VALID_FLAGS |OBD_MD_FLUID |OBD_MD_FLGID);
648         else
649                 obdo_from_inode(oa, inode, OBD_MD_FLUID | OBD_MD_FLGID);
650
651         lquota_getflag(quota_interface, obd, oa);
652
653         fsfilt_check_slow(now, obd_timeout, "direct_io");
654
655         err = fsfilt_commit_wait(obd, inode, wait_handle);
656         if (err)
657                 rc = err;
658
659         if (obd_sync_filter && !err)
660                 LASSERTF(oti->oti_transno <= obd->obd_last_committed,
661                          "oti_transno "LPU64" last_committed "LPU64"\n",
662                          oti->oti_transno, obd->obd_last_committed);
663
664         fsfilt_check_slow(now, obd_timeout, "commitrw commit");
665
666 cleanup:
667         filter_grant_commit(exp, niocount, res);
668
669         switch (cleanup_phase) {
670         case 2:
671                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
672                 LASSERT(current->journal_info == NULL);
673         case 1:
674                 filter_iobuf_put(&obd->u.filter, iobuf, oti);
675         case 0:
676                 /*
677                  * lnb->page automatically returns back into per-thread page
678                  * pool (bug 5137)
679                  */
680                 f_dput(res->dentry);
681         }
682
683         /* trigger quota pre-acquire */
684         qcids[USRQUOTA] = oa->o_uid;
685         qcids[GRPQUOTA] = oa->o_gid;
686         err = lquota_adjust(quota_interface, obd, qcids, NULL, rc,
687                             FSFILT_OP_CREATE); 
688         CDEBUG(err ? D_ERROR : D_QUOTA,
689                "error filter adjust qunit! (rc:%d)\n", err);
690
691         RETURN(rc);
692 }