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