Whamcloud - gitweb
8e07388f7e85b358d9c0c0af529b42650d7da063
[fs/lustre-release.git] / lustre / llite / llite_lib.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/llite/llite_lib.c
37  *
38  * Lustre Light Super operations
39  */
40
41 #define DEBUG_SUBSYSTEM S_LLITE
42
43 #include <linux/module.h>
44 #include <linux/types.h>
45 #include <linux/random.h>
46 #include <linux/version.h>
47
48 #include <lustre_lite.h>
49 #include <lustre_ha.h>
50 #include <lustre_dlm.h>
51 #include <lprocfs_status.h>
52 #include <lustre_disk.h>
53 #include <lustre_param.h>
54 #include <lustre_cache.h>
55 #include <obd_support.h>
56 #include "llite_internal.h"
57
58 cfs_mem_cache_t *ll_file_data_slab;
59
60 LIST_HEAD(ll_super_blocks);
61 struct rw_semaphore ll_sb_sem;
62
63 extern struct address_space_operations ll_aops;
64 extern struct address_space_operations ll_dir_aops;
65
66 #ifndef log2
67 #define log2(n) ffz(~(n))
68 #endif
69
70 static void ll_pglist_fini(struct ll_sb_info *sbi)
71 {
72         struct page *page;
73         int i;
74
75         if (sbi->ll_pglist == NULL)
76                 return;
77
78         for_each_possible_cpu(i) {
79                 page = sbi->ll_pglist[i]->llpd_page;
80                 if (page) {
81                         sbi->ll_pglist[i] = NULL;
82                         __free_page(page);
83                 }
84         }
85
86         OBD_FREE(sbi->ll_pglist, sizeof(void *)*num_possible_cpus());
87         sbi->ll_pglist = NULL;
88 }
89
90 static int ll_pglist_init(struct ll_sb_info *sbi)
91 {
92         struct ll_pglist_data *pd;
93         unsigned long budget;
94         int i, color = 0;
95         ENTRY;
96
97         OBD_ALLOC(sbi->ll_pglist, sizeof(void *) * num_possible_cpus());
98         if (sbi->ll_pglist == NULL)
99                 RETURN(-ENOMEM);
100
101         budget = sbi->ll_async_page_max / num_online_cpus();
102         for_each_possible_cpu(i) {
103                 struct page *page = alloc_pages_node(cpu_to_node(i),
104                                                     GFP_KERNEL, 0);
105                 if (page == NULL) {
106                         ll_pglist_fini(sbi);
107                         RETURN(-ENOMEM);
108                 }
109
110                 if (color + L1_CACHE_ALIGN(sizeof(*pd)) > PAGE_SIZE)
111                         color = 0;
112
113                 pd = (struct ll_pglist_data *)(page_address(page) + color);
114                 memset(pd, 0, sizeof(*pd));
115                 spin_lock_init(&pd->llpd_lock);
116                 INIT_LIST_HEAD(&pd->llpd_list);
117                 if (cpu_online(i))
118                         pd->llpd_budget = budget;
119                 pd->llpd_cpu = i;
120                 pd->llpd_page = page;
121                 atomic_set(&pd->llpd_sample_count, 0);
122                 sbi->ll_pglist[i] = pd;
123                 color += L1_CACHE_ALIGN(sizeof(*pd));
124         }
125
126         RETURN(0);
127 }
128
129 static struct ll_sb_info *ll_init_sbi(void)
130 {
131         struct ll_sb_info *sbi = NULL;
132         unsigned long pages;
133         struct sysinfo si;
134         class_uuid_t uuid;
135         int i;
136         ENTRY;
137
138         OBD_ALLOC(sbi, sizeof(*sbi));
139         if (!sbi)
140                 RETURN(NULL);
141
142         OBD_ALLOC(sbi->ll_async_page_sample, sizeof(long)*num_possible_cpus());
143         if (sbi->ll_async_page_sample == NULL)
144                 GOTO(out, 0);
145
146         spin_lock_init(&sbi->ll_lock);
147         init_mutex(&sbi->ll_lco.lco_lock);
148         spin_lock_init(&sbi->ll_pp_extent_lock);
149         spin_lock_init(&sbi->ll_process_lock);
150         sbi->ll_rw_stats_on = 0;
151
152         si_meminfo(&si);
153         pages = si.totalram - si.totalhigh;
154         if (pages >> (20 - CFS_PAGE_SHIFT) < 512) {
155 #ifdef HAVE_BGL_SUPPORT
156                 sbi->ll_async_page_max = pages / 4;
157 #else
158                 sbi->ll_async_page_max = pages / 2;
159 #endif
160         } else {
161                 sbi->ll_async_page_max = (pages / 4) * 3;
162         }
163
164         lcounter_init(&sbi->ll_async_page_count);
165         spin_lock_init(&sbi->ll_async_page_reblnc_lock);
166         sbi->ll_async_page_sample_max = 64 * num_online_cpus();
167         sbi->ll_async_page_reblnc_count = 0;
168         sbi->ll_async_page_clock_hand = 0;
169         if (ll_pglist_init(sbi))
170                 GOTO(out, 0);
171
172         sbi->ll_ra_info.ra_max_pages_per_file = min(pages / 32,
173                                            SBI_DEFAULT_READAHEAD_MAX);
174         sbi->ll_ra_info.ra_max_pages = sbi->ll_ra_info.ra_max_pages_per_file;
175         sbi->ll_ra_info.ra_max_read_ahead_whole_pages =
176                                            SBI_DEFAULT_READAHEAD_WHOLE_MAX;
177         sbi->ll_contention_time = SBI_DEFAULT_CONTENTION_SECONDS;
178         sbi->ll_lockless_truncate_enable = SBI_DEFAULT_LOCKLESS_TRUNCATE_ENABLE;
179         sbi->ll_direct_io_default = SBI_DEFAULT_DIRECT_IO_DEFAULT;
180         INIT_LIST_HEAD(&sbi->ll_conn_chain);
181         INIT_LIST_HEAD(&sbi->ll_orphan_dentry_list);
182
183         ll_generate_random_uuid(uuid);
184         class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
185         CDEBUG(D_CONFIG, "generated uuid: %s\n", sbi->ll_sb_uuid.uuid);
186
187         down_write(&ll_sb_sem);
188         list_add_tail(&sbi->ll_list, &ll_super_blocks);
189         up_write(&ll_sb_sem);
190
191 #ifdef ENABLE_CHECKSUM
192         sbi->ll_flags |= LL_SBI_DATA_CHECKSUM;
193 #endif
194 #ifdef ENABLE_LLITE_CHECKSUM
195         sbi->ll_flags |= LL_SBI_LLITE_CHECKSUM;
196 #endif
197
198 #ifdef HAVE_LRU_RESIZE_SUPPORT
199         sbi->ll_flags |= LL_SBI_LRU_RESIZE;
200 #endif
201
202         for (i = 0; i <= LL_PROCESS_HIST_MAX; i++) {
203                 spin_lock_init(&sbi->ll_rw_extents_info.pp_extents[i].pp_r_hist.oh_lock);
204                 spin_lock_init(&sbi->ll_rw_extents_info.pp_extents[i].pp_w_hist.oh_lock);
205         }
206
207         /* metadata statahead is enabled by default */
208         sbi->ll_sa_max = LL_SA_RPC_DEF;
209
210         RETURN(sbi);
211
212 out:
213         if (sbi->ll_async_page_sample)
214                 OBD_FREE(sbi->ll_async_page_sample,
215                          sizeof(long) * num_possible_cpus());
216         ll_pglist_fini(sbi);
217         OBD_FREE(sbi, sizeof(*sbi));
218         RETURN(NULL);
219 }
220
221 void ll_free_sbi(struct super_block *sb)
222 {
223         struct ll_sb_info *sbi = ll_s2sbi(sb);
224         ENTRY;
225
226         if (sbi != NULL) {
227                 down_write(&ll_sb_sem);
228                 list_del(&sbi->ll_list);
229                 up_write(&ll_sb_sem);
230                 /* dont allow find cache via sb list first */
231                 ll_pglist_fini(sbi);
232                 lcounter_destroy(&sbi->ll_async_page_count);
233                 OBD_FREE(sbi->ll_async_page_sample,
234                          sizeof(long) * num_possible_cpus());
235                 OBD_FREE(sbi, sizeof(*sbi));
236         }
237         EXIT;
238 }
239
240 static struct dentry_operations ll_d_root_ops = {
241         .d_compare = ll_dcompare,
242         .d_revalidate = ll_revalidate_nd,
243 };
244
245 static int client_common_fill_super(struct super_block *sb,
246                                     char *mdc, char *osc)
247 {
248         struct inode *root = 0;
249         struct ll_sb_info *sbi = ll_s2sbi(sb);
250         struct obd_device *obd;
251         struct ll_fid rootfid;
252         struct obd_statfs osfs;
253         struct ptlrpc_request *request = NULL;
254         struct lustre_handle osc_conn = {0, };
255         struct lustre_handle mdc_conn = {0, };
256         struct lustre_md md;
257         struct obd_connect_data *data = NULL;
258         int err, checksum;
259         ENTRY;
260
261         obd = class_name2obd(mdc);
262         if (!obd) {
263                 CERROR("MDC %s: not setup or attached\n", mdc);
264                 RETURN(-EINVAL);
265         }
266
267         OBD_ALLOC(data, sizeof(*data));
268         if (data == NULL)
269                 RETURN(-ENOMEM);
270
271         if (proc_lustre_fs_root) {
272                 err = lprocfs_register_mountpoint(proc_lustre_fs_root, sb,
273                                                   osc, mdc);
274                 if (err < 0)
275                         CERROR("could not register mount in /proc/fs/lustre\n");
276         }
277
278         /* indicate the features supported by this client */
279         data->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_IBITS      |
280                                   OBD_CONNECT_JOIN    | OBD_CONNECT_ATTRFID    |
281                                   OBD_CONNECT_NODEVOH | OBD_CONNECT_CANCELSET  |
282                                   OBD_CONNECT_AT      | OBD_CONNECT_FID |
283                                   OBD_CONNECT_VBR     | OBD_CONNECT_LOV_V3;
284 #ifdef HAVE_LRU_RESIZE_SUPPORT
285         if (sbi->ll_flags & LL_SBI_LRU_RESIZE)
286                 data->ocd_connect_flags |= OBD_CONNECT_LRU_RESIZE;
287 #endif
288 #ifdef CONFIG_FS_POSIX_ACL
289         data->ocd_connect_flags |= OBD_CONNECT_ACL;
290 #endif
291         data->ocd_ibits_known = MDS_INODELOCK_FULL;
292         data->ocd_version = LUSTRE_VERSION_CODE;
293
294         if (sb->s_flags & MS_RDONLY)
295                 data->ocd_connect_flags |= OBD_CONNECT_RDONLY;
296         if (sbi->ll_flags & LL_SBI_USER_XATTR)
297                 data->ocd_connect_flags |= OBD_CONNECT_XATTR;
298
299 #ifdef HAVE_MS_FLOCK_LOCK
300         /* force vfs to use lustre handler for flock() calls - bug 10743 */
301         sb->s_flags |= MS_FLOCK_LOCK;
302 #endif
303
304         if (sbi->ll_flags & LL_SBI_FLOCK)
305                 sbi->ll_fop = &ll_file_operations_flock;
306         else if (sbi->ll_flags & LL_SBI_LOCALFLOCK)
307                 sbi->ll_fop = &ll_file_operations;
308         else
309                 sbi->ll_fop = &ll_file_operations_noflock;
310
311
312         err = obd_connect(&mdc_conn, obd, &sbi->ll_sb_uuid, data, &sbi->ll_mdc_exp);
313         if (err == -EBUSY) {
314                 LCONSOLE_ERROR_MSG(0x14f, "An MDT (mdc %s) is performing "
315                                    "recovery, of which this client is not a "
316                                    "part. Please wait for recovery to complete,"
317                                    " abort, or time out.\n", mdc);
318                 GOTO(out, err);
319         } else if (err) {
320                 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
321                 GOTO(out, err);
322         }
323
324         err = obd_fid_init(sbi->ll_mdc_exp);
325         if (err)
326                 GOTO(out_mdc, err);
327
328         err = obd_statfs(obd, &osfs,
329                          cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS), 0);
330         if (err)
331                 GOTO(out_mdc, err);
332
333         /* MDC connect is surely finished by now because we actually sent
334          * a statfs RPC, otherwise obd_connect() is asynchronous. */
335         *data = class_exp2cliimp(sbi->ll_mdc_exp)->imp_connect_data;
336
337         LASSERT(osfs.os_bsize);
338         sb->s_blocksize = osfs.os_bsize;
339         sb->s_blocksize_bits = log2(osfs.os_bsize);
340         sb->s_magic = LL_SUPER_MAGIC;
341
342         /* for bug 11559. in $LINUX/fs/read_write.c, function do_sendfile():
343          *         retval = in_file->f_op->sendfile(...);
344          *         if (*ppos > max)
345          *                 retval = -EOVERFLOW;
346          *
347          * it will check if *ppos is greater than max. However, max equals to
348          * s_maxbytes, which is a negative integer in a x86_64 box since loff_t
349          * has been defined as a signed long long ineger in linux kernel. */
350 #if BITS_PER_LONG == 64
351         sb->s_maxbytes = PAGE_CACHE_MAXBYTES >> 1;
352 #else
353         sb->s_maxbytes = PAGE_CACHE_MAXBYTES;
354 #endif
355         sbi->ll_namelen = osfs.os_namelen;
356         sbi->ll_max_rw_chunk = LL_DEFAULT_MAX_RW_CHUNK;
357
358         if ((sbi->ll_flags & LL_SBI_USER_XATTR) &&
359             !(data->ocd_connect_flags & OBD_CONNECT_XATTR)) {
360                 LCONSOLE_INFO("Disabling user_xattr feature because "
361                               "it is not supported on the server\n");
362                 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
363         }
364
365         if (data->ocd_connect_flags & OBD_CONNECT_ACL) {
366 #ifdef MS_POSIXACL
367                 sb->s_flags |= MS_POSIXACL;
368 #endif
369                 sbi->ll_flags |= LL_SBI_ACL;
370         } else
371                 sbi->ll_flags &= ~LL_SBI_ACL;
372
373         if (data->ocd_connect_flags & OBD_CONNECT_JOIN)
374                 sbi->ll_flags |= LL_SBI_JOIN;
375
376         obd = class_name2obd(osc);
377         if (!obd) {
378                 CERROR("OSC %s: not setup or attached\n", osc);
379                 GOTO(out_mdc, err = -ENODEV);
380         }
381
382         data->ocd_connect_flags = OBD_CONNECT_VERSION   | OBD_CONNECT_GRANT    |
383                                   OBD_CONNECT_REQPORTAL | OBD_CONNECT_BRW_SIZE |
384                                   OBD_CONNECT_SRVLOCK   | OBD_CONNECT_CANCELSET|
385                                   OBD_CONNECT_AT        | OBD_CONNECT_FID      |
386                                   OBD_CONNECT_VBR       | OBD_CONNECT_TRUNCLOCK;
387
388         if (!OBD_FAIL_CHECK(OBD_FAIL_OSC_CONNECT_CKSUM)) {
389                 /* OBD_CONNECT_CKSUM should always be set, even if checksums are
390                  * disabled by default, because it can still be enabled on the
391                  * fly via /proc. As a consequence, we still need to come to an
392                  * agreement on the supported algorithms at connect time */
393                 data->ocd_connect_flags |= OBD_CONNECT_CKSUM;
394
395                 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_CKSUM_ADLER_ONLY))
396                         data->ocd_cksum_types = OBD_CKSUM_ADLER;
397                 else
398                         /* send the list of supported checksum types */
399                         data->ocd_cksum_types = OBD_CKSUM_ALL;
400         }
401
402 #ifdef HAVE_LRU_RESIZE_SUPPORT
403         if (sbi->ll_flags & LL_SBI_LRU_RESIZE)
404                 data->ocd_connect_flags |= OBD_CONNECT_LRU_RESIZE;
405 #endif
406
407         CDEBUG(D_RPCTRACE, "ocd_connect_flags: "LPX64" ocd_version: %d "
408                "ocd_grant: %d\n", data->ocd_connect_flags,
409                data->ocd_version, data->ocd_grant);
410
411         obd->obd_upcall.onu_owner = &sbi->ll_lco;
412         obd->obd_upcall.onu_upcall = ll_ocd_update;
413         data->ocd_brw_size = PTLRPC_MAX_BRW_PAGES << CFS_PAGE_SHIFT;
414
415         /* ask lov to generate OBD_NOTIFY_CREATE events for already registered
416          * targets */
417         obd_notify(obd, NULL, OBD_NOTIFY_CREATE, NULL);
418
419         obd_register_lock_cancel_cb(obd, ll_extent_lock_cancel_cb);
420         obd_register_page_removal_cb(obd, ll_page_removal_cb, ll_pin_extent_cb);
421
422         err = obd_connect(&osc_conn, obd, &sbi->ll_sb_uuid, data, &sbi->ll_osc_exp);
423         if (err == -EBUSY) {
424                 LCONSOLE_ERROR_MSG(0x150, "An OST (osc %s) is performing "
425                                    "recovery, of which this client is not a "
426                                    "part.  Please wait for recovery to "
427                                    "complete, abort, or time out.\n", osc);
428                 GOTO(out, err);
429         } else if (err) {
430                 CERROR("cannot connect to %s: rc = %d\n", osc, err);
431                 GOTO(out_cb, err);
432         }
433
434         mutex_down(&sbi->ll_lco.lco_lock);
435         sbi->ll_lco.lco_flags = data->ocd_connect_flags;
436         sbi->ll_lco.lco_mdc_exp = sbi->ll_mdc_exp;
437         sbi->ll_lco.lco_osc_exp = sbi->ll_osc_exp;
438         mutex_up(&sbi->ll_lco.lco_lock);
439
440         err = mdc_init_ea_size(sbi->ll_mdc_exp, sbi->ll_osc_exp);
441         if (err) {
442                 CERROR("cannot set max EA and cookie sizes: rc = %d\n", err);
443                 GOTO(out_osc, err);
444         }
445
446         err = obd_prep_async_page(sbi->ll_osc_exp, NULL, NULL, NULL,
447                                   0, NULL, NULL, NULL, 0, NULL);
448         if (err < 0) {
449                 LCONSOLE_ERROR_MSG(0x151, "There are no OST's in this "
450                                    "filesystem. There must be at least one "
451                                    "active OST for a client to start.\n");
452                 GOTO(out_osc, err);
453         }
454
455         if (!ll_async_page_slab) {
456                 ll_async_page_slab_size =
457                         size_round(sizeof(struct ll_async_page)) + err;
458                 ll_async_page_slab = cfs_mem_cache_create("ll_async_page",
459                                                           ll_async_page_slab_size,
460                                                           0, 0);
461                 if (!ll_async_page_slab)
462                         GOTO(out_osc, err = -ENOMEM);
463         }
464
465         err = mdc_getstatus(sbi->ll_mdc_exp, &rootfid);
466         if (err) {
467                 CERROR("cannot mds_connect: rc = %d\n", err);
468                 GOTO(out_osc, err);
469         }
470         CDEBUG(D_SUPER, "rootfid "LPU64":"DFID"\n", rootfid.id,
471                         PFID((struct lu_fid*)&rootfid));
472         sbi->ll_rootino = rootfid.id;
473
474         sb->s_op = &lustre_super_operations;
475 #if THREAD_SIZE >= 8192
476         /* Disable the NFS export because of stack overflow
477          * when THREAD_SIZE < 8192. Please refer to 17630. */
478         sb->s_export_op = &lustre_export_operations;
479 #endif
480
481         /* make root inode
482          * XXX: move this to after cbd setup? */
483         err = mdc_getattr(sbi->ll_mdc_exp, &rootfid,
484                           OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS |
485                           (sbi->ll_flags & LL_SBI_ACL ? OBD_MD_FLACL : 0),
486                           0, &request);
487         if (err) {
488                 CERROR("mdc_getattr failed for root: rc = %d\n", err);
489                 GOTO(out_osc, err);
490         }
491
492         err = mdc_req2lustre_md(request, REPLY_REC_OFF, sbi->ll_osc_exp, &md);
493         if (err) {
494                 CERROR("failed to understand root inode md: rc = %d\n",err);
495                 ptlrpc_req_finished (request);
496                 GOTO(out_osc, err);
497         }
498
499         LASSERT(sbi->ll_rootino != 0);
500         root = ll_iget(sb, ll_fid_build_ino(&rootfid), &md);
501
502         ptlrpc_req_finished(request);
503
504         if (root == NULL || is_bad_inode(root)) {
505                 mdc_free_lustre_md(sbi->ll_osc_exp, &md);
506                 CERROR("lustre_lite: bad iget4 for root\n");
507                 GOTO(out_root, err = -EBADF);
508         }
509
510         err = ll_close_thread_start(&sbi->ll_lcq);
511         if (err) {
512                 CERROR("cannot start close thread: rc %d\n", err);
513                 GOTO(out_root, err);
514         }
515
516         checksum = sbi->ll_flags & LL_SBI_DATA_CHECKSUM;
517         err = obd_set_info_async(sbi->ll_osc_exp, sizeof(KEY_CHECKSUM),
518                                  KEY_CHECKSUM, sizeof(checksum),
519                                  &checksum, NULL);
520
521         /* making vm readahead 0 for 2.4.x. In the case of 2.6.x,
522            backing dev info assigned to inode mapping is used for
523            determining maximal readahead. */
524 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)) && \
525     !defined(KERNEL_HAS_AS_MAX_READAHEAD)
526         /* bug 2805 - set VM readahead to zero */
527         vm_max_readahead = vm_min_readahead = 0;
528 #endif
529
530         sb->s_root = d_alloc_root(root);
531         if (data != NULL)
532                 OBD_FREE(data, sizeof(*data));
533         sb->s_root->d_op = &ll_d_root_ops;
534
535         sbi->ll_sdev_orig = sb->s_dev;
536         /* We set sb->s_dev equal on all lustre clients in order to support
537          * NFS export clustering.  NFSD requires that the FSID be the same
538          * on all clients. */
539         /* s_dev is also used in lt_compare() to compare two fs, but that is
540          * only a node-local comparison. */
541         sb->s_dev = get_uuid2int(sbi2mdc(sbi)->cl_target_uuid.uuid,
542                                  strlen(sbi2mdc(sbi)->cl_target_uuid.uuid));
543
544         RETURN(err);
545
546 out_root:
547         if (root)
548                 iput(root);
549 out_osc:
550         obd_disconnect(sbi->ll_osc_exp);
551         sbi->ll_osc_exp = NULL;
552 out_cb:
553         obd = class_name2obd(osc);
554         obd_unregister_lock_cancel_cb(obd, ll_extent_lock_cancel_cb);
555         obd_unregister_page_removal_cb(obd, ll_page_removal_cb);
556 out_mdc:
557         obd_fid_fini(sbi->ll_mdc_exp);
558         obd_disconnect(sbi->ll_mdc_exp);
559         sbi->ll_mdc_exp = NULL;
560 out:
561         if (data != NULL)
562                 OBD_FREE(data, sizeof(*data));
563         lprocfs_unregister_mountpoint(sbi);
564         RETURN(err);
565 }
566
567 int ll_get_max_mdsize(struct ll_sb_info *sbi, int *lmmsize)
568 {
569         int size, rc;
570
571         *lmmsize = obd_size_diskmd(sbi->ll_osc_exp, NULL);
572         size = sizeof(int);
573         rc = obd_get_info(sbi->ll_mdc_exp, sizeof(KEY_MAX_EASIZE),
574                           KEY_MAX_EASIZE, &size, lmmsize, NULL);
575         if (rc)
576                 CERROR("Get max mdsize error rc %d \n", rc);
577
578         RETURN(rc);
579 }
580
581 void ll_dump_inode(struct inode *inode)
582 {
583         struct list_head *tmp;
584         int dentry_count = 0;
585
586         LASSERT(inode != NULL);
587
588         list_for_each(tmp, &inode->i_dentry)
589                 dentry_count++;
590
591         CERROR("inode %p dump: dev=%s ino=%lu mode=%o count=%u, %d dentries\n",
592                inode, ll_i2mdcexp(inode)->exp_obd->obd_name, inode->i_ino,
593                inode->i_mode, atomic_read(&inode->i_count), dentry_count);
594 }
595
596 void lustre_dump_dentry(struct dentry *dentry, int recur)
597 {
598         struct list_head *tmp;
599         int subdirs = 0;
600
601         LASSERT(dentry != NULL);
602
603         list_for_each(tmp, &dentry->d_subdirs)
604                 subdirs++;
605
606         CERROR("dentry %p dump: name=%.*s parent=%.*s (%p), inode=%p, count=%u,"
607                " flags=0x%x, fsdata=%p, %d subdirs\n", dentry,
608                dentry->d_name.len, dentry->d_name.name,
609                dentry->d_parent->d_name.len, dentry->d_parent->d_name.name,
610                dentry->d_parent, dentry->d_inode, atomic_read(&dentry->d_count),
611                dentry->d_flags, dentry->d_fsdata, subdirs);
612         if (dentry->d_inode != NULL)
613                 ll_dump_inode(dentry->d_inode);
614
615         if (recur == 0)
616                 return;
617
618         list_for_each(tmp, &dentry->d_subdirs) {
619                 struct dentry *d = list_entry(tmp, struct dentry, d_child);
620                 lustre_dump_dentry(d, recur - 1);
621         }
622 }
623
624 void client_common_put_super(struct super_block *sb)
625 {
626         struct ll_sb_info *sbi = ll_s2sbi(sb);
627         ENTRY;
628
629         ll_close_thread_shutdown(sbi->ll_lcq);
630
631         lprocfs_unregister_mountpoint(sbi);
632
633         list_del(&sbi->ll_conn_chain);
634
635         /* callbacks is cleared after disconnect each target */
636         obd_disconnect(sbi->ll_osc_exp);
637         sbi->ll_osc_exp = NULL;
638
639         obd_fid_fini(sbi->ll_mdc_exp);
640         obd_disconnect(sbi->ll_mdc_exp);
641         sbi->ll_mdc_exp = NULL;
642
643         EXIT;
644 }
645
646 void ll_kill_super(struct super_block *sb)
647 {
648         struct ll_sb_info *sbi;
649
650         ENTRY;
651
652         /* not init sb ?*/
653         if (!(sb->s_flags & MS_ACTIVE))
654                 return;
655
656         sbi = ll_s2sbi(sb);
657         /* we need restore s_dev from changed for clustred NFS before put_super
658          * because new kernels have cached s_dev and change sb->s_dev in
659          * put_super not affected real removing devices */
660         if (sbi)
661                 sb->s_dev = sbi->ll_sdev_orig;
662         EXIT;
663 }
664
665 char *ll_read_opt(const char *opt, char *data)
666 {
667         char *value;
668         char *retval;
669         ENTRY;
670
671         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
672         if (strncmp(opt, data, strlen(opt)))
673                 RETURN(NULL);
674         if ((value = strchr(data, '=')) == NULL)
675                 RETURN(NULL);
676
677         value++;
678         OBD_ALLOC(retval, strlen(value) + 1);
679         if (!retval) {
680                 CERROR("out of memory!\n");
681                 RETURN(NULL);
682         }
683
684         memcpy(retval, value, strlen(value)+1);
685         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
686         RETURN(retval);
687 }
688
689 static inline int ll_set_opt(const char *opt, char *data, int fl)
690 {
691         if (strncmp(opt, data, strlen(opt)) != 0)
692                 return(0);
693         else
694                 return(fl);
695 }
696
697 /* non-client-specific mount options are parsed in lmd_parse */
698 static int ll_options(char *options, int *flags)
699 {
700         int tmp;
701         char *s1 = options, *s2;
702         ENTRY;
703
704         if (!options)
705                 RETURN(0);
706
707         CDEBUG(D_CONFIG, "Parsing opts %s\n", options);
708
709         while (*s1) {
710                 CDEBUG(D_SUPER, "next opt=%s\n", s1);
711                 tmp = ll_set_opt("nolock", s1, LL_SBI_NOLCK);
712                 if (tmp) {
713                         *flags |= tmp;
714                         goto next;
715                 }
716                 tmp = ll_set_opt("flock", s1, LL_SBI_FLOCK);
717                 if (tmp) {
718                         *flags |= tmp;
719                         goto next;
720                 }
721                 tmp = ll_set_opt("localflock", s1, LL_SBI_LOCALFLOCK);
722                 if (tmp) {
723                         *flags |= tmp;
724                         goto next;
725                 }
726                 tmp = ll_set_opt("noflock", s1, LL_SBI_FLOCK|LL_SBI_LOCALFLOCK);
727                 if (tmp) {
728                         *flags &= ~tmp;
729                         goto next;
730                 }
731                 tmp = ll_set_opt("user_xattr", s1, LL_SBI_USER_XATTR);
732                 if (tmp) {
733                         *flags |= tmp;
734                         goto next;
735                 }
736                 tmp = ll_set_opt("nouser_xattr", s1, LL_SBI_USER_XATTR);
737                 if (tmp) {
738                         *flags &= ~tmp;
739                         goto next;
740                 }
741                 tmp = ll_set_opt("acl", s1, LL_SBI_ACL);
742                 if (tmp) {
743                         /* Ignore deprecated mount option.  The client will
744                          * always try to mount with ACL support, whether this
745                          * is used depends on whether server supports it. */
746                         LCONSOLE_ERROR_MSG(0x152, "Ignoring deprecated "
747                                                   "mount option 'acl'.\n");
748                         goto next;
749                 }
750                 tmp = ll_set_opt("noacl", s1, LL_SBI_ACL);
751                 if (tmp) {
752                         LCONSOLE_ERROR_MSG(0x152, "Ignoring deprecated "
753                                                   "mount option 'noacl'.\n");
754                         goto next;
755                 }
756
757                 tmp = ll_set_opt("checksum", s1, LL_SBI_DATA_CHECKSUM);
758                 if (tmp) {
759                         *flags |= tmp;
760                         goto next;
761                 }
762                 tmp = ll_set_opt("nochecksum", s1, LL_SBI_DATA_CHECKSUM);
763                 if (tmp) {
764                         *flags &= ~tmp;
765                         goto next;
766                 }
767
768                 tmp = ll_set_opt("lruresize", s1, LL_SBI_LRU_RESIZE);
769                 if (tmp) {
770                         *flags |= tmp;
771                         goto next;
772                 }
773                 tmp = ll_set_opt("nolruresize", s1, LL_SBI_LRU_RESIZE);
774                 if (tmp) {
775                         *flags &= ~tmp;
776                         goto next;
777                 }
778                 tmp = ll_set_opt("lazystatfs", s1, LL_SBI_LAZYSTATFS);
779                 if (tmp) {
780                         *flags |= tmp;
781                         goto next;
782                 }
783                 tmp = ll_set_opt("nolazystatfs", s1, LL_SBI_LAZYSTATFS);
784                 if (tmp) {
785                         *flags &= ~tmp;
786                         goto next;
787                 }
788                 LCONSOLE_ERROR_MSG(0x152, "Unknown option '%s', won't mount.\n",
789                                    s1);
790                 RETURN(-EINVAL);
791
792 next:
793                 /* Find next opt */
794                 s2 = strchr(s1, ',');
795                 if (s2 == NULL)
796                         break;
797                 s1 = s2 + 1;
798         }
799         RETURN(0);
800 }
801
802 void ll_lli_init(struct ll_inode_info *lli)
803 {
804         lli->lli_inode_magic = LLI_INODE_MAGIC;
805         sema_init(&lli->lli_size_sem, 1);
806         sema_init(&lli->lli_write_sem, 1);
807         lli->lli_flags = 0;
808         lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
809         spin_lock_init(&lli->lli_lock);
810         sema_init(&lli->lli_och_sem, 1);
811         lli->lli_mds_read_och = lli->lli_mds_write_och = NULL;
812         lli->lli_mds_exec_och = NULL;
813         lli->lli_open_fd_read_count = lli->lli_open_fd_write_count = 0;
814         lli->lli_open_fd_exec_count = 0;
815         INIT_LIST_HEAD(&lli->lli_dead_list);
816 #ifdef HAVE_CLOSE_THREAD
817         INIT_LIST_HEAD(&lli->lli_pending_write_llaps);
818 #endif
819         init_rwsem(&lli->lli_truncate_rwsem);
820 }
821
822 /* COMPAT_146 */
823 #define MDCDEV "mdc_dev"
824 static int old_lustre_process_log(struct super_block *sb, char *newprofile,
825                                   struct config_llog_instance *cfg)
826 {
827         struct lustre_sb_info *lsi = s2lsi(sb);
828         struct obd_device *obd;
829         struct lustre_handle mdc_conn = {0, };
830         struct obd_export *exp;
831         char *ptr, *mdt, *profile;
832         char niduuid[10] = "mdtnid0";
833         class_uuid_t uuid;
834         struct obd_uuid mdc_uuid;
835         struct llog_ctxt *ctxt;
836         struct obd_connect_data ocd = { 0 };
837         lnet_nid_t nid;
838         int i, rc = 0, recov_bk = 1, failnodes = 0;
839         ENTRY;
840
841         ll_generate_random_uuid(uuid);
842         class_uuid_unparse(uuid, &mdc_uuid);
843         CDEBUG(D_HA, "generated uuid: %s\n", mdc_uuid.uuid);
844
845         /* Figure out the old mdt and profile name from new-style profile
846            ("lustre" from "mds/lustre-client") */
847         mdt = newprofile;
848         profile = strchr(mdt, '/');
849         if (profile == NULL) {
850                 CDEBUG(D_CONFIG, "Can't find MDT name in %s\n", newprofile);
851                 GOTO(out, rc = -EINVAL);
852         }
853         *profile = '\0';
854         profile++;
855         ptr = strrchr(profile, '-');
856         if (ptr == NULL) {
857                 CDEBUG(D_CONFIG, "Can't find client name in %s\n", newprofile);
858                 GOTO(out, rc = -EINVAL);
859         }
860         *ptr = '\0';
861
862         LCONSOLE_WARN("This looks like an old mount command; I will try to "
863                       "contact MDT '%s' for profile '%s'\n", mdt, profile);
864
865         /* Use nids from mount line: uml1,1@elan:uml2,2@elan:/lustre */
866         i = 0;
867         ptr = lsi->lsi_lmd->lmd_dev;
868         while (class_parse_nid(ptr, &nid, &ptr) == 0) {
869                 rc = do_lcfg(MDCDEV, nid, LCFG_ADD_UUID, niduuid, 0,0,0);
870                 i++;
871                 /* Stop at the first failover nid */
872                 if (*ptr == ':')
873                         break;
874         }
875         if (i == 0) {
876                 CERROR("No valid MDT nids found.\n");
877                 GOTO(out, rc = -EINVAL);
878         }
879         failnodes++;
880
881         rc = do_lcfg(MDCDEV, 0, LCFG_ATTACH, LUSTRE_MDC_NAME,mdc_uuid.uuid,0,0);
882         if (rc < 0)
883                 GOTO(out_del_uuid, rc);
884
885         rc = do_lcfg(MDCDEV, 0, LCFG_SETUP, mdt, niduuid, 0, 0);
886         if (rc < 0) {
887                 LCONSOLE_ERROR_MSG(0x153, "I couldn't establish a connection "
888                                    "with the MDT. Check that the MDT host NID "
889                                    "is correct and the networks are up.\n");
890                 GOTO(out_detach, rc);
891         }
892
893         obd = class_name2obd(MDCDEV);
894         if (obd == NULL)
895                 GOTO(out_cleanup, rc = -EINVAL);
896
897         /* Add any failover nids */
898         while (*ptr == ':') {
899                 /* New failover node */
900                 sprintf(niduuid, "mdtnid%d", failnodes);
901                 i = 0;
902                 while (class_parse_nid(ptr, &nid, &ptr) == 0) {
903                         i++;
904                         rc = do_lcfg(MDCDEV, nid, LCFG_ADD_UUID, niduuid,0,0,0);
905                         if (rc)
906                                 CERROR("Add uuid for %s failed %d\n",
907                                        libcfs_nid2str(nid), rc);
908                         if (*ptr == ':')
909                                 break;
910                 }
911                 if (i > 0) {
912                         rc = do_lcfg(MDCDEV, 0, LCFG_ADD_CONN, niduuid, 0, 0,0);
913                         if (rc)
914                                 CERROR("Add conn for %s failed %d\n",
915                                        libcfs_nid2str(nid), rc);
916                         failnodes++;
917                 } else {
918                         /* at ":/fsname" */
919                         break;
920                 }
921         }
922
923         /* Try all connections, but only once. */
924         rc = obd_set_info_async(obd->obd_self_export,
925                                 sizeof(KEY_INIT_RECOV_BACKUP), KEY_INIT_RECOV_BACKUP,
926                                 sizeof(recov_bk), &recov_bk, NULL);
927         if (rc)
928                 GOTO(out_cleanup, rc);
929
930         /* If we don't have this then an ACL MDS will refuse the connection */
931         ocd.ocd_connect_flags = OBD_CONNECT_ACL;
932
933         rc = obd_connect(&mdc_conn, obd, &mdc_uuid, &ocd, &exp);
934         if (rc) {
935                 CERROR("cannot connect to %s: rc = %d\n", mdt, rc);
936                 GOTO(out_cleanup, rc);
937         }
938
939         ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
940
941         cfg->cfg_flags |= CFG_F_COMPAT146;
942
943 #if 1
944         rc = class_config_parse_llog(ctxt, profile, cfg);
945 #else
946         /*
947          * For debugging, it's useful to just dump the log
948          */
949         rc = class_config_dump_llog(ctxt, profile, cfg);
950 #endif
951         llog_ctxt_put(ctxt);
952         switch (rc) {
953         case 0: {
954                 /* Set the caller's profile name to the old-style */
955                 memcpy(newprofile, profile, strlen(profile) + 1);
956                 break;
957         }
958         case -EINVAL:
959                 LCONSOLE_ERROR_MSG(0x154, "%s: The configuration '%s' could not"
960                                    " be read from the MDT '%s'.  Make sure this"
961                                    " client and the MDT are running compatible "
962                                    "versions of Lustre.\n",
963                                    obd->obd_name, profile, mdt);
964                 /* fall through */
965         default:
966                 LCONSOLE_ERROR_MSG(0x155, "%s: The configuration '%s' could not"
967                                    " be read from the MDT '%s'.  This may be "
968                                    "the result of communication errors between "
969                                    "the client and the MDT, or if the MDT is "
970                                    "not running.\n", obd->obd_name, profile,
971                                    mdt);
972                 break;
973         }
974
975         /* We don't so much care about errors in cleaning up the config llog
976          * connection, as we have already read the config by this point. */
977         obd_disconnect(exp);
978
979 out_cleanup:
980         do_lcfg(MDCDEV, 0, LCFG_CLEANUP, 0, 0, 0, 0);
981
982 out_detach:
983         do_lcfg(MDCDEV, 0, LCFG_DETACH, 0, 0, 0, 0);
984
985 out_del_uuid:
986         /* class_add_uuid adds a nid even if the same uuid exists; we might
987            delete any copy here.  So they all better match. */
988         for (i = 0; i < failnodes; i++) {
989                 sprintf(niduuid, "mdtnid%d", i);
990                 do_lcfg(MDCDEV, 0, LCFG_DEL_UUID, niduuid, 0, 0, 0);
991         }
992         /* class_import_put will get rid of the additional connections */
993 out:
994         RETURN(rc);
995 }
996 /* end COMPAT_146 */
997
998 static struct backing_dev_info ll_backing_dev_info = {
999         .ra_pages       = 0,    /* No readahead */
1000 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12))
1001         .capabilities   = 0,    /* Does contribute to dirty memory */
1002 #else
1003         .memory_backed  = 0,    /* Does contribute to dirty memory */
1004 #endif
1005 #ifdef HAVE_NEW_BACKING_DEV_INFO
1006         .wb_cnt = 0,
1007 #endif
1008 };
1009
1010 static atomic_t ll_bdi_num = ATOMIC_INIT(0);
1011
1012 int ll_fill_super(struct super_block *sb)
1013 {
1014         struct lustre_profile *lprof = NULL;
1015         struct lustre_sb_info *lsi = s2lsi(sb);
1016         struct ll_sb_info *sbi;
1017         char  *osc = NULL, *mdc = NULL;
1018         char  *profilenm = get_profile_name(sb);
1019         struct config_llog_instance cfg = {0, };
1020         char   ll_instance[sizeof(sb) * 2 + 1];
1021         int    err;
1022         char  *save = NULL;
1023         char  pseudo[32] = { 0 };
1024         ENTRY;
1025
1026         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
1027
1028         cfs_module_get();
1029
1030         /* client additional sb info */
1031         lsi->lsi_llsbi = sbi = ll_init_sbi();
1032         if (!sbi) {
1033                 cfs_module_put();
1034                 RETURN(-ENOMEM);
1035         }
1036
1037         err = ll_options(lsi->lsi_lmd->lmd_opts, &sbi->ll_flags);
1038         if (err)
1039                 GOTO(out_free, err);
1040
1041         /* Generate a string unique to this super, in case some joker tries
1042            to mount the same fs at two mount points.
1043            Use the address of the super itself.*/
1044         sprintf(ll_instance, "%p", sb);
1045         cfg.cfg_instance = ll_instance;
1046         cfg.cfg_uuid = lsi->lsi_llsbi->ll_sb_uuid;
1047         cfg.cfg_sb = sb;
1048
1049         /* set up client obds */
1050         if (strchr(profilenm, '/') != NULL) /* COMPAT_146 */
1051                 err = -EINVAL; /* skip error messages, use old config code */
1052         else
1053                 err = lustre_process_log(sb, profilenm, &cfg);
1054         /* COMPAT_146 */
1055         if (err < 0) {
1056                 char *oldname;
1057                 int rc, oldnamelen;
1058                 oldnamelen = strlen(profilenm) + 1;
1059                 /* Temp storage for 1.4.6 profile name */
1060                 OBD_ALLOC(oldname, oldnamelen);
1061                 if (oldname) {
1062                         memcpy(oldname, profilenm, oldnamelen);
1063                         rc = old_lustre_process_log(sb, oldname, &cfg);
1064                         if (rc >= 0) {
1065                                 /* That worked - update the profile name
1066                                    permanently */
1067                                 err = rc;
1068                                 OBD_FREE(lsi->lsi_lmd->lmd_profile,
1069                                          strlen(lsi->lsi_lmd->lmd_profile) + 1);
1070                                 OBD_ALLOC(lsi->lsi_lmd->lmd_profile,
1071                                          strlen(oldname) + 1);
1072                                 if (!lsi->lsi_lmd->lmd_profile) {
1073                                         OBD_FREE(oldname, oldnamelen);
1074                                         GOTO(out_free, err = -ENOMEM);
1075                                 }
1076                                 memcpy(lsi->lsi_lmd->lmd_profile, oldname,
1077                                        strlen(oldname) + 1);
1078                                 profilenm = get_profile_name(sb);
1079                                 /* Don't ever try to recover the MGS */
1080                                 rc = ptlrpc_set_import_active(
1081                                         lsi->lsi_mgc->u.cli.cl_import, 0);
1082                         }
1083                         OBD_FREE(oldname, oldnamelen);
1084                 }
1085         }
1086         /* end COMPAT_146 */
1087         if (err < 0) {
1088                 CERROR("Unable to process log: %d\n", err);
1089                 GOTO(out_free, err);
1090         }
1091
1092         lprof = class_get_profile(profilenm);
1093         if (lprof == NULL) {
1094                 LCONSOLE_ERROR_MSG(0x156, "The client profile '%s' could not be"
1095                                    " read from the MGS.  Does that filesystem "
1096                                    "exist?\n", profilenm);
1097                 GOTO(out_free, err = -EINVAL);
1098         }
1099
1100         /*
1101          * The configuration for 1.8 client and 2.0 client are different.
1102          * 2.0 introduces lmv, but 1.8 directly uses mdc.
1103          * Here, we will hack to use proper name for mdc if needed.
1104          */
1105         {
1106                 char *fsname_end;
1107                 int namelen;
1108
1109                 save = lprof->lp_mdc;
1110                 fsname_end = strrchr(save, '-');
1111                 if (fsname_end) {
1112                         namelen = fsname_end - save;
1113                         if (strcmp(fsname_end, "-clilmv") == 0) {
1114                                 strncpy(pseudo, save, namelen);
1115                                 strcat(pseudo, "-MDT0000-mdc");
1116                                 lprof->lp_mdc = pseudo;
1117                                 CDEBUG(D_INFO, "1.8.x connecting to 2.0: lmv=%s"
1118                                        " new mdc=%s\n", save, pseudo);
1119                         }
1120                 }
1121         }
1122
1123         CDEBUG(D_CONFIG, "Found profile %s: mdc=%s osc=%s\n", profilenm,
1124                lprof->lp_mdc, lprof->lp_osc);
1125
1126         OBD_ALLOC(osc, strlen(lprof->lp_osc) +
1127                   strlen(ll_instance) + 2);
1128         if (!osc)
1129                 GOTO(out_free, err = -ENOMEM);
1130         sprintf(osc, "%s-%s", lprof->lp_osc, ll_instance);
1131
1132         OBD_ALLOC(mdc, strlen(lprof->lp_mdc) +
1133                   strlen(ll_instance) + 2);
1134         if (!mdc)
1135                 GOTO(out_free, err = -ENOMEM);
1136         sprintf(mdc, "%s-%s", lprof->lp_mdc, ll_instance);
1137
1138         /* connections, registrations, sb setup */
1139         err = client_common_fill_super(sb, mdc, osc);
1140         if (err)
1141                 GOTO(out_free, err);
1142
1143 #ifdef HAVE_NEW_BACKING_DEV_INFO
1144         lsi->bdi = ll_backing_dev_info;
1145         err = bdi_init(&lsi->bdi);
1146         if (err)
1147                 GOTO(out_free, err);
1148
1149         err = bdi_register(&lsi->bdi, NULL, "lustre-%d",
1150                                 atomic_inc_return(&ll_bdi_num));
1151         if (err) {
1152                 bdi_destroy(&lsi->bdi);
1153                 GOTO(out_free, err);
1154         }
1155 #endif
1156
1157 out_free:
1158         if (save && lprof)
1159                 lprof->lp_mdc = save;
1160         if (mdc)
1161                 OBD_FREE(mdc, strlen(mdc) + 1);
1162         if (osc)
1163                 OBD_FREE(osc, strlen(osc) + 1);
1164         if (err)
1165                 ll_put_super(sb);
1166         else
1167                 LCONSOLE_WARN("Client %s has started\n", profilenm);
1168
1169         RETURN(err);
1170 } /* ll_fill_super */
1171
1172
1173 void ll_put_super(struct super_block *sb)
1174 {
1175         struct config_llog_instance cfg;
1176         char   ll_instance[sizeof(sb) * 2 + 1];
1177         struct obd_device *obd;
1178         struct lustre_sb_info *lsi = s2lsi(sb);
1179         struct ll_sb_info *sbi = ll_s2sbi(sb);
1180         char *profilenm = get_profile_name(sb);
1181         int force = 1, next;
1182         ENTRY;
1183
1184         CDEBUG(D_VFSTRACE, "VFS Op: sb %p - %s\n", sb, profilenm);
1185
1186         sprintf(ll_instance, "%p", sb);
1187         cfg.cfg_instance = ll_instance;
1188         lustre_end_log(sb, NULL, &cfg);
1189
1190         if (sbi->ll_mdc_exp) {
1191                 obd = class_exp2obd(sbi->ll_mdc_exp);
1192                 if (obd)
1193                         force = obd->obd_force;
1194         }
1195
1196         /* We need to set force before the lov_disconnect in
1197            lustre_common_put_super, since l_d cleans up osc's as well. */
1198         if (force) {
1199                 next = 0;
1200                 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid,
1201                                                      &next)) != NULL) {
1202                         obd->obd_force = force;
1203                 }
1204         }
1205
1206         if (sbi->ll_lcq) {
1207                 /* Only if client_common_fill_super succeeded */
1208                 client_common_put_super(sb);
1209         }
1210
1211         next = 0;
1212         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) !=NULL) {
1213                 class_manual_cleanup(obd);
1214         }
1215
1216         if (profilenm)
1217                 class_del_profile(profilenm);
1218
1219 #ifdef HAVE_NEW_BACKING_DEV_INFO
1220         if (lsi->bdi.wb_cnt > 0)
1221                 bdi_destroy(&lsi->bdi);
1222 #endif
1223
1224         ll_free_sbi(sb);
1225         lsi->lsi_llsbi = NULL;
1226
1227         lustre_common_put_super(sb);
1228
1229         LCONSOLE_WARN("client %s umount complete\n", ll_instance);
1230
1231         cfs_module_put();
1232
1233         EXIT;
1234 } /* client_put_super */
1235
1236 int ll_shrink_cache(int nr_to_scan, SHRINKER_MASK_T gfp_mask)
1237 {
1238         struct ll_sb_info *sbi;
1239         int count = 0;
1240
1241         if ((nr_to_scan != 0) && !(gfp_mask & __GFP_FS))
1242                 return -1;
1243
1244         /* don't race with umount */
1245         down_read(&ll_sb_sem);
1246         list_for_each_entry(sbi, &ll_super_blocks, ll_list)
1247                 count += llap_shrink_cache(sbi, nr_to_scan);
1248         up_read(&ll_sb_sem);
1249
1250         return count;
1251 }
1252
1253 struct inode *ll_inode_from_lock(struct ldlm_lock *lock)
1254 {
1255         struct inode *inode = NULL;
1256         /* NOTE: we depend on atomic igrab() -bzzz */
1257         lock_res_and_lock(lock);
1258         if (lock->l_ast_data) {
1259                 struct ll_inode_info *lli = ll_i2info(lock->l_ast_data);
1260                 if (lli->lli_inode_magic == LLI_INODE_MAGIC) {
1261                         inode = igrab(lock->l_ast_data);
1262                 } else {
1263                         inode = lock->l_ast_data;
1264                         LDLM_DEBUG_LIMIT(inode->i_state & I_FREEING ?  D_INFO :
1265                                          D_WARNING, lock, "l_ast_data %p is "
1266                                          "bogus: magic %08x", lock->l_ast_data,
1267                                          lli->lli_inode_magic);
1268                         inode = NULL;
1269                 }
1270         }
1271         unlock_res_and_lock(lock);
1272         return inode;
1273 }
1274
1275 static int null_if_equal(struct ldlm_lock *lock, void *data)
1276 {
1277         if (data == lock->l_ast_data) {
1278                 lock->l_ast_data = NULL;
1279
1280                 if (lock->l_req_mode != lock->l_granted_mode)
1281                         LDLM_ERROR(lock,"clearing inode with ungranted lock");
1282         }
1283
1284         return LDLM_ITER_CONTINUE;
1285 }
1286
1287 void ll_clear_inode(struct inode *inode)
1288 {
1289         struct ll_fid fid;
1290         struct ll_inode_info *lli = ll_i2info(inode);
1291         struct ll_sb_info *sbi = ll_i2sbi(inode);
1292         ENTRY;
1293
1294         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1295                inode->i_generation, inode);
1296
1297         if (S_ISDIR(inode->i_mode)) {
1298                 /* these should have been cleared in ll_file_release */
1299                 LASSERT(lli->lli_sai == NULL);
1300                 LASSERT(lli->lli_opendir_key == NULL);
1301                 LASSERT(lli->lli_opendir_pid == 0);
1302         }
1303
1304         ll_inode2fid(&fid, inode);
1305         clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
1306         mdc_change_cbdata(sbi->ll_mdc_exp, &fid, null_if_equal, inode);
1307
1308         LASSERT(!lli->lli_open_fd_write_count);
1309         LASSERT(!lli->lli_open_fd_read_count);
1310         LASSERT(!lli->lli_open_fd_exec_count);
1311
1312         if (lli->lli_mds_write_och)
1313                 ll_mdc_real_close(inode, FMODE_WRITE);
1314         if (lli->lli_mds_exec_och) {
1315                 if (!FMODE_EXEC)
1316                         CERROR("No FMODE exec, bug exec och is present for "
1317                                "inode %ld\n", inode->i_ino);
1318                 ll_mdc_real_close(inode, FMODE_EXEC);
1319         }
1320         if (lli->lli_mds_read_och)
1321                 ll_mdc_real_close(inode, FMODE_READ);
1322
1323
1324         if (lli->lli_smd) {
1325                 obd_change_cbdata(sbi->ll_osc_exp, lli->lli_smd,
1326                                   null_if_equal, inode);
1327
1328                 obd_free_memmd(sbi->ll_osc_exp, &lli->lli_smd);
1329                 lli->lli_smd = NULL;
1330         }
1331
1332         if (lli->lli_symlink_name) {
1333                 OBD_FREE(lli->lli_symlink_name,
1334                          strlen(lli->lli_symlink_name) + 1);
1335                 lli->lli_symlink_name = NULL;
1336         }
1337
1338 #ifdef CONFIG_FS_POSIX_ACL
1339         if (lli->lli_posix_acl) {
1340                 LASSERT(atomic_read(&lli->lli_posix_acl->a_refcount) == 1);
1341                 posix_acl_release(lli->lli_posix_acl);
1342                 lli->lli_posix_acl = NULL;
1343         }
1344 #endif
1345
1346         lli->lli_inode_magic = LLI_INODE_DEAD;
1347
1348         EXIT;
1349 }
1350 static int ll_setattr_do_truncate(struct inode *inode, loff_t new_size)
1351 {
1352         struct ll_sb_info *sbi = ll_i2sbi(inode);
1353         struct ll_inode_info *lli = ll_i2info(inode);
1354         struct lov_stripe_md *lsm = lli->lli_smd;
1355         int rc;
1356         ldlm_policy_data_t policy = { .l_extent = {new_size,
1357                                                    OBD_OBJECT_EOF } };
1358         struct lustre_handle lockh = { 0 };
1359         int local_lock = 1; /* 0 - no local lock;
1360                              * 1 - lock taken by lock_extent;
1361                              * 2 - by obd_match*/
1362         int ast_flags;
1363         ENTRY;
1364
1365         UNLOCK_INODE_MUTEX(inode);
1366         UP_WRITE_I_ALLOC_SEM(inode);
1367
1368         down_write(&lli->lli_truncate_rwsem);
1369         if (sbi->ll_lockless_truncate_enable &&
1370             (sbi->ll_lco.lco_flags & OBD_CONNECT_TRUNCLOCK)) {
1371                 int n_matches = 0;
1372
1373                 ast_flags = LDLM_FL_BLOCK_GRANTED;
1374                 rc = obd_match(sbi->ll_osc_exp, lsm, LDLM_EXTENT,
1375                                &policy, LCK_PW, &ast_flags, inode, &lockh,
1376                                &n_matches);
1377                 if (rc > 0) {
1378                         local_lock = 2;
1379                         rc = 0;
1380                 } else {
1381                         /* clear the lock handle as it not cleared
1382                          * by obd_match if no matched lock found */
1383                         lockh.cookie = 0;
1384                         if (rc == 0 && n_matches == 0) {
1385                                 local_lock = 0;
1386                                 rc = ll_file_punch(inode, new_size, 1);
1387                         }
1388                 }
1389         }
1390         if (local_lock == 1) {
1391                 /* XXX when we fix the AST intents to pass the discard-range
1392                  * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
1393                  * XXX here. */
1394                 ast_flags = (new_size == 0) ? LDLM_AST_DISCARD_DATA : 0;
1395                 rc = ll_extent_lock(NULL, inode, lsm, LCK_PW, &policy,
1396                                     &lockh, ast_flags);
1397                 if (unlikely(rc != 0))
1398                         local_lock = 0;
1399         }
1400
1401         LOCK_INODE_MUTEX(inode);
1402         DOWN_WRITE_I_ALLOC_SEM(inode);
1403         if (likely(rc == 0)) {
1404                 /* Only ll_inode_size_lock is taken at this level.
1405                  * lov_stripe_lock() is grabbed by ll_truncate() only over
1406                  * call to obd_adjust_kms().  If vmtruncate returns 0, then
1407                  * ll_truncate dropped ll_inode_size_lock() */
1408                 ll_inode_size_lock(inode, 0);
1409                 if (!local_lock)
1410                         set_bit(LLI_F_SRVLOCK, &lli->lli_flags);
1411                 rc = vmtruncate(inode, new_size);
1412                 clear_bit(LLI_F_SRVLOCK, &lli->lli_flags);
1413                 if (rc != 0) {
1414                         LASSERT(SEM_COUNT(&lli->lli_size_sem) <= 0);
1415                         ll_inode_size_unlock(inode, 0);
1416                 }
1417         }
1418         if (local_lock) {
1419                 int err;
1420
1421                 err = (local_lock == 2) ?
1422                         obd_cancel(sbi->ll_osc_exp, lsm, LCK_PW, &lockh, 0, 0):
1423                         ll_extent_unlock(NULL, inode, lsm, LCK_PW, &lockh);
1424                 if (unlikely(err != 0)){
1425                         CERROR("extent unlock failed: err=%d,"
1426                                " unlock method =%d\n", err, local_lock);
1427                         if (rc == 0)
1428                                 rc = err;
1429                 }
1430         }
1431         up_write(&lli->lli_truncate_rwsem);
1432         RETURN(rc);
1433 }
1434
1435 /* If this inode has objects allocated to it (lsm != NULL), then the OST
1436  * object(s) determine the file size and mtime.  Otherwise, the MDS will
1437  * keep these values until such a time that objects are allocated for it.
1438  * We do the MDS operations first, as it is checking permissions for us.
1439  * We don't to the MDS RPC if there is nothing that we want to store there,
1440  * otherwise there is no harm in updating mtime/atime on the MDS if we are
1441  * going to do an RPC anyways.
1442  *
1443  * If we are doing a truncate, we will send the mtime and ctime updates
1444  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
1445  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
1446  * at the same time.
1447  */
1448 int ll_setattr_raw(struct inode *inode, struct iattr *attr)
1449 {
1450         struct ll_inode_info *lli = ll_i2info(inode);
1451         struct lov_stripe_md *lsm = lli->lli_smd;
1452         struct ll_sb_info *sbi = ll_i2sbi(inode);
1453         struct ptlrpc_request *request = NULL;
1454         struct mdc_op_data *op_data;
1455         struct lustre_md md;
1456         int ia_valid = attr->ia_valid;
1457         int rc = 0;
1458         ENTRY;
1459
1460         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu valid %x\n", inode->i_ino,
1461                attr->ia_valid);
1462         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETATTR, 1);
1463
1464         if (ia_valid & ATTR_SIZE) {
1465                 if (attr->ia_size > ll_file_maxbytes(inode)) {
1466                         CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
1467                                attr->ia_size, ll_file_maxbytes(inode));
1468                         RETURN(-EFBIG);
1469                 }
1470
1471                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1472         }
1473
1474         /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
1475         if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
1476                 if (cfs_curproc_fsuid() != inode->i_uid &&
1477                     !cfs_capable(CFS_CAP_FOWNER))
1478                         RETURN(-EPERM);
1479         }
1480
1481         /* We mark all of the fields "set" so MDS/OST does not re-set them */
1482         if (attr->ia_valid & ATTR_CTIME) {
1483                 attr->ia_ctime = CURRENT_TIME;
1484                 attr->ia_valid |= ATTR_CTIME_SET;
1485         }
1486         if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
1487                 attr->ia_atime = CURRENT_TIME;
1488                 attr->ia_valid |= ATTR_ATIME_SET;
1489         }
1490         if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
1491                 attr->ia_mtime = CURRENT_TIME;
1492                 attr->ia_valid |= ATTR_MTIME_SET;
1493         }
1494
1495         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
1496                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
1497                        LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
1498                        CURRENT_SECONDS);
1499
1500         /* NB: ATTR_SIZE will only be set after this point if the size
1501          * resides on the MDS, ie, this file has no objects. */
1502         if (lsm)
1503                 attr->ia_valid &= ~ATTR_SIZE;
1504
1505         OBD_ALLOC_PTR(op_data);
1506         if (NULL == op_data)
1507                 RETURN(-ENOMEM);
1508         /* We always do an MDS RPC, even if we're only changing the size;
1509          * only the MDS knows whether truncate() should fail with -ETXTBUSY */
1510         ll_prepare_mdc_op_data(op_data, inode, NULL, NULL, 0, 0, NULL);
1511
1512         rc = mdc_setattr(sbi->ll_mdc_exp, op_data,
1513                          attr, NULL, 0, NULL, 0, &request);
1514
1515         if (rc) {
1516                 ptlrpc_req_finished(request);
1517                 if (rc == -ENOENT) {
1518                         inode->i_nlink = 0;
1519                         /* Unlinked special device node?  Or just a race?
1520                          * Pretend we done everything. */
1521                         if (!S_ISREG(inode->i_mode) &&
1522                             !S_ISDIR(inode->i_mode))
1523                                 rc = inode_setattr(inode, attr);
1524                 } else if (rc != -EPERM && rc != -EACCES && rc != -ETXTBSY)
1525                         CERROR("mdc_setattr fails: rc = %d\n", rc);
1526                 OBD_FREE_PTR(op_data);
1527                 RETURN(rc);
1528         }
1529         OBD_FREE_PTR(op_data);
1530
1531         rc = mdc_req2lustre_md(request, REPLY_REC_OFF, sbi->ll_osc_exp, &md);
1532         if (rc) {
1533                 ptlrpc_req_finished(request);
1534                 RETURN(rc);
1535         }
1536
1537         /* We call inode_setattr to adjust timestamps.
1538          * If there is at least some data in file, we cleared ATTR_SIZE above to
1539          * avoid invoking vmtruncate, otherwise it is important to call
1540          * vmtruncate in inode_setattr to update inode->i_size (bug 6196) */
1541         rc = inode_setattr(inode, attr);
1542
1543         ll_update_inode(inode, &md);
1544         ptlrpc_req_finished(request);
1545
1546         if (!lsm || !S_ISREG(inode->i_mode)) {
1547                 CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
1548                 RETURN(rc);
1549         }
1550
1551         /* We really need to get our PW lock before we change inode->i_size.
1552          * If we don't we can race with other i_size updaters on our node, like
1553          * ll_file_read.  We can also race with i_size propogation to other
1554          * nodes through dirtying and writeback of final cached pages.  This
1555          * last one is especially bad for racing o_append users on other
1556          * nodes. */
1557         if (ia_valid & ATTR_SIZE) {
1558                 rc = ll_setattr_do_truncate(inode, attr->ia_size);
1559         } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
1560                 struct obd_info *oinfo;
1561                 struct obdo *oa;
1562                 struct lustre_handle lockh = { 0 };
1563                 obd_valid valid;
1564
1565                 OBD_ALLOC_PTR(oinfo);
1566                 if (NULL == oinfo)
1567                         RETURN(-ENOMEM);
1568
1569                 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
1570                        inode->i_ino, LTIME_S(attr->ia_mtime));
1571
1572                 OBDO_ALLOC(oa);
1573                 if (oa) {
1574                         oa->o_id = lsm->lsm_object_id;
1575                         oa->o_gr = lsm->lsm_object_gr;
1576                         oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1577
1578                         valid = OBD_MD_FLTYPE | OBD_MD_FLFID | OBD_MD_FLGENER;
1579
1580                         if (LTIME_S(attr->ia_mtime) < LTIME_S(attr->ia_ctime)){
1581                                 struct ost_lvb xtimes;
1582
1583                                 UNLOCK_INODE_MUTEX(inode);
1584                                 /* setting mtime to past is performed under PW
1585                                  * EOF extent lock */
1586                                 oinfo->oi_policy.l_extent.start = 0;
1587                                 oinfo->oi_policy.l_extent.end = OBD_OBJECT_EOF;
1588                                 rc = ll_extent_lock(NULL, inode, lsm, LCK_PW,
1589                                                     &oinfo->oi_policy,
1590                                                     &lockh, 0);
1591                                 LOCK_INODE_MUTEX(inode);
1592                                 if (rc) {
1593                                         OBD_FREE_PTR(oinfo);
1594                                         RETURN(rc);
1595                                 }
1596
1597                                 /* setattr under locks
1598                                  *
1599                                  * 1. restore inode's timestamps which
1600                                  * are about to be set as long as
1601                                  * concurrent stat (via
1602                                  * ll_glimpse_size) might bring
1603                                  * out-of-date ones
1604                                  *
1605                                  * 2. update lsm so that next stat
1606                                  * (via ll_glimpse_size) could get
1607                                  * correct values in lsm */
1608                                 lov_stripe_lock(lli->lli_smd);
1609                                 if (ia_valid & ATTR_ATIME) {
1610                                         LTIME_S(inode->i_atime) =
1611                                                 xtimes.lvb_atime =
1612                                                 LTIME_S(attr->ia_atime);
1613                                         valid |= OBD_MD_FLATIME;
1614                                 }
1615                                 if (ia_valid & ATTR_MTIME) {
1616                                         LTIME_S(inode->i_mtime) =
1617                                                 xtimes.lvb_mtime =
1618                                                 LTIME_S(attr->ia_mtime);
1619                                         valid |= OBD_MD_FLMTIME;
1620                                 }
1621                                 if (ia_valid & ATTR_CTIME) {
1622                                         LTIME_S(inode->i_ctime) =
1623                                                 xtimes.lvb_ctime =
1624                                                 LTIME_S(attr->ia_ctime);
1625                                         valid |= OBD_MD_FLCTIME;
1626                                 }
1627
1628                                 obd_update_lvb(ll_i2obdexp(inode), lli->lli_smd,
1629                                                &xtimes, valid);
1630                                 lov_stripe_unlock(lli->lli_smd);
1631                         } else {
1632                                 /* lockless setattr
1633                                  *
1634                                  * 1. do not use inode's timestamps
1635                                  * because concurrent stat might fill
1636                                  * the inode with out-of-date times,
1637                                  * send values from attr instead
1638                                  *
1639                                  * 2.do no update lsm, as long as stat
1640                                  * (via ll_glimpse_size) will bring
1641                                  * attributes from osts anyway */
1642                                 if (ia_valid & ATTR_ATIME) {
1643                                         oa->o_atime = LTIME_S(attr->ia_atime);
1644                                         oa->o_valid |= OBD_MD_FLATIME;
1645                                 }
1646                                 if (ia_valid & ATTR_MTIME) {
1647                                         oa->o_mtime = LTIME_S(attr->ia_mtime);
1648                                         oa->o_valid |= OBD_MD_FLMTIME;
1649                                 }
1650                                 if (ia_valid & ATTR_CTIME) {
1651                                         oa->o_ctime = LTIME_S(attr->ia_ctime);
1652                                         oa->o_valid |= OBD_MD_FLCTIME;
1653                                 }
1654                         }
1655
1656                         obdo_from_inode(oa, inode, valid);
1657
1658                         oinfo->oi_oa = oa;
1659                         oinfo->oi_md = lsm;
1660
1661                         rc = obd_setattr_rqset(sbi->ll_osc_exp, oinfo, NULL);
1662                         if (rc)
1663                                 CERROR("obd_setattr_async fails: rc=%d\n", rc);
1664
1665                         if (LTIME_S(attr->ia_mtime) < LTIME_S(attr->ia_ctime)){
1666                                 int err;
1667
1668                                 err = ll_extent_unlock(NULL, inode, lsm,
1669                                                        LCK_PW, &lockh);
1670                                 if (unlikely(err != 0)) {
1671                                         CERROR("extent unlock failed: "
1672                                                "err=%d\n", err);
1673                                         if (rc == 0)
1674                                                 rc = err;
1675                                 }
1676                         }
1677                         OBDO_FREE(oa);
1678                 } else {
1679                         rc = -ENOMEM;
1680                 }
1681                 OBD_FREE_PTR(oinfo);
1682         }
1683         RETURN(rc);
1684 }
1685
1686 int ll_setattr(struct dentry *de, struct iattr *attr)
1687 {
1688         int mode = de->d_inode->i_mode;
1689
1690         if ((attr->ia_valid & (ATTR_CTIME|ATTR_SIZE|ATTR_MODE)) ==
1691             (ATTR_CTIME|ATTR_SIZE|ATTR_MODE))
1692                 attr->ia_valid |= MDS_OPEN_OWNEROVERRIDE;
1693         if ((attr->ia_valid & (ATTR_MODE|ATTR_FORCE|ATTR_SIZE)) ==
1694             (ATTR_SIZE|ATTR_MODE)) {
1695                 if (((mode & S_ISUID) && (!(attr->ia_mode & S_ISUID))) ||
1696                     ((mode & S_ISGID) && (mode & S_IXGRP) &&
1697                     (!(attr->ia_mode & S_ISGID))))
1698                         attr->ia_valid |= ATTR_FORCE;
1699         }
1700
1701         if ((mode & S_ISUID) && S_ISREG(mode) &&
1702             !(attr->ia_mode & S_ISUID))
1703                 attr->ia_valid |= ATTR_KILL_SUID;
1704
1705         if (((mode & (S_ISGID|S_IXGRP)) == (S_ISGID|S_IXGRP)) &&
1706             S_ISREG(mode) && !(attr->ia_mode & S_ISGID))
1707                 attr->ia_valid |= ATTR_KILL_SGID;
1708
1709         return ll_setattr_raw(de->d_inode, attr);
1710 }
1711
1712 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
1713                        __u64 max_age, __u32 flags)
1714 {
1715         struct ll_sb_info *sbi = ll_s2sbi(sb);
1716         struct obd_statfs obd_osfs;
1717         int rc;
1718         ENTRY;
1719
1720         rc = obd_statfs(class_exp2obd(sbi->ll_mdc_exp), osfs, max_age, flags);
1721         if (rc) {
1722                 CERROR("mdc_statfs fails: rc = %d\n", rc);
1723                 RETURN(rc);
1724         }
1725
1726         osfs->os_type = sb->s_magic;
1727
1728         CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1729                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1730
1731         if (sbi->ll_flags & LL_SBI_LAZYSTATFS)
1732                 flags |= OBD_STATFS_NODELAY;
1733
1734         rc = obd_statfs_rqset(class_exp2obd(sbi->ll_osc_exp),
1735                               &obd_osfs, max_age, flags);
1736         if (rc) {
1737                 CERROR("obd_statfs fails: rc = %d\n", rc);
1738                 RETURN(rc);
1739         }
1740
1741         CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1742                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1743                obd_osfs.os_files);
1744
1745         osfs->os_bsize = obd_osfs.os_bsize;
1746         osfs->os_blocks = obd_osfs.os_blocks;
1747         osfs->os_bfree = obd_osfs.os_bfree;
1748         osfs->os_bavail = obd_osfs.os_bavail;
1749
1750         /* If we don't have as many objects free on the OST as inodes
1751          * on the MDS, we reduce the total number of inodes to
1752          * compensate, so that the "inodes in use" number is correct.
1753          */
1754         if (obd_osfs.os_ffree < osfs->os_ffree) {
1755                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1756                         obd_osfs.os_ffree;
1757                 osfs->os_ffree = obd_osfs.os_ffree;
1758         }
1759
1760         RETURN(rc);
1761 }
1762 #ifndef HAVE_STATFS_DENTRY_PARAM
1763 int ll_statfs(struct super_block *sb, struct kstatfs *sfs)
1764 {
1765 #else
1766 int ll_statfs(struct dentry *de, struct kstatfs *sfs)
1767 {
1768         struct super_block *sb = de->d_sb;
1769 #endif
1770         struct obd_statfs osfs;
1771         int rc;
1772
1773         CDEBUG(D_VFSTRACE, "VFS Op: at "LPU64" jiffies\n", get_jiffies_64());
1774         ll_stats_ops_tally(ll_s2sbi(sb), LPROC_LL_STAFS, 1);
1775
1776         /* Some amount of caching on the client is allowed */
1777         rc = ll_statfs_internal(sb, &osfs,
1778                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1779                                 0);
1780         if (rc)
1781                 return rc;
1782
1783         statfs_unpack(sfs, &osfs);
1784
1785         /* We need to downshift for all 32-bit kernels, because we can't
1786          * tell if the kernel is being called via sys_statfs64() or not.
1787          * Stop before overflowing f_bsize - in which case it is better
1788          * to just risk EOVERFLOW if caller is using old sys_statfs(). */
1789         if (sizeof(long) < 8) {
1790                 while (osfs.os_blocks > ~0UL && sfs->f_bsize < 0x40000000) {
1791                         sfs->f_bsize <<= 1;
1792
1793                         osfs.os_blocks >>= 1;
1794                         osfs.os_bfree >>= 1;
1795                         osfs.os_bavail >>= 1;
1796                 }
1797         }
1798
1799         sfs->f_blocks = osfs.os_blocks;
1800         sfs->f_bfree = osfs.os_bfree;
1801         sfs->f_bavail = osfs.os_bavail;
1802
1803         return 0;
1804 }
1805
1806 void ll_inode_size_lock(struct inode *inode, int lock_lsm)
1807 {
1808         struct ll_inode_info *lli;
1809         struct lov_stripe_md *lsm;
1810
1811         lli = ll_i2info(inode);
1812         LASSERT(lli->lli_size_sem_owner != current);
1813         down(&lli->lli_size_sem);
1814         LASSERT(lli->lli_size_sem_owner == NULL);
1815         lli->lli_size_sem_owner = current;
1816         lsm = lli->lli_smd;
1817         LASSERTF(lsm != NULL || lock_lsm == 0, "lsm %p, lock_lsm %d\n",
1818                  lsm, lock_lsm);
1819         if (lock_lsm)
1820                 lov_stripe_lock(lsm);
1821 }
1822
1823 void ll_inode_size_unlock(struct inode *inode, int unlock_lsm)
1824 {
1825         struct ll_inode_info *lli;
1826         struct lov_stripe_md *lsm;
1827
1828         lli = ll_i2info(inode);
1829         lsm = lli->lli_smd;
1830         LASSERTF(lsm != NULL || unlock_lsm == 0, "lsm %p, lock_lsm %d\n",
1831                  lsm, unlock_lsm);
1832         if (unlock_lsm)
1833                 lov_stripe_unlock(lsm);
1834         LASSERT(lli->lli_size_sem_owner == current);
1835         lli->lli_size_sem_owner = NULL;
1836         up(&lli->lli_size_sem);
1837 }
1838
1839 static void ll_replace_lsm(struct inode *inode, struct lov_stripe_md *lsm)
1840 {
1841         struct ll_inode_info *lli = ll_i2info(inode);
1842
1843         dump_lsm(D_INODE, lsm);
1844         dump_lsm(D_INODE, lli->lli_smd);
1845         LASSERTF(lsm->lsm_magic == LOV_MAGIC_JOIN,
1846                  "lsm must be joined lsm %p\n", lsm);
1847         obd_free_memmd(ll_i2obdexp(inode), &lli->lli_smd);
1848         CDEBUG(D_INODE, "replace lsm %p to lli_smd %p for inode %lu%u(%p)\n",
1849                lsm, lli->lli_smd, inode->i_ino, inode->i_generation, inode);
1850         lli->lli_smd = lsm;
1851         lli->lli_maxbytes = lsm->lsm_maxbytes;
1852         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1853                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1854 }
1855
1856 void ll_update_inode(struct inode *inode, struct lustre_md *md)
1857 {
1858         struct ll_inode_info *lli = ll_i2info(inode);
1859         struct mds_body *body = md->body;
1860         struct lov_stripe_md *lsm = md->lsm;
1861         struct ll_sb_info *sbi = ll_i2sbi(inode);
1862         ENTRY;
1863
1864         CDEBUG(D_INODE, "body->valid = "LPX64"\n", body->valid);
1865
1866         LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
1867         if (lsm != NULL) {
1868                 spin_lock(&lli->lli_lock);
1869                 if (lli->lli_smd == NULL) {
1870                         if (lsm->lsm_magic != LOV_MAGIC_V1 &&
1871                             lsm->lsm_magic != LOV_MAGIC_V3 &&
1872                             lsm->lsm_magic != LOV_MAGIC_JOIN) {
1873                                 dump_lsm(D_ERROR, lsm);
1874                                 LBUG();
1875                         }
1876                         CDEBUG(D_INODE, "adding lsm %p to inode %lu/%u(%p)\n",
1877                                lsm, inode->i_ino, inode->i_generation, inode);
1878                         /* ll_inode_size_lock() requires it is only called
1879                          * with lli_smd != NULL or lock_lsm == 0 or we can
1880                          * race between lock/unlock.  bug 9547 */
1881                         lli->lli_smd = lsm;
1882                         spin_unlock(&lli->lli_lock);
1883                         lli->lli_maxbytes = lsm->lsm_maxbytes;
1884                         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1885                                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1886                 } else {
1887                         spin_unlock(&lli->lli_lock);
1888                         if ((lli->lli_smd->lsm_magic == lsm->lsm_magic ||
1889                              (lli->lli_smd->lsm_magic == LOV_MAGIC_V3 &&
1890                               lsm->lsm_magic == LOV_MAGIC_V1) ||
1891                              (lli->lli_smd->lsm_magic == LOV_MAGIC_V1 &&
1892                               lsm->lsm_magic == LOV_MAGIC_V3)) &&
1893                             lli->lli_smd->lsm_stripe_count ==
1894                                         lsm->lsm_stripe_count) {
1895                                 /* The MDS can suddenly change the magic to
1896                                  * v1/v3 if it has been upgraded/downgraded to a
1897                                  * version that does/doesn't support OST pools.
1898                                  * In this case, we consider that the cached lsm
1899                                  * is equivalent to the new one and keep it in
1900                                  * place until the inode is evicted from the
1901                                  * cache */
1902                                 if (lov_stripe_md_cmp(lli->lli_smd, lsm)) {
1903                                         CERROR("lsm mismatch for inode %ld\n",
1904                                                 inode->i_ino);
1905                                         CERROR("lli_smd:\n");
1906                                         dump_lsm(D_ERROR, lli->lli_smd);
1907                                         CERROR("lsm:\n");
1908                                         dump_lsm(D_ERROR, lsm);
1909                                         LBUG();
1910                                 }
1911                         } else
1912                                 ll_replace_lsm(inode, lsm);
1913                 }
1914                 if (lli->lli_smd != lsm)
1915                         obd_free_memmd(ll_i2obdexp(inode), &lsm);
1916         }
1917
1918 #ifdef CONFIG_FS_POSIX_ACL
1919         LASSERT(!md->posix_acl || (body->valid & OBD_MD_FLACL));
1920         if (body->valid & OBD_MD_FLACL) {
1921                 spin_lock(&lli->lli_lock);
1922                 if (lli->lli_posix_acl)
1923                         posix_acl_release(lli->lli_posix_acl);
1924                 lli->lli_posix_acl = md->posix_acl;
1925                 spin_unlock(&lli->lli_lock);
1926         }
1927 #endif
1928
1929         inode->i_ino = ll_fid_build_ino(&body->fid1);
1930         inode->i_generation = ll_fid_build_gen(sbi, &body->fid1);
1931         *ll_inode_lu_fid(inode) = *((struct lu_fid*)&md->body->fid1);
1932
1933         if (body->valid & OBD_MD_FLATIME) {
1934                 if (body->atime > LTIME_S(inode->i_atime))
1935                         LTIME_S(inode->i_atime) = body->atime;
1936                 lli->lli_lvb.lvb_atime = body->atime;
1937         }
1938         if (body->valid & OBD_MD_FLMTIME) {
1939                 if (body->mtime > LTIME_S(inode->i_mtime)) {
1940                         CDEBUG(D_INODE, "setting ino %lu mtime from %lu "
1941                                "to "LPU64"\n", inode->i_ino,
1942                                LTIME_S(inode->i_mtime), body->mtime);
1943                         LTIME_S(inode->i_mtime) = body->mtime;
1944                 }
1945                 lli->lli_lvb.lvb_mtime = body->mtime;
1946         }
1947         if (body->valid & OBD_MD_FLCTIME) {
1948                 if (body->ctime > LTIME_S(inode->i_ctime))
1949                         LTIME_S(inode->i_ctime) = body->ctime;
1950                 lli->lli_lvb.lvb_ctime = body->ctime;
1951         }
1952         if (body->valid & OBD_MD_FLMODE)
1953                 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
1954         if (body->valid & OBD_MD_FLTYPE)
1955                 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
1956         if (S_ISREG(inode->i_mode)) {
1957                 inode->i_blkbits = min(PTLRPC_MAX_BRW_BITS+1, LL_MAX_BLKSIZE_BITS);
1958         } else {
1959                 inode->i_blkbits = inode->i_sb->s_blocksize_bits;
1960         }
1961 #ifdef HAVE_INODE_BLKSIZE
1962         inode->i_blksize = 1<<inode->i_blkbits;
1963 #endif
1964         if (body->valid & OBD_MD_FLUID)
1965                 inode->i_uid = body->uid;
1966         if (body->valid & OBD_MD_FLGID)
1967                 inode->i_gid = body->gid;
1968         if (body->valid & OBD_MD_FLFLAGS)
1969                 inode->i_flags = ll_ext_to_inode_flags(body->flags |
1970                                                        MDS_BFLAG_EXT_FLAGS);
1971         if (body->valid & OBD_MD_FLNLINK)
1972                 inode->i_nlink = body->nlink;
1973
1974         if (body->valid & OBD_MD_FLRDEV)
1975                 inode->i_rdev = old_decode_dev(body->rdev);
1976         if (body->valid & OBD_MD_FLSIZE) {
1977 #if 0           /* Can't block ll_test_inode->ll_update_inode, b=14326*/
1978                 ll_inode_size_lock(inode, 0);
1979                 i_size_write(inode, body->size);
1980                 ll_inode_size_unlock(inode, 0);
1981 #else
1982                 inode->i_size = body->size;
1983 #endif
1984         }
1985         if (body->valid & OBD_MD_FLBLOCKS)
1986                 inode->i_blocks = body->blocks;
1987
1988         if (body->valid & OBD_MD_FLSIZE)
1989                 set_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
1990         EXIT;
1991 }
1992
1993 void ll_read_inode2(struct inode *inode, void *opaque)
1994 {
1995         struct lustre_md *md = opaque;
1996         struct ll_inode_info *lli = ll_i2info(inode);
1997         ENTRY;
1998
1999         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
2000                inode->i_generation, inode);
2001
2002         ll_lli_init(lli);
2003
2004         LASSERT(!lli->lli_smd);
2005
2006         /* Core attributes from the MDS first.  This is a new inode, and
2007          * the VFS doesn't zero times in the core inode so we have to do
2008          * it ourselves.  They will be overwritten by either MDS or OST
2009          * attributes - we just need to make sure they aren't newer. */
2010         LTIME_S(inode->i_mtime) = 0;
2011         LTIME_S(inode->i_atime) = 0;
2012         LTIME_S(inode->i_ctime) = 0;
2013         inode->i_rdev = 0;
2014         ll_update_inode(inode, md);
2015
2016         /* OIDEBUG(inode); */
2017
2018         if (S_ISREG(inode->i_mode)) {
2019                 struct ll_sb_info *sbi = ll_i2sbi(inode);
2020                 inode->i_op = &ll_file_inode_operations;
2021                 inode->i_fop = sbi->ll_fop;
2022                 inode->i_mapping->a_ops = &ll_aops;
2023                 EXIT;
2024         } else if (S_ISDIR(inode->i_mode)) {
2025                 inode->i_op = &ll_dir_inode_operations;
2026                 inode->i_fop = &ll_dir_operations;
2027                 inode->i_mapping->a_ops = &ll_dir_aops;
2028                 EXIT;
2029         } else if (S_ISLNK(inode->i_mode)) {
2030                 inode->i_op = &ll_fast_symlink_inode_operations;
2031                 EXIT;
2032         } else {
2033                 inode->i_op = &ll_special_inode_operations;
2034                 init_special_inode(inode, inode->i_mode,
2035                                    kdev_t_to_nr(inode->i_rdev));
2036                 /* initializing backing dev info. */
2037 #ifdef HAVE_NEW_BACKING_DEV_INFO
2038                 inode->i_mapping->backing_dev_info = &(s2lsi(inode->i_sb)->bdi);
2039 #else
2040                 inode->i_mapping->backing_dev_info = &ll_backing_dev_info;
2041 #endif
2042                 EXIT;
2043         }
2044 }
2045
2046 int ll_iocontrol(struct inode *inode, struct file *file,
2047                  unsigned int cmd, unsigned long arg)
2048 {
2049         struct ll_sb_info *sbi = ll_i2sbi(inode);
2050         struct ptlrpc_request *req = NULL;
2051         int rc, flags = 0;
2052         ENTRY;
2053
2054         switch(cmd) {
2055         case FSFILT_IOC_GETFLAGS: {
2056                 struct ll_fid fid;
2057                 struct mds_body *body;
2058
2059                 ll_inode2fid(&fid, inode);
2060                 rc = mdc_getattr(sbi->ll_mdc_exp, &fid, OBD_MD_FLFLAGS,0,&req);
2061                 if (rc) {
2062                         CERROR("failure %d inode %lu\n", rc, inode->i_ino);
2063                         RETURN(-abs(rc));
2064                 }
2065
2066                 body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
2067                                       sizeof(*body));
2068
2069                 /* We want to return EXT3_*_FL flags to the caller via this
2070                  * ioctl.  An older MDS may be sending S_* flags, fix it up. */
2071                 flags = ll_inode_to_ext_flags(body->flags,
2072                                               MDS_BFLAG_EXT_FLAGS);
2073                 ptlrpc_req_finished (req);
2074
2075                 RETURN(put_user(flags, (int *)arg));
2076         }
2077         case FSFILT_IOC_SETFLAGS: {
2078                 struct mdc_op_data op_data = { { 0 } };
2079                 struct ll_iattr_struct attr;
2080                 struct obd_info oinfo = { { { 0 } } };
2081                 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
2082
2083                 if (get_user(flags, (int *)arg))
2084                         RETURN(-EFAULT);
2085
2086                 oinfo.oi_md = lsm;
2087                 OBDO_ALLOC(oinfo.oi_oa);
2088                 if (!oinfo.oi_oa)
2089                         RETURN(-ENOMEM);
2090
2091                 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0, NULL);
2092
2093                 memset(&attr, 0, sizeof(attr));
2094                 attr.ia_attr_flags = flags;
2095                 ((struct iattr *)&attr)->ia_valid |= ATTR_ATTR_FLAG;
2096
2097                 rc = mdc_setattr(sbi->ll_mdc_exp, &op_data,
2098                                  (struct iattr *)&attr, NULL, 0, NULL, 0, &req);
2099                 ptlrpc_req_finished(req);
2100                 if (rc) {
2101                         OBDO_FREE(oinfo.oi_oa);
2102                         RETURN(rc);
2103                 }
2104
2105                 if (lsm == NULL) {
2106                         OBDO_FREE(oinfo.oi_oa);
2107                         GOTO(update_cache, rc);
2108                 }
2109
2110                 oinfo.oi_oa->o_id = lsm->lsm_object_id;
2111                 oinfo.oi_oa->o_gr = lsm->lsm_object_gr;
2112                 oinfo.oi_oa->o_flags = flags;
2113                 oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP |
2114                                        OBD_MD_FLFLAGS;
2115
2116                 obdo_from_inode(oinfo.oi_oa, inode,
2117                                 OBD_MD_FLFID | OBD_MD_FLGENER);
2118                 rc = obd_setattr_rqset(sbi->ll_osc_exp, &oinfo, NULL);
2119                 OBDO_FREE(oinfo.oi_oa);
2120                 if (rc) {
2121                         if (rc != -EPERM && rc != -EACCES)
2122                                 CERROR("osc_setattr_async fails: rc = %d\n",rc);
2123                         RETURN(rc);
2124                 }
2125
2126                 EXIT;
2127 update_cache:
2128                 inode->i_flags = ll_ext_to_inode_flags(flags |
2129                                                        MDS_BFLAG_EXT_FLAGS);
2130                 return 0;
2131         }
2132         default:
2133                 RETURN(-ENOSYS);
2134         }
2135
2136         RETURN(0);
2137 }
2138
2139 /* umount -f client means force down, don't save state */
2140 #ifdef HAVE_UMOUNTBEGIN_VFSMOUNT
2141 void ll_umount_begin(struct vfsmount *vfsmnt, int flags)
2142 {
2143         struct super_block *sb = vfsmnt->mnt_sb;
2144 #else
2145 void ll_umount_begin(struct super_block *sb)
2146 {
2147 #endif
2148         struct lustre_sb_info *lsi = s2lsi(sb);
2149         struct ll_sb_info *sbi = ll_s2sbi(sb);
2150         struct obd_device *obd;
2151         struct obd_ioctl_data ioc_data = { 0 };
2152         ENTRY;
2153
2154 #ifdef HAVE_UMOUNTBEGIN_VFSMOUNT
2155         if (!(flags & MNT_FORCE)) {
2156                 EXIT;
2157                 return;
2158         }
2159 #endif
2160
2161         /* Tell the MGC we got umount -f */
2162         lsi->lsi_flags |= LSI_UMOUNT_FORCE;
2163
2164         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p count %d active %d\n", sb,
2165                sb->s_count, atomic_read(&sb->s_active));
2166
2167         obd = class_exp2obd(sbi->ll_mdc_exp);
2168         if (obd == NULL) {
2169                 CERROR("Invalid MDC connection handle "LPX64"\n",
2170                        sbi->ll_mdc_exp->exp_handle.h_cookie);
2171                 EXIT;
2172                 return;
2173         }
2174         obd->obd_force = 1;
2175         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_mdc_exp, sizeof ioc_data,
2176                       &ioc_data, NULL);
2177
2178         obd = class_exp2obd(sbi->ll_osc_exp);
2179         if (obd == NULL) {
2180                 CERROR("Invalid LOV connection handle "LPX64"\n",
2181                        sbi->ll_osc_exp->exp_handle.h_cookie);
2182                 EXIT;
2183                 return;
2184         }
2185
2186         obd->obd_force = 1;
2187         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_osc_exp, sizeof ioc_data,
2188                       &ioc_data, NULL);
2189
2190         /* Really, we'd like to wait until there are no requests outstanding,
2191          * and then continue.  For now, we just invalidate the requests,
2192          * schedule() and sleep one second if needed, and hope.
2193          */
2194         schedule();
2195 #ifdef HAVE_UMOUNTBEGIN_VFSMOUNT
2196         if (atomic_read(&vfsmnt->mnt_count) > 2) {
2197                 cfs_schedule_timeout(CFS_TASK_INTERRUPTIBLE,
2198                                      cfs_time_seconds(1));
2199                 if (atomic_read(&vfsmnt->mnt_count) > 2)
2200                         LCONSOLE_WARN("Mount still busy with %d refs! You "
2201                                       "may try to umount it a bit later\n",
2202                                       atomic_read(&vfsmnt->mnt_count));
2203         }
2204 #endif
2205
2206         EXIT;
2207 }
2208
2209 int ll_remount_fs(struct super_block *sb, int *flags, char *data)
2210 {
2211         struct ll_sb_info *sbi = ll_s2sbi(sb);
2212         int err;
2213         __u32 read_only;
2214
2215         if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
2216                 read_only = *flags & MS_RDONLY;
2217                 err = obd_set_info_async(sbi->ll_mdc_exp, sizeof(KEY_READONLY),
2218                                          KEY_READONLY, sizeof(read_only),
2219                                          &read_only, NULL);
2220
2221                 /* MDS might have expected a different ro key value, b=17493 */
2222                 if (err == -EINVAL) {
2223                         CDEBUG(D_CONFIG, "Retrying remount with 1.6.6 ro key\n");
2224                         err = obd_set_info_async(sbi->ll_mdc_exp,
2225                                                  sizeof(KEY_READONLY_166COMPAT),
2226                                                  KEY_READONLY_166COMPAT,
2227                                                  sizeof(read_only),
2228                                                  &read_only, NULL);
2229                 }
2230
2231                 if (err) {
2232                         CERROR("Failed to change the read-only flag during "
2233                                "remount: %d\n", err);
2234                         return err;
2235                 }
2236
2237                 if (read_only)
2238                         sb->s_flags |= MS_RDONLY;
2239                 else
2240                         sb->s_flags &= ~MS_RDONLY;
2241         }
2242         return 0;
2243 }
2244
2245 int ll_prep_inode(struct obd_export *exp, struct inode **inode,
2246                   struct ptlrpc_request *req, int offset,struct super_block *sb)
2247 {
2248         struct lustre_md md;
2249         struct ll_sb_info *sbi = NULL;
2250         int rc = 0;
2251         ENTRY;
2252
2253         LASSERT(*inode || sb);
2254         sbi = sb ? ll_s2sbi(sb) : ll_i2sbi(*inode);
2255
2256         rc = mdc_req2lustre_md(req, offset, exp, &md);
2257         if (rc)
2258                 RETURN(rc);
2259
2260         if (*inode) {
2261                 ll_update_inode(*inode, &md);
2262         } else {
2263                 LASSERT(sb);
2264                 /** hashing VFS inode by FIDs.
2265                  * IGIF will be used for for compatibility if needed.
2266                  */
2267                 *inode =ll_iget(sb, ll_fid_build_ino(&md.body->fid1), &md);
2268                 if (*inode == NULL || is_bad_inode(*inode)) {
2269                         mdc_free_lustre_md(exp, &md);
2270                         rc = -ENOMEM;
2271                         CERROR("new_inode -fatal: rc %d\n", rc);
2272                         GOTO(out, rc);
2273                 }
2274         }
2275
2276         rc = obd_checkmd(exp, ll_i2mdcexp(*inode),
2277                          ll_i2info(*inode)->lli_smd);
2278 out:
2279         RETURN(rc);
2280 }
2281
2282 char *llap_origins[] = {
2283         [LLAP_ORIGIN_UNKNOWN] = "--",
2284         [LLAP_ORIGIN_READPAGE] = "rp",
2285         [LLAP_ORIGIN_READAHEAD] = "ra",
2286         [LLAP_ORIGIN_COMMIT_WRITE] = "cw",
2287         [LLAP_ORIGIN_WRITEPAGE] = "wp"
2288 };
2289
2290 struct ll_async_page *llite_pglist_next_llap(struct list_head *head,
2291                                              struct list_head *list)
2292 {
2293         struct ll_async_page *llap;
2294         struct list_head *pos;
2295
2296         list_for_each(pos, list) {
2297                 if (pos == head)
2298                         return NULL;
2299                 llap = list_entry(pos, struct ll_async_page, llap_pglist_item);
2300                 if (llap->llap_page == NULL)
2301                         continue;
2302                 return llap;
2303         }
2304         LBUG();
2305         return NULL;
2306 }
2307
2308 int ll_obd_statfs(struct inode *inode, void *arg)
2309 {
2310         struct ll_sb_info *sbi = NULL;
2311         struct obd_device *client_obd = NULL, *lov_obd = NULL;
2312         struct lov_obd *lov = NULL;
2313         struct obd_statfs stat_buf = {0};
2314         char *buf = NULL;
2315         struct obd_ioctl_data *data = NULL;
2316         __u32 type, index;
2317         int len = 0, rc;
2318
2319         if (!inode || !(sbi = ll_i2sbi(inode)))
2320                 GOTO(out_statfs, rc = -EINVAL);
2321
2322         rc = obd_ioctl_getdata(&buf, &len, arg);
2323         if (rc)
2324                 GOTO(out_statfs, rc);
2325
2326         data = (void*)buf;
2327         if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
2328             !data->ioc_pbuf1 || !data->ioc_pbuf2)
2329                 GOTO(out_statfs, rc = -EINVAL);
2330
2331         memcpy(&type, data->ioc_inlbuf1, sizeof(__u32));
2332         memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
2333
2334         if (type == LL_STATFS_MDC) {
2335                 if (index > 0)
2336                         GOTO(out_statfs, rc = -ENODEV);
2337                 client_obd = class_exp2obd(sbi->ll_mdc_exp);
2338         } else if (type == LL_STATFS_LOV) {
2339                 lov_obd = class_exp2obd(sbi->ll_osc_exp);
2340                 lov = &lov_obd->u.lov;
2341
2342                 if (index >= lov->desc.ld_tgt_count)
2343                         GOTO(out_statfs, rc = -ENODEV);
2344
2345                 if (!lov->lov_tgts[index])
2346                         /* Try again with the next index */
2347                         GOTO(out_statfs, rc = -EAGAIN);
2348
2349                 client_obd = class_exp2obd(lov->lov_tgts[index]->ltd_exp);
2350                 if (!lov->lov_tgts[index]->ltd_active)
2351                         GOTO(out_uuid, rc = -ENODATA);
2352         }
2353
2354         if (!client_obd)
2355                 GOTO(out_statfs, rc = -EINVAL);
2356
2357         rc = obd_statfs(client_obd, &stat_buf,
2358                         cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS), 1);
2359         if (rc)
2360                 GOTO(out_uuid, rc);
2361
2362         if (copy_to_user(data->ioc_pbuf1, &stat_buf, data->ioc_plen1))
2363                 GOTO(out_statfs, rc = -EFAULT);
2364
2365 out_uuid:
2366         if (client_obd) {
2367                 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(client_obd),
2368                                  data->ioc_plen2))
2369                         rc = -EFAULT;
2370         } else {
2371                 if (copy_to_user(data->ioc_pbuf2, "Unknown UUID",
2372                                  sizeof("Unknown UUID") + 1))
2373                         rc = -EFAULT;
2374         }
2375
2376 out_statfs:
2377         if (buf)
2378                 obd_ioctl_freedata(buf, len);
2379         return rc;
2380 }
2381
2382 int ll_process_config(struct lustre_cfg *lcfg)
2383 {
2384         char *ptr;
2385         void *sb;
2386         struct lprocfs_static_vars lvars;
2387         unsigned long x;
2388         int rc = 0;
2389
2390         lprocfs_llite_init_vars(&lvars);
2391
2392         /* The instance name contains the sb: lustre-client-aacfe000 */
2393         ptr = strrchr(lustre_cfg_string(lcfg, 0), '-');
2394         if (!ptr || !*(++ptr))
2395                 return -EINVAL;
2396         if (sscanf(ptr, "%lx", &x) != 1)
2397                 return -EINVAL;
2398         sb = (void *)x;
2399         /* This better be a real Lustre superblock! */
2400         LASSERT(s2lsi((struct super_block *)sb)->lsi_lmd->lmd_magic == LMD_MAGIC);
2401
2402         /* Note we have not called client_common_fill_super yet, so
2403            proc fns must be able to handle that! */
2404         rc = class_process_proc_param(PARAM_LLITE, lvars.obd_vars,
2405                                       lcfg, sb);
2406         return(rc);
2407 }
2408
2409 int ll_show_options(struct seq_file *seq, struct vfsmount *vfs)
2410 {
2411         struct ll_sb_info *sbi;
2412
2413         LASSERT((seq != NULL) && (vfs != NULL));
2414         sbi = ll_s2sbi(vfs->mnt_sb);
2415
2416         if (sbi->ll_flags & LL_SBI_NOLCK)
2417                 seq_puts(seq, ",nolock");
2418
2419         if (sbi->ll_flags & LL_SBI_FLOCK)
2420                 seq_puts(seq, ",flock");
2421
2422         if (sbi->ll_flags & LL_SBI_LOCALFLOCK)
2423                 seq_puts(seq, ",localflock");
2424
2425         if (sbi->ll_flags & LL_SBI_USER_XATTR)
2426                 seq_puts(seq, ",user_xattr");
2427
2428         if (sbi->ll_flags & LL_SBI_ACL)
2429                 seq_puts(seq, ",acl");
2430
2431         if (sbi->ll_flags & LL_SBI_LAZYSTATFS)
2432                 seq_puts(seq, ",lazystatfs");
2433
2434         RETURN(0);
2435 }