Whamcloud - gitweb
Add support for usage PG_writeback bit in lustre.
[fs/lustre-release.git] / lustre / llite / rw26.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Lustre Lite I/O page cache routines for the 2.5/2.6 kernel version
5  *
6  *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23 #ifdef HAVE_KERNEL_CONFIG_H
24 #include <linux/config.h>
25 #endif
26 #include <linux/kernel.h>
27 #include <linux/mm.h>
28 #include <linux/string.h>
29 #include <linux/stat.h>
30 #include <linux/errno.h>
31 #include <linux/smp_lock.h>
32 #include <linux/unistd.h>
33 #include <linux/version.h>
34 #include <asm/system.h>
35 #include <asm/uaccess.h>
36
37 #include <linux/fs.h>
38 #include <linux/buffer_head.h>
39 #include <linux/mpage.h>
40 #include <linux/writeback.h>
41 #include <linux/stat.h>
42 #include <asm/uaccess.h>
43 #include <asm/segment.h>
44 #include <linux/mm.h>
45 #include <linux/pagemap.h>
46 #include <linux/smp_lock.h>
47
48 #define DEBUG_SUBSYSTEM S_LLITE
49
50 #include <lustre_lite.h>
51 #include "llite_internal.h"
52 #include <linux/lustre_compat25.h>
53
54 static int ll_writepage_26(struct page *page, struct writeback_control *wbc)
55 {
56         return ll_writepage(page);
57 }
58
59 /* It is safe to not check anything in invalidatepage/releasepage below
60    because they are run with page locked and all our io is happening with
61    locked page too */
62 #ifdef HAVE_INVALIDATEPAGE_RETURN_INT
63 static int ll_invalidatepage(struct page *page, unsigned long offset)
64 {
65         if (offset)
66                 return 0;
67         if (PagePrivate(page))
68                 ll_removepage(page);
69         return 1;
70 }
71 #else
72 static void ll_invalidatepage(struct page *page, unsigned long offset)
73 {
74         if (offset)
75                 return;
76         if (PagePrivate(page))
77                 ll_removepage(page);
78 }
79 #endif
80
81 static int ll_releasepage(struct page *page, gfp_t gfp_mask)
82 {
83         if (PagePrivate(page))
84                 ll_removepage(page);
85         return 1;
86 }
87
88 #define MAX_DIRECTIO_SIZE 2*1024*1024*1024UL
89
90 static inline int ll_get_user_pages(int rw, unsigned long user_addr,
91                                     size_t size, struct page ***pages)
92 {
93         int result = -ENOMEM;
94         int page_count;
95
96         /* set an arbitrary limit to prevent arithmetic overflow */
97         if (size > MAX_DIRECTIO_SIZE) {
98                 *pages = NULL;
99                 return -EFBIG;
100         }
101
102         page_count = ((user_addr + size + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT)-
103                       (user_addr >> CFS_PAGE_SHIFT);
104
105         OBD_ALLOC_GFP(*pages, page_count * sizeof(**pages), GFP_KERNEL);
106         if (*pages) {
107                 down_read(&current->mm->mmap_sem);
108                 result = get_user_pages(current, current->mm, user_addr,
109                                         page_count, (rw == READ), 0, *pages,
110                                         NULL);
111                 up_read(&current->mm->mmap_sem);
112                 if (result < 0)
113                         OBD_FREE(*pages, page_count * sizeof(**pages));
114         }
115
116         return result;
117 }
118
119 /*  ll_free_user_pages - tear down page struct array
120  *  @pages: array of page struct pointers underlying target buffer */
121 static void ll_free_user_pages(struct page **pages, int npages, int do_dirty)
122 {
123         int i;
124
125         for (i = 0; i < npages; i++) {
126                 if (do_dirty)
127                         set_page_dirty_lock(pages[i]);
128                 page_cache_release(pages[i]);
129         }
130
131         OBD_FREE(pages, npages * sizeof(*pages));
132 }
133
134 static ssize_t ll_direct_IO_26_seg(int rw, struct inode *inode,
135                                    struct address_space *mapping,
136                                    struct lov_stripe_md *lsm,
137                                    size_t size, loff_t file_offset,
138                                    struct page **pages, int page_count)
139 {
140         struct brw_page *pga;
141         struct obdo oa;
142         int i, rc = 0;
143         size_t length;
144         ENTRY;
145
146         OBD_ALLOC(pga, sizeof(*pga) * page_count);
147         if (!pga) {
148                 CDEBUG(D_VFSTRACE, "sizeof(*pga) = %u page_count = %u\n",
149                       (int)sizeof(*pga), page_count);
150                 RETURN(-ENOMEM);
151         }
152
153         for (i = 0, length = size; length > 0;
154              length -=pga[i].count, file_offset +=pga[i].count,i++) {/*i last!*/
155                 pga[i].pg = pages[i];
156                 pga[i].off = file_offset;
157                 /* To the end of the page, or the length, whatever is less */
158                 pga[i].count = min_t(int, CFS_PAGE_SIZE -(file_offset & ~CFS_PAGE_MASK),
159                                      length);
160                 pga[i].flag = 0;
161                 if (rw == READ)
162                         POISON_PAGE(pages[i], 0x0d);
163         }
164
165         ll_inode_fill_obdo(inode, rw, &oa);
166
167         rc = obd_brw_rqset(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
168                            ll_i2obdexp(inode), &oa, lsm, page_count, pga, NULL);
169         if (rc == 0) {
170                 rc = size;
171                 if (rw == WRITE) {
172                         lov_stripe_lock(lsm);
173                         obd_adjust_kms(ll_i2obdexp(inode), lsm, file_offset, 0);
174                         lov_stripe_unlock(lsm);
175                 }
176         }
177
178         OBD_FREE(pga, sizeof(*pga) * page_count);
179         RETURN(rc);
180 }
181
182 /* This is the maximum size of a single O_DIRECT request, based on a 128kB
183  * kmalloc limit.  We need to fit all of the brw_page structs, each one
184  * representing PAGE_SIZE worth of user data, into a single buffer, and
185  * then truncate this to be a full-sized RPC.  This is 22MB for 4kB pages. */
186 #define MAX_DIO_SIZE ((128 * 1024 / sizeof(struct brw_page) * CFS_PAGE_SIZE) & \
187                       ~(PTLRPC_MAX_BRW_SIZE - 1))
188 static ssize_t ll_direct_IO_26(int rw, struct kiocb *iocb,
189                                const struct iovec *iov, loff_t file_offset,
190                                unsigned long nr_segs)
191 {
192         struct file *file = iocb->ki_filp;
193         struct inode *inode = file->f_mapping->host;
194         ssize_t count = iov_length(iov, nr_segs), tot_bytes = 0;
195         struct ll_inode_info *lli = ll_i2info(inode);
196         unsigned long seg;
197         size_t size = MAX_DIO_SIZE;
198         ENTRY;
199
200         if (!lli->lli_smd || !lli->lli_smd->lsm_object_id)
201                 RETURN(-EBADF);
202
203         /* FIXME: io smaller than CFS_PAGE_SIZE is broken on ia64 ??? */
204         if ((file_offset & (~CFS_PAGE_MASK)) || (count & ~CFS_PAGE_MASK))
205                 RETURN(-EINVAL);
206
207         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), size="LPSZ" (max "LPSZ
208                "), offset=%lld=%llx, pages "LPSZ" (max "LPSZ")\n",
209                inode->i_ino, inode->i_generation, inode, count, MAX_DIO_SIZE,
210                file_offset, file_offset, count >> CFS_PAGE_SHIFT,
211                MAX_DIO_SIZE >> CFS_PAGE_SHIFT);
212
213         if (rw == WRITE)
214                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
215                                     LPROC_LL_DIRECT_WRITE, count);
216         else
217                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
218                                     LPROC_LL_DIRECT_READ, count);
219
220         /* Check that all user buffers are aligned as well */
221         for (seg = 0; seg < nr_segs; seg++) {
222                 if (((unsigned long)iov[seg].iov_base & ~CFS_PAGE_MASK) ||
223                     (iov[seg].iov_len & ~CFS_PAGE_MASK))
224                         RETURN(-EINVAL);
225         }
226
227         for (seg = 0; seg < nr_segs; seg++) {
228                 size_t iov_left = iov[seg].iov_len;
229                 unsigned long user_addr = (unsigned long)iov[seg].iov_base;
230
231                 while (iov_left > 0) {
232                         struct page **pages;
233                         int page_count;
234                         ssize_t result;
235
236                         page_count = ll_get_user_pages(rw, user_addr,
237                                                        min(size, iov_left),
238                                                        &pages);
239                         LASSERT(page_count != 0);
240                         if (page_count > 0) {
241                                 result = ll_direct_IO_26_seg(rw, inode,
242                                                              file->f_mapping,
243                                                              lli->lli_smd,
244                                                              min(size,iov_left),
245                                                              file_offset, pages,
246                                                              page_count);
247                                 ll_free_user_pages(pages, page_count, rw==READ);
248                         } else {
249                                 result = 0;
250                         }
251                         if (page_count < 0 || result <= 0) {
252                                 /* If we can't allocate a large enough buffer
253                                  * for the request, shrink it to a smaller
254                                  * PAGE_SIZE multiple and try again.
255                                  * We should always be able to kmalloc for a
256                                  * page worth of page pointers = 4MB on i386. */
257                                 if ((page_count == -ENOMEM||result == -ENOMEM)&&
258                                     size > (CFS_PAGE_SIZE / sizeof(*pages)) *
259                                            CFS_PAGE_SIZE) {
260                                         size = ((((size / 2) - 1) |
261                                                  ~CFS_PAGE_MASK) + 1) &
262                                                 CFS_PAGE_MASK;
263                                         CDEBUG(D_VFSTRACE, "DIO size now %u\n",
264                                                (int)size);
265                                         continue;
266                                 }
267                                 if (tot_bytes > 0)
268                                         RETURN(tot_bytes);
269                                 RETURN(page_count < 0 ? page_count : result);
270                         }
271
272                         tot_bytes += result;
273                         file_offset += result;
274                         iov_left -= result;
275                         user_addr += result;
276                 }
277         }
278         RETURN(tot_bytes);
279 }
280
281 struct address_space_operations ll_aops = {
282         .readpage       = ll_readpage,
283 //        .readpages      = ll_readpages,
284         .direct_IO      = ll_direct_IO_26,
285         .writepage      = ll_writepage_26,
286         .writepages     = generic_writepages,
287         .set_page_dirty = __set_page_dirty_nobuffers,
288         .sync_page      = NULL,
289         .prepare_write  = ll_prepare_write,
290         .commit_write   = ll_commit_write,
291         .invalidatepage = ll_invalidatepage,
292         .releasepage    = ll_releasepage,
293         .bmap           = NULL
294 };