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