Whamcloud - gitweb
75428102b3f9988c8bb281d9fc7129fc7ccdc2f4
[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  * Lustre Light Super operations
5  *
6  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_LLITE
25
26 #include <linux/module.h>
27 #include <linux/types.h>
28 #include <linux/random.h>
29 #include <linux/version.h>
30
31 #include <linux/lustre_lite.h>
32 #include <linux/lustre_ha.h>
33 #include <linux/lustre_dlm.h>
34 #include <linux/lprocfs_status.h>
35 #include "llite_internal.h"
36
37 kmem_cache_t *ll_file_data_slab;
38
39 extern struct address_space_operations ll_aops;
40 extern struct address_space_operations ll_dir_aops;
41
42 #ifndef log2
43 #define log2(n) ffz(~(n))
44 #endif
45
46 struct ll_sb_info *lustre_init_sbi(struct super_block *sb)
47 {
48         struct ll_sb_info *sbi = NULL;
49         class_uuid_t uuid;
50         ENTRY;
51
52         OBD_ALLOC(sbi, sizeof(*sbi));
53         if (!sbi)
54                 RETURN(NULL);
55
56         spin_lock_init(&sbi->ll_lock);
57         INIT_LIST_HEAD(&sbi->ll_pglist);
58         sbi->ll_pglist_gen = 0;
59         if (num_physpages < SBI_DEFAULT_RA_MAX / 4)
60                 sbi->ll_ra_info.ra_max_pages = num_physpages / 4;
61         else
62                 sbi->ll_ra_info.ra_max_pages = SBI_DEFAULT_RA_MAX;
63         INIT_LIST_HEAD(&sbi->ll_conn_chain);
64         INIT_HLIST_HEAD(&sbi->ll_orphan_dentry_list);
65         INIT_LIST_HEAD(&sbi->ll_mnt_list);
66         sema_init(&sbi->ll_gns_sem, 1);
67         init_completion(&sbi->ll_gns_completion);
68         sbi->ll_gns_state = LL_GNS_STATE_IDLE;
69         sbi->ll_gns_timer.data = (unsigned long)sbi;
70         sbi->ll_gns_timer.function = ll_gns_timer_callback;
71         init_timer(&sbi->ll_gns_timer);
72         INIT_LIST_HEAD(&sbi->ll_gns_sbi_head);
73
74         ll_set_sbi(sb, sbi);
75
76         generate_random_uuid(uuid);
77         class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
78         RETURN(sbi);
79 }
80
81 void lustre_free_sbi(struct super_block *sb)
82 {
83         struct ll_sb_info *sbi = ll_s2sbi(sb);
84         ENTRY;
85
86         if (sbi != NULL) {
87                 list_del(&sbi->ll_gns_sbi_head);
88                 del_timer(&sbi->ll_gns_timer);
89                 OBD_FREE(sbi, sizeof(*sbi));
90         }
91         ll_set_sbi(sb, NULL);
92         EXIT;
93 }
94
95 int lustre_init_lov_desc(struct ll_sb_info *sbi)
96 {
97         int valsize, rc;
98         ENTRY;
99         
100         valsize = sizeof(sbi->ll_lov_desc);
101         memset(&sbi->ll_lov_desc, 0, sizeof(sbi->ll_lov_desc));
102         rc = obd_get_info(sbi->ll_lov_exp, strlen("lovdesc") + 1,
103                           "lovdesc", &valsize, &sbi->ll_lov_desc);
104         RETURN(rc);
105 }
106
107 int lustre_init_lmv_desc(struct ll_sb_info *sbi)
108 {
109         int valsize, rc;
110         ENTRY;
111         
112         valsize = sizeof(sbi->ll_lmv_desc);
113         memset(&sbi->ll_lmv_desc, 0, sizeof(sbi->ll_lmv_desc));
114         rc = obd_get_info(sbi->ll_lmv_exp, strlen("lmvdesc") + 1,
115                           "lmvdesc", &valsize, &sbi->ll_lmv_desc);
116         RETURN(rc);
117 }
118
119 int lustre_common_fill_super(struct super_block *sb, char *lmv, char *lov)
120 {
121         struct ll_sb_info *sbi = ll_s2sbi(sb);
122         struct ptlrpc_request *request = NULL;
123         struct lustre_handle lov_conn = {0, };
124         struct lustre_handle lmv_conn = {0, };
125         struct inode *root = NULL;
126         struct obd_device *obd;
127         struct obd_statfs osfs;
128         struct lustre_md md;
129         kdev_t devno;
130         int err;
131
132         obd = class_name2obd(lmv);
133         if (!obd) {
134                 CERROR("MDC %s: not setup or attached\n", lmv);
135                 RETURN(-EINVAL);
136         }
137
138         if (proc_lustre_fs_root) {
139                 err = lprocfs_register_mountpoint(proc_lustre_fs_root, sb,
140                                                   lov, lmv);
141                 if (err < 0)
142                         CERROR("could not register mount in /proc/lustre");
143         }
144
145         err = obd_connect(&lmv_conn, obd, &sbi->ll_sb_uuid, OBD_OPT_REAL_CLIENT);
146         if (err == -EBUSY) {
147                 CERROR("An MDS (mdc %s) is performing recovery, of which this"
148                        " client is not a part.  Please wait for recovery to "
149                        "complete, abort, or time out.\n", lmv);
150                 GOTO(out, err);
151         } else if (err) {
152                 CERROR("cannot connect to %s: rc = %d\n", lmv, err);
153                 GOTO(out, err);
154         }
155         sbi->ll_lmv_exp = class_conn2export(&lmv_conn);
156
157         lustre_init_lmv_desc(sbi);
158         
159         err = obd_statfs(obd, &osfs, jiffies - HZ);
160         if (err)
161                 GOTO(out_lmv, err);
162
163         if (!osfs.os_bsize) {
164                 CERROR("Invalid block size is detected.");
165                 GOTO(out_lmv, err);
166         }
167
168         sb->s_magic = LL_SUPER_MAGIC;
169         sb->s_blocksize = osfs.os_bsize;
170         sb->s_blocksize_bits = log2(osfs.os_bsize);
171         sb->s_maxbytes = PAGE_CACHE_MAXBYTES;
172        
173         devno = get_uuid2int(sbi->ll_lmv_exp->exp_obd->obd_uuid.uuid, 
174                              strlen(sbi->ll_lmv_exp->exp_obd->obd_uuid.uuid));
175
176         sb->s_dev = devno;
177
178         obd = class_name2obd(lov);
179         if (!obd) {
180                 CERROR("OSC %s: not setup or attached\n", lov);
181                 GOTO(out_lmv, err);
182         }
183
184         err = obd_connect(&lov_conn, obd, &sbi->ll_sb_uuid, OBD_OPT_REAL_CLIENT);
185         if (err == -EBUSY) {
186                 CERROR("An OST (osc %s) is performing recovery, of which this"
187                        " client is not a part.  Please wait for recovery to "
188                        "complete, abort, or time out.\n", lov);
189                 GOTO(out, err);
190         } else if (err) {
191                 CERROR("cannot connect to %s: rc = %d\n", lov, err);
192                 GOTO(out_lmv, err);
193         }
194         sbi->ll_lov_exp = class_conn2export(&lov_conn);
195
196         err = lustre_init_lov_desc(sbi);
197         if (err == 0) {
198                 int mdsize = obd_size_diskmd(sbi->ll_lov_exp, NULL);
199                 obd_init_ea_size(sbi->ll_lmv_exp, mdsize,
200                                  sbi->ll_lov_desc.ld_tgt_count *
201                                  sizeof(struct llog_cookie));
202         }
203         
204         err = md_getstatus(sbi->ll_lmv_exp, &sbi->ll_rootid);
205         if (err) {
206                 CERROR("cannot mds_connect: rc = %d\n", err);
207                 GOTO(out_lov, err);
208         }
209         CDEBUG(D_SUPER, "rootid "DLID4"\n", OLID4(&sbi->ll_rootid));
210
211         sb->s_op = &lustre_super_operations;
212
213         /* make root inode
214          * XXX: move this to after cbd setup? */
215         err = md_getattr(sbi->ll_lmv_exp, &sbi->ll_rootid,
216                          OBD_MD_FLNOTOBD | OBD_MD_FLBLOCKS | OBD_MD_FID,
217                          0, &request);
218         if (err) {
219                 CERROR("md_getattr failed for root: rc = %d\n", err);
220                 GOTO(out_lov, err);
221         }
222
223         err = mdc_req2lustre_md(sbi->ll_lmv_exp, request, 0, 
224                                 sbi->ll_lov_exp, &md);
225         if (err) {
226                 CERROR("failed to understand root inode md: rc = %d\n", err);
227                 ptlrpc_req_finished(request);
228                 GOTO(out_lov, err);
229         }
230
231         LASSERT(id_ino(&sbi->ll_rootid) != 0);
232         root = ll_iget(sb, id_ino(&sbi->ll_rootid), &md);
233
234         ptlrpc_req_finished(request);
235
236         if (root == NULL || is_bad_inode(root)) {
237                 CERROR("lustre_lite: bad iget4 for root\n");
238                 GOTO(out_root, err = -EBADF);
239         }
240
241         err = ll_close_thread_start(&sbi->ll_lcq);
242         if (err) {
243                 CERROR("cannot start close thread: rc %d\n", err);
244                 GOTO(out_root, err);
245         }
246
247         ll_gns_add_timer(sbi);
248
249         /* making vm readahead 0 for 2.4.x. In the case of 2.6.x, backing dev
250            info assigned to inode mapping is used for determining maximal
251            readahead. */
252 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
253         /* bug 2805 - set VM readahead to zero */
254         vm_max_readahead = vm_min_readahead = 0;
255 #endif
256
257         sb->s_root = d_alloc_root(root);
258
259 #ifdef S_PDIROPS
260         CWARN("Enabling PDIROPS\n");
261         sb->s_flags |= S_PDIROPS;
262 #endif
263
264         RETURN(err);
265
266 out_root:
267         if (root)
268                 iput(root);
269 out_lov:
270         obd_disconnect(sbi->ll_lov_exp, 0);
271 out_lmv:
272         obd_disconnect(sbi->ll_lmv_exp, 0);
273 out:
274         lprocfs_unregister_mountpoint(sbi);
275         RETURN(err);
276 }
277
278 void lustre_common_put_super(struct super_block *sb)
279 {
280         struct ll_sb_info *sbi = ll_s2sbi(sb);
281         struct hlist_node *tmp, *next;
282         ENTRY;
283
284         ll_gns_del_timer(sbi);
285         ll_close_thread_shutdown(sbi->ll_lcq);
286
287         list_del(&sbi->ll_conn_chain);
288         obd_disconnect(sbi->ll_lov_exp, 0);
289
290         lprocfs_unregister_mountpoint(sbi);
291         if (sbi->ll_proc_root) {
292                 lprocfs_remove(sbi->ll_proc_root);
293                 sbi->ll_proc_root = NULL;
294         }
295
296         obd_disconnect(sbi->ll_lmv_exp, 0);
297
298         // We do this to get rid of orphaned dentries. That is not really trw.
299         spin_lock(&dcache_lock);
300         hlist_for_each_safe(tmp, next, &sbi->ll_orphan_dentry_list) {
301                 struct dentry *dentry = hlist_entry(tmp, struct dentry, d_hash);
302                 CWARN("orphan dentry %*s (%p) at unmount\n",
303                       dentry->d_name.len, dentry->d_name.name, dentry);
304                 shrink_dcache_parent(dentry);
305         }
306         spin_unlock(&dcache_lock);
307         EXIT;
308 }
309
310 char *ll_read_opt(const char *opt, char *data)
311 {
312         char *value;
313         char *retval;
314         ENTRY;
315
316         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
317         if (strncmp(opt, data, strlen(opt)))
318                 RETURN(NULL);
319         if ((value = strchr(data, '=')) == NULL)
320                 RETURN(NULL);
321
322         value++;
323         OBD_ALLOC(retval, strlen(value) + 1);
324         if (!retval) {
325                 CERROR("out of memory!\n");
326                 RETURN(NULL);
327         }
328
329         memcpy(retval, value, strlen(value)+1);
330         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
331         RETURN(retval);
332 }
333
334 int ll_set_opt(const char *opt, char *data, int fl)
335 {
336         ENTRY;
337
338         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
339         if (strncmp(opt, data, strlen(opt)))
340                 RETURN(0);
341         else
342                 RETURN(fl);
343 }
344
345 void ll_options(char *options, char **lov, char **lmv, int *flags)
346 {
347         char *this_char;
348 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
349         char *opt_ptr = options;
350 #endif
351         ENTRY;
352
353         if (!options) {
354                 EXIT;
355                 return;
356         }
357
358 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
359         for (this_char = strtok (options, ",");
360              this_char != NULL;
361              this_char = strtok (NULL, ",")) {
362 #else
363         while ((this_char = strsep (&opt_ptr, ",")) != NULL) {
364 #endif
365                 CDEBUG(D_SUPER, "this_char %s\n", this_char);
366                 if (!*lov && (*lov = ll_read_opt("osc", this_char)))
367                         continue;
368                 if (!*lmv && (*lmv = ll_read_opt("mdc", this_char)))
369                         continue;
370                 if (!(*flags & LL_SBI_NOLCK) &&
371                     ((*flags) = (*flags) |
372                                 ll_set_opt("nolock", this_char,
373                                            LL_SBI_NOLCK)))
374                         continue;
375         }
376         
377         EXIT;
378 }
379
380 void ll_lli_init(struct ll_inode_info *lli)
381 {
382         sema_init(&lli->lli_open_sem, 1);
383         lli->lli_flags = 0;
384         lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
385         spin_lock_init(&lli->lli_lock);
386         INIT_LIST_HEAD(&lli->lli_pending_write_llaps);
387         lli->lli_inode_magic = LLI_INODE_MAGIC;
388         memset(&lli->lli_id, 0, sizeof(lli->lli_id));
389 }
390
391 int ll_fill_super(struct super_block *sb, void *data, int silent)
392 {
393         struct ll_sb_info *sbi;
394         char *lov = NULL;
395         char *lmv = NULL;
396         int err;
397         ENTRY;
398
399         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
400
401         sbi = lustre_init_sbi(sb);
402         if (!sbi)
403                 RETURN(-ENOMEM);
404
405         sbi->ll_flags |= LL_SBI_READAHEAD;
406         ll_options(data, &lov, &lmv, &sbi->ll_flags);
407
408         if (!lov) {
409                 CERROR("no osc\n");
410                 GOTO(out, err = -EINVAL);
411         }
412
413         if (!lmv) {
414                 CERROR("no mdc\n");
415                 GOTO(out, err = -EINVAL);
416         }
417
418         err = lustre_common_fill_super(sb, lmv, lov);
419 out:
420         if (err)
421                 lustre_free_sbi(sb);
422
423         if (lmv)
424                 OBD_FREE(lmv, strlen(lmv) + 1);
425         if (lov)
426                 OBD_FREE(lov, strlen(lov) + 1);
427         RETURN(err);
428 } /* ll_read_super */
429
430 static int lustre_process_log(struct lustre_mount_data *lmd, char *profile,
431                               struct config_llog_instance *cfg, int allow_recov)
432 {
433         struct lustre_cfg lcfg;
434         struct portals_cfg pcfg;
435         char *peer = "MDS_PEER_UUID";
436         struct obd_device *obd;
437         struct lustre_handle lmv_conn = {0, };
438         struct obd_export *exp;
439         char *name = "mdc_dev";
440         class_uuid_t uuid;
441         struct obd_uuid lmv_uuid;
442         struct llog_ctxt *ctxt;
443         int rc = 0;
444         int err;
445         ENTRY;
446
447         if (lmd_bad_magic(lmd))
448                 RETURN(-EINVAL);
449
450         generate_random_uuid(uuid);
451         class_uuid_unparse(uuid, &lmv_uuid);
452
453         if (lmd->lmd_local_nid) {
454                 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
455                 pcfg.pcfg_nal = lmd->lmd_nal;
456                 pcfg.pcfg_nid = lmd->lmd_local_nid;
457                 err = libcfs_nal_cmd(&pcfg);
458                 if (err <0)
459                         GOTO(out, err);
460         }
461
462         if (lmd->lmd_nal == SOCKNAL ||
463               lmd->lmd_nal == OPENIBNAL) {
464                 PCFG_INIT(pcfg, NAL_CMD_ADD_PEER);
465                 pcfg.pcfg_nal     = lmd->lmd_nal;
466                 pcfg.pcfg_nid     = lmd->lmd_server_nid;
467                 pcfg.pcfg_id      = lmd->lmd_server_ipaddr;
468                 pcfg.pcfg_misc    = lmd->lmd_port;
469                 pcfg.pcfg_size    = 8388608;
470                 pcfg.pcfg_flags   = 0x4; /*share*/
471                 err = libcfs_nal_cmd(&pcfg);
472                 if (err <0)
473                         GOTO(out, err);
474         }
475
476         LCFG_INIT(lcfg, LCFG_ADD_UUID, name);
477         lcfg.lcfg_nid = lmd->lmd_server_nid;
478         lcfg.lcfg_inllen1 = strlen(peer) + 1;
479         lcfg.lcfg_inlbuf1 = peer;
480         lcfg.lcfg_nal = lmd->lmd_nal;
481         err = class_process_config(&lcfg);
482         if (err < 0)
483                 GOTO(out_del_conn, err);
484
485         LCFG_INIT(lcfg, LCFG_ATTACH, name);
486         lcfg.lcfg_inlbuf1 = "mdc";
487         lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
488         lcfg.lcfg_inlbuf2 = lmv_uuid.uuid;
489         lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
490         err = class_process_config(&lcfg);
491         if (err < 0)
492                 GOTO(out_del_uuid, err);
493
494         LCFG_INIT(lcfg, LCFG_SETUP, name);
495         lcfg.lcfg_inlbuf1 = lmd->lmd_mds;
496         lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
497         lcfg.lcfg_inlbuf2 = peer;
498         lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
499         err = class_process_config(&lcfg);
500         if (err < 0)
501                 GOTO(out_detach, err);
502
503         obd = class_name2obd(name);
504         if (obd == NULL)
505                 GOTO(out_cleanup, err = -EINVAL);
506
507         /* Disable initial recovery on this import */
508         err = obd_set_info(obd->obd_self_export,
509                            strlen("initial_recov"), "initial_recov",
510                            sizeof(allow_recov), &allow_recov);
511         if (err)
512                 GOTO(out_cleanup, err);
513
514         err = obd_connect(&lmv_conn, obd, &lmv_uuid, 0);
515         if (err) {
516                 CERROR("cannot connect to %s: rc = %d\n", lmd->lmd_mds, err);
517                 GOTO(out_cleanup, err);
518         }
519
520         exp = class_conn2export(&lmv_conn);
521
522         ctxt = llog_get_context(&exp->exp_obd->obd_llogs,LLOG_CONFIG_REPL_CTXT);
523         rc = class_config_process_llog(ctxt, profile, cfg);
524         if (rc)
525                 CERROR("class_config_process_llog failed: rc = %d\n", rc);
526
527         err = obd_disconnect(exp, 0);
528
529 out_cleanup:
530         LCFG_INIT(lcfg, LCFG_CLEANUP, name);
531         err = class_process_config(&lcfg);
532         if (err < 0)
533                 GOTO(out, err);
534
535 out_detach:
536         LCFG_INIT(lcfg, LCFG_DETACH, name);
537         err = class_process_config(&lcfg);
538         if (err < 0)
539                 GOTO(out, err);
540
541 out_del_uuid:
542         LCFG_INIT(lcfg, LCFG_DEL_UUID, name);
543         lcfg.lcfg_inllen1 = strlen(peer) + 1;
544         lcfg.lcfg_inlbuf1 = peer;
545         err = class_process_config(&lcfg);
546
547 out_del_conn:
548         if (lmd->lmd_nal == SOCKNAL ||
549             lmd->lmd_nal == OPENIBNAL) {
550                 PCFG_INIT(pcfg, NAL_CMD_DEL_PEER);
551                 pcfg.pcfg_nal     = lmd->lmd_nal;
552                 pcfg.pcfg_nid     = lmd->lmd_server_nid;
553                 pcfg.pcfg_flags   = 1;          /* single_share */
554                 err = libcfs_nal_cmd(&pcfg);
555                 if (err <0)
556                         GOTO(out, err);
557         }
558 out:
559         if (rc == 0)
560                 rc = err;
561
562         RETURN(rc);
563 }
564
565 int lustre_fill_super(struct super_block *sb, void *data, int silent)
566 {
567         struct lustre_mount_data * lmd = data;
568         char *lov = NULL, *lmv = NULL;
569         struct ll_sb_info *sbi;
570         int err;
571         ENTRY;
572
573         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
574         if (lmd_bad_magic(lmd))
575                 RETURN(-EINVAL);
576
577         sbi = lustre_init_sbi(sb);
578         if (!sbi)
579                 RETURN(-ENOMEM);
580
581         sbi->ll_flags |= LL_SBI_READAHEAD;
582
583         if (lmd->lmd_profile) {
584                 struct lustre_profile *lprof;
585                 struct config_llog_instance cfg;
586                 int len;
587
588                 if (lmd->lmd_mds[0] == '\0') {
589                         CERROR("no mds name\n");
590                         GOTO(out_free, err = -EINVAL);
591                 }
592
593                 OBD_ALLOC(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
594                 if (sbi->ll_lmd == NULL)
595                         GOTO(out_free, err = -ENOMEM);
596                 memcpy(sbi->ll_lmd, lmd, sizeof(*lmd));
597
598                 /* generate a string unique to this super, let's try
599                  the address of the super itself.*/
600                 len = (sizeof(sb) * 2) + 1;
601                 OBD_ALLOC(sbi->ll_instance, len);
602                 if (sbi->ll_instance == NULL)
603                         GOTO(out_free, err = -ENOMEM);
604                 sprintf(sbi->ll_instance, "%p", sb);
605
606                 cfg.cfg_instance = sbi->ll_instance;
607                 cfg.cfg_uuid = sbi->ll_sb_uuid;
608                 cfg.cfg_local_nid = lmd->lmd_local_nid;
609                 err = lustre_process_log(lmd, lmd->lmd_profile, &cfg, 0);
610                 if (err < 0) {
611                         CERROR("Unable to process log: %s\n", lmd->lmd_profile);
612                         GOTO(out_free, err);
613                 }
614
615                 lprof = class_get_profile(lmd->lmd_profile);
616                 if (lprof == NULL) {
617                         CERROR("No profile found: %s\n", lmd->lmd_profile);
618                         GOTO(out_free, err = -EINVAL);
619                 }
620                 if (lov)
621                         OBD_FREE(lov, strlen(lov) + 1);
622                 OBD_ALLOC(lov, strlen(lprof->lp_lov) +
623                           strlen(sbi->ll_instance) + 2);
624                 sprintf(lov, "%s-%s", lprof->lp_lov, sbi->ll_instance);
625
626                 if (lmv)
627                         OBD_FREE(lmv, strlen(lmv) + 1);
628                 OBD_ALLOC(lmv, strlen(lprof->lp_lmv) +
629                           strlen(sbi->ll_instance) + 2);
630                 sprintf(lmv, "%s-%s", lprof->lp_lmv, sbi->ll_instance);
631         }
632
633         if (!lov) {
634                 CERROR("no osc\n");
635                 GOTO(out_free, err = -EINVAL);
636         }
637
638         if (!lmv) {
639                 CERROR("no mdc\n");
640                 GOTO(out_free, err = -EINVAL);
641         }
642
643         err = lustre_common_fill_super(sb, lmv, lov);
644
645         if (err)
646                 GOTO(out_free, err);
647         
648 out_dev:
649         if (lmv)
650                 OBD_FREE(lmv, strlen(lmv) + 1);
651         if (lov)
652                 OBD_FREE(lov, strlen(lov) + 1);
653
654         RETURN(err);
655
656 out_free:
657         if (sbi->ll_lmd) {
658                 int len = strlen(sbi->ll_lmd->lmd_profile) + sizeof("-clean")+1;
659                 int err;
660
661                 if (sbi->ll_instance != NULL) {
662                         struct lustre_mount_data *lmd = sbi->ll_lmd;
663                         char * cln_prof;
664                         struct config_llog_instance cfg;
665
666                         cfg.cfg_instance = sbi->ll_instance;
667                         cfg.cfg_uuid = sbi->ll_sb_uuid;
668
669                         OBD_ALLOC(cln_prof, len);
670                         sprintf(cln_prof, "%s-clean", lmd->lmd_profile);
671
672                         err = lustre_process_log(lmd, cln_prof, &cfg, 0);
673                         if (err < 0)
674                                 CERROR("Unable to process log: %s\n", cln_prof);
675                         OBD_FREE(cln_prof, len);
676                         OBD_FREE(sbi->ll_instance, strlen(sbi->ll_instance)+ 1);
677                 }
678                 OBD_FREE(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
679         }
680         lustre_free_sbi(sb);
681
682         goto out_dev;
683 } /* lustre_fill_super */
684
685 static void lustre_manual_cleanup(struct ll_sb_info *sbi)
686 {
687         struct lustre_cfg lcfg;
688         struct obd_device *obd;
689         int next = 0;
690
691         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) != NULL)
692         {
693                 int err;
694
695                 LCFG_INIT(lcfg, LCFG_CLEANUP, obd->obd_name);
696                 err = class_process_config(&lcfg);
697                 if (err) {
698                         CERROR("cleanup failed: %s\n", obd->obd_name);
699                         //continue;
700                 }
701
702                 LCFG_INIT(lcfg, LCFG_DETACH, obd->obd_name);
703                 err = class_process_config(&lcfg);
704                 if (err) {
705                         CERROR("detach failed: %s\n", obd->obd_name);
706                         //continue;
707                 }
708         }
709
710         if (sbi->ll_lmd != NULL)
711                 class_del_profile(sbi->ll_lmd->lmd_profile);
712 }
713
714 void lustre_put_super(struct super_block *sb)
715 {
716         struct obd_device *obd;
717         struct ll_sb_info *sbi = ll_s2sbi(sb);
718         int force_umount = 0;
719         ENTRY;
720
721         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
722         obd = class_exp2obd(sbi->ll_lmv_exp);
723         if (obd)
724                 force_umount = obd->obd_no_recov;
725         obd = NULL;
726
727         lustre_common_put_super(sb);
728         if (sbi->ll_lmd != NULL) {
729                 char * cln_prof;
730                 int len = strlen(sbi->ll_lmd->lmd_profile) + sizeof("-clean")+1;
731                 int err;
732                 struct config_llog_instance cfg;
733
734                 if (force_umount) {
735                         CERROR("force umount, doing manual cleanup\n");
736                         lustre_manual_cleanup(sbi);
737                         GOTO(free_lmd, 0);
738                 }
739
740                 cfg.cfg_instance = sbi->ll_instance;
741                 cfg.cfg_uuid = sbi->ll_sb_uuid;
742
743                 OBD_ALLOC(cln_prof, len);
744                 sprintf(cln_prof, "%s-clean", sbi->ll_lmd->lmd_profile);
745
746                 err = lustre_process_log(sbi->ll_lmd, cln_prof, &cfg, 0);
747                 if (err < 0) {
748                         CERROR("Unable to process log: %s, doing manual cleanup"
749                                "\n", cln_prof);
750                         lustre_manual_cleanup(sbi);
751                 }
752
753                 OBD_FREE(cln_prof, len);
754         free_lmd:
755                 OBD_FREE(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
756                 OBD_FREE(sbi->ll_instance, strlen(sbi->ll_instance) + 1);
757         }
758
759         lustre_free_sbi(sb);
760
761         EXIT;
762 } /* lustre_put_super */
763
764 int ll_process_config_update(struct ll_sb_info *sbi, int clean)
765 {
766         struct obd_export *lmv_exp = sbi->ll_lmv_exp;
767         struct lustre_mount_data *lmd = sbi->ll_lmd;
768         struct llog_ctxt *ctxt;
769         struct config_llog_instance cfg;
770         char *profile = lmd->lmd_profile, *name = NULL;
771         int rc, namelen =  0, version;
772         ENTRY;
773
774         if (profile == NULL)
775                 RETURN(0);
776         if (lmd == NULL) {
777                 CERROR("Client not mounted with zero-conf; cannot process "
778                        "update log.\n");
779                 RETURN(0);
780         }
781
782         rc = ldlm_cli_cancel_unused(lmv_exp->exp_obd->obd_namespace, NULL,
783                                     LDLM_FL_CONFIG_CHANGE, NULL);
784         if (rc != 0)
785                 CWARN("ldlm_cli_cancel_unused(mdc): %d\n", rc);
786
787         rc = obd_cancel_unused(sbi->ll_lov_exp, NULL, LDLM_FL_CONFIG_CHANGE,
788                                NULL);
789         if (rc != 0)
790                 CWARN("obd_cancel_unused(lov): %d\n", rc);
791
792         cfg.cfg_instance = sbi->ll_instance;
793         cfg.cfg_uuid = sbi->ll_sb_uuid;
794         cfg.cfg_local_nid = lmd->lmd_local_nid;
795
796         namelen = strlen(profile) + 20; /* -clean-######### */
797         OBD_ALLOC(name, namelen);
798         if (name == NULL)
799                 RETURN(-ENOMEM);
800
801         if (clean) {
802                 version = sbi->ll_config_version - 1;
803                 sprintf(name, "%s-clean-%d", profile, version);
804         } else {
805                 version = sbi->ll_config_version + 1;
806                 sprintf(name, "%s-%d", profile, version);
807         }
808
809         CWARN("Applying configuration log %s\n", name);
810
811         ctxt = llog_get_context(&lmv_exp->exp_obd->obd_llogs,
812                                 LLOG_CONFIG_REPL_CTXT);
813         rc = class_config_process_llog(ctxt, name, &cfg);
814         if (rc == 0)
815                 sbi->ll_config_version = version;
816         CWARN("Finished applying configuration log %s: %d\n", name, rc);
817
818         if (rc == 0 && clean == 0) {
819                 struct lov_desc desc;
820                 int rc, valsize;
821                 valsize = sizeof(desc);
822                 rc = obd_get_info(sbi->ll_lov_exp, strlen("lovdesc") + 1,
823                                   "lovdesc", &valsize, &desc);
824
825                 rc = obd_init_ea_size(lmv_exp,
826                                       obd_size_diskmd(sbi->ll_lov_exp, NULL),
827                                       (desc.ld_tgt_count *
828                                        sizeof(struct llog_cookie)));
829         }
830         OBD_FREE(name, namelen);
831         RETURN(rc);
832 }
833
834 struct inode *ll_inode_from_lock(struct ldlm_lock *lock)
835 {
836         struct inode *inode = NULL;
837         l_lock(&lock->l_resource->lr_namespace->ns_lock);
838         if (lock->l_ast_data) {
839                 struct ll_inode_info *lli = ll_i2info(lock->l_ast_data);
840                 if (lli->lli_inode_magic == LLI_INODE_MAGIC) {
841                         inode = igrab(lock->l_ast_data);
842                 } else {
843                         CERROR("DEBUG: l_ast_data %p is bogus: magic %x\n",
844                                lock->l_ast_data, lli->lli_inode_magic);
845                 }
846         }
847         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
848         return inode;
849 }
850
851 int null_if_equal(struct ldlm_lock *lock, void *data)
852 {
853         if (data == lock->l_ast_data) {
854                 lock->l_ast_data = NULL;
855
856                 if (lock->l_req_mode != lock->l_granted_mode)
857                         LDLM_ERROR(lock,"clearing inode with ungranted lock\n");
858         }
859
860         return LDLM_ITER_CONTINUE;
861 }
862
863 void ll_clear_inode(struct inode *inode)
864 {
865         struct lustre_id id;
866         struct ll_inode_info *lli = ll_i2info(inode);
867         struct ll_sb_info *sbi = ll_i2sbi(inode);
868         ENTRY;
869
870         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
871                inode->i_generation, inode);
872
873         lli->lli_inode_magic = LLI_INODE_DEAD;
874         ll_inode2id(&id, inode);
875         
876         clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &(ll_i2info(inode)->lli_flags));
877         md_change_cbdata(sbi->ll_lmv_exp, &id, null_if_equal, inode);
878
879         if (lli->lli_smd)
880                 obd_change_cbdata(sbi->ll_lov_exp, lli->lli_smd,
881                                   null_if_equal, inode);
882
883         if (lli->lli_smd) {
884                 obd_free_memmd(sbi->ll_lov_exp, &lli->lli_smd);
885                 lli->lli_smd = NULL;
886         }
887
888         if (lli->lli_mea) {
889                 obd_free_memmd(sbi->ll_lmv_exp,
890                                (struct lov_stripe_md **) &lli->lli_mea);
891                 lli->lli_mea = NULL;
892         }
893
894         if (lli->lli_symlink_name) {
895                 OBD_FREE(lli->lli_symlink_name,
896                          strlen(lli->lli_symlink_name) + 1);
897                 lli->lli_symlink_name = NULL;
898         }
899
900         EXIT;
901 }
902
903 /* If this inode has objects allocated to it (lsm != NULL), then the OST
904  * object(s) determine the file size and mtime.  Otherwise, the MDS will
905  * keep these values until such a time that objects are allocated for it.
906  * We do the MDS operations first, as it is checking permissions for us.
907  * We don't to the MDS RPC if there is nothing that we want to store there,
908  * otherwise there is no harm in updating mtime/atime on the MDS if we are
909  * going to do an RPC anyways.
910  *
911  * If we are doing a truncate, we will send the mtime and ctime updates
912  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
913  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
914  * at the same time.
915  */
916 int ll_setattr_raw(struct inode *inode, struct iattr *attr)
917 {
918         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
919         struct ll_sb_info *sbi = ll_i2sbi(inode);
920         struct ptlrpc_request *request = NULL;
921         struct mdc_op_data op_data;
922         int ia_valid = attr->ia_valid;
923         int rc = 0;
924         ENTRY;
925
926         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", inode->i_ino);
927         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_SETATTR);
928
929         if (ia_valid & ATTR_SIZE) {
930                 if (attr->ia_size > ll_file_maxbytes(inode)) {
931                         CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
932                                attr->ia_size, ll_file_maxbytes(inode));
933                         RETURN(-EFBIG);
934                 }
935
936                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
937         }
938
939         /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
940         if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
941                 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
942                         RETURN(-EPERM);
943         }
944
945         /* We mark all of the fields "set" so MDS/OST does not re-set them */
946         if (attr->ia_valid & ATTR_CTIME) {
947                 attr->ia_ctime = CURRENT_TIME;
948                 attr->ia_valid |= ATTR_CTIME_SET;
949         }
950         if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
951                 attr->ia_atime = CURRENT_TIME;
952                 attr->ia_valid |= ATTR_ATIME_SET;
953         }
954         if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
955                 attr->ia_mtime = CURRENT_TIME;
956                 attr->ia_valid |= ATTR_MTIME_SET;
957         }
958
959         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
960                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
961                        LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
962                        LTIME_S(CURRENT_TIME));
963         if (lsm)
964                 attr->ia_valid &= ~ATTR_SIZE;
965
966         /* If only OST attributes being set on objects, don't do MDS RPC.
967          * In that case, we need to check permissions and update the local
968          * inode ourselves so we can call obdo_from_inode() always. */
969         if (ia_valid & (lsm ? ~(ATTR_SIZE | ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
970                 struct lustre_md md;
971                 ll_prepare_mdc_data(&op_data, inode, NULL, NULL, 0, 0);
972
973                 rc = md_setattr(sbi->ll_lmv_exp, &op_data,
974                                 attr, NULL, 0, NULL, 0, &request);
975                 if (rc) {
976                         ptlrpc_req_finished(request);
977                         if (rc != -EPERM && rc != -EACCES)
978                                 CERROR("md_setattr fails: rc = %d\n", rc);
979                         RETURN(rc);
980                 }
981
982                 rc = mdc_req2lustre_md(sbi->ll_lmv_exp, request, 0, 
983                                        sbi->ll_lov_exp, &md);
984                 if (rc) {
985                         ptlrpc_req_finished(request);
986                         RETURN(rc);
987                 }
988
989                 /* Won't invoke vmtruncate as we already cleared ATTR_SIZE,
990                  * but needed to set timestamps backwards on utime. */
991                 inode_setattr(inode, attr);
992                 ll_update_inode(inode, &md);
993                 ptlrpc_req_finished(request);
994
995                 if (!lsm || !S_ISREG(inode->i_mode)) {
996                         CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
997                         RETURN(0);
998                 }
999         } else {
1000                 /* The OST doesn't check permissions, but the alternative is
1001                  * a gratuitous RPC to the MDS.  We already rely on the client
1002                  * to do read/write/truncate permission checks, so is mtime OK?
1003                  */
1004                 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
1005                         /* from sys_utime() */
1006                         if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
1007                                 if (current->fsuid != inode->i_uid &&
1008                                     (rc=ll_permission(inode,MAY_WRITE,NULL))!=0)
1009                                         RETURN(rc);
1010                         } else {
1011                                 /* from inode_change_ok() */
1012                                 if (current->fsuid != inode->i_uid &&
1013                                     !capable(CAP_FOWNER))
1014                                         RETURN(-EPERM);
1015                         }
1016                 }
1017
1018                 /* Won't invoke vmtruncate, as we already cleared ATTR_SIZE */
1019                 inode_setattr(inode, attr);
1020         }
1021
1022         /* We really need to get our PW lock before we change inode->i_size.
1023          * If we don't we can race with other i_size updaters on our node, like
1024          * ll_file_read.  We can also race with i_size propogation to other
1025          * nodes through dirtying and writeback of final cached pages.  This
1026          * last one is especially bad for racing o_append users on other
1027          * nodes. */
1028         if (ia_valid & ATTR_SIZE) {
1029                 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
1030                                                            OBD_OBJECT_EOF } };
1031                 struct lustre_handle lockh = { 0 };
1032                 int err, ast_flags = 0;
1033                 /* XXX when we fix the AST intents to pass the discard-range
1034                  * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
1035                  * XXX here. */
1036                 if (attr->ia_size == 0)
1037                         ast_flags = LDLM_AST_DISCARD_DATA;
1038
1039                 /* bug 1639: avoid write/truncate i_sem/DLM deadlock */
1040                 LASSERT(atomic_read(&inode->i_sem.count) <= 0);
1041                 up(&inode->i_sem);
1042                 rc = ll_extent_lock(NULL, inode, lsm, LCK_PW, &policy, &lockh,
1043                                     ast_flags, &ll_i2sbi(inode)->ll_seek_stime);
1044                 down(&inode->i_sem);
1045                 if (rc != 0)
1046                         RETURN(rc);
1047
1048                 rc = vmtruncate(inode, attr->ia_size);
1049
1050                 /* We need to drop the semaphore here, because this unlock may
1051                  * result in a cancellation, which will need the i_sem */
1052                 up(&inode->i_sem);
1053                 /* unlock now as we don't mind others file lockers racing with
1054                  * the mds updates below? */
1055                 err = ll_extent_unlock(NULL, inode, lsm, LCK_PW, &lockh);
1056                 down(&inode->i_sem);
1057                 if (err) {
1058                         CERROR("ll_extent_unlock failed: %d\n", err);
1059                         if (!rc)
1060                                 rc = err;
1061                 }
1062         } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
1063                 struct obdo oa;
1064
1065                 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
1066                        inode->i_ino, LTIME_S(attr->ia_mtime));
1067                 oa.o_id = lsm->lsm_object_id;
1068                 oa.o_gr = lsm->lsm_object_gr;
1069                 oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1070                 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
1071                                             OBD_MD_FLMTIME | OBD_MD_FLCTIME);
1072                 rc = obd_setattr(sbi->ll_lov_exp, &oa, lsm, NULL);
1073                 if (rc)
1074                         CERROR("obd_setattr fails: rc=%d\n", rc);
1075         }
1076         RETURN(rc);
1077 }
1078
1079 int ll_setattr(struct dentry *de, struct iattr *attr)
1080 {
1081         LBUG(); /* code is unused, but leave this in case of VFS changes */
1082         RETURN(-ENOSYS);
1083 }
1084
1085 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
1086                        unsigned long max_age)
1087 {
1088         struct ll_sb_info *sbi = ll_s2sbi(sb);
1089         struct obd_statfs obd_osfs;
1090         int rc;
1091         ENTRY;
1092
1093         rc = obd_statfs(class_exp2obd(sbi->ll_lmv_exp), osfs, max_age);
1094         if (rc) {
1095                 CERROR("mdc_statfs fails: rc = %d\n", rc);
1096                 RETURN(rc);
1097         }
1098
1099         osfs->os_type = sb->s_magic;
1100
1101         CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1102                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1103
1104         rc = obd_statfs(class_exp2obd(sbi->ll_lov_exp), &obd_osfs, max_age);
1105         if (rc) {
1106                 CERROR("obd_statfs fails: rc = %d\n", rc);
1107                 RETURN(rc);
1108         }
1109
1110         CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1111                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1112                obd_osfs.os_files);
1113
1114         osfs->os_blocks = obd_osfs.os_blocks;
1115         osfs->os_bfree = obd_osfs.os_bfree;
1116         osfs->os_bavail = obd_osfs.os_bavail;
1117
1118         /* If we don't have as many objects free on the OST as inodes
1119          * on the MDS, we reduce the total number of inodes to
1120          * compensate, so that the "inodes in use" number is correct.
1121          */
1122         if (obd_osfs.os_ffree < osfs->os_ffree) {
1123                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1124                         obd_osfs.os_ffree;
1125                 osfs->os_ffree = obd_osfs.os_ffree;
1126         }
1127
1128         RETURN(rc);
1129 }
1130
1131 int ll_statfs(struct super_block *sb, struct kstatfs *sfs)
1132 {
1133         struct obd_statfs osfs;
1134         int rc;
1135
1136         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p\n", sb);
1137         lprocfs_counter_incr(ll_s2sbi(sb)->ll_stats, LPROC_LL_STAFS);
1138
1139         /* For now we will always get up-to-date statfs values, but in the
1140          * future we may allow some amount of caching on the client (e.g.
1141          * from QOS or lprocfs updates). */
1142         rc = ll_statfs_internal(sb, &osfs, jiffies - 1);
1143         if (rc)
1144                 return rc;
1145
1146         statfs_unpack(sfs, &osfs);
1147
1148         if (sizeof(sfs->f_blocks) == 4) {
1149                 while (osfs.os_blocks > ~0UL) {
1150                         sfs->f_bsize <<= 1;
1151
1152                         osfs.os_blocks >>= 1;
1153                         osfs.os_bfree >>= 1;
1154                         osfs.os_bavail >>= 1;
1155                 }
1156         }
1157
1158         sfs->f_blocks = osfs.os_blocks;
1159         sfs->f_bfree = osfs.os_bfree;
1160         sfs->f_bavail = osfs.os_bavail;
1161
1162         return 0;
1163 }
1164
1165 void ll_update_inode(struct inode *inode, struct lustre_md *md)
1166 {
1167         struct ll_sb_info *sbi = ll_s2sbi(inode->i_sb);
1168         struct ll_inode_info *lli = ll_i2info(inode);
1169         struct lov_stripe_md *lsm = md->lsm;
1170         struct mds_body *body = md->body;
1171         struct mea *mea = md->mea;
1172         ENTRY;
1173
1174         LASSERT((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
1175         LASSERT((mea != NULL) == ((body->valid & OBD_MD_FLDIREA) != 0));
1176         if (lsm != NULL) {
1177                 LASSERT(lsm->lsm_object_gr > 0);
1178                 if (lli->lli_smd == NULL) {
1179                         lli->lli_smd = lsm;
1180                         lli->lli_maxbytes = lsm->lsm_maxbytes;
1181                         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1182                                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1183                 } else {
1184                         int i;
1185                         if (memcmp(lli->lli_smd, lsm, sizeof(*lsm))) {
1186                                 CERROR("lsm mismatch for inode %ld\n",
1187                                        inode->i_ino);
1188                                 CERROR("lli_smd:\n");
1189                                 dump_lsm(D_ERROR, lli->lli_smd);
1190                                 CERROR("lsm:\n");
1191                                 dump_lsm(D_ERROR, lsm);
1192                                 LBUG();
1193                         }
1194                         /* XXX FIXME -- We should decide on a safer (atomic) and
1195                          * more elegant way to update the lsm */
1196                         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1197                                 lli->lli_smd->lsm_oinfo[i].loi_id =
1198                                         lsm->lsm_oinfo[i].loi_id;
1199                                 lli->lli_smd->lsm_oinfo[i].loi_gr =
1200                                         lsm->lsm_oinfo[i].loi_gr;
1201                                 lli->lli_smd->lsm_oinfo[i].loi_ost_idx =
1202                                         lsm->lsm_oinfo[i].loi_ost_idx;
1203                                 lli->lli_smd->lsm_oinfo[i].loi_ost_gen =
1204                                         lsm->lsm_oinfo[i].loi_ost_gen;
1205                         }
1206                 }
1207                 /* bug 2844 - limit i_blksize for broken user-space apps */
1208                 LASSERTF(lsm->lsm_xfersize != 0, "%lu\n", lsm->lsm_xfersize);
1209                 inode->i_blksize = min(lsm->lsm_xfersize, LL_MAX_BLKSIZE);
1210                 if (lli->lli_smd != lsm)
1211                         obd_free_memmd(ll_i2obdexp(inode), &lsm);
1212         }
1213
1214         if (mea != NULL) {
1215                 if (lli->lli_mea == NULL) {
1216                         lli->lli_mea = mea;
1217                 } else {
1218                         if (memcmp(lli->lli_mea, mea, body->eadatasize)) {
1219                                 CERROR("mea mismatch for inode %lu\n",
1220                                         inode->i_ino);
1221                                 LBUG();
1222                         }
1223                 }
1224                 if (lli->lli_mea != mea)
1225                         obd_free_memmd(ll_i2lmvexp(inode),
1226                                        (struct lov_stripe_md **) &mea);
1227         }
1228
1229         /*
1230          * updating inode lustre id. We try optimize things a little bit here,
1231          * thus do not do it all the time.
1232          */
1233         if (body->valid & OBD_MD_FID) {
1234                 LASSERT(id_fid(&body->id1) != 0);
1235                 id_assign_fid(&lli->lli_id, &body->id1);
1236         }
1237         
1238         if ((body->valid & OBD_MD_FLID) || (body->valid & OBD_MD_FLGENER))
1239                 id_assign_stc(&lli->lli_id, &body->id1);
1240
1241         if (body->valid & OBD_MD_FLID)
1242                 inode->i_ino = id_ino(&body->id1);
1243         if (body->valid & OBD_MD_FLGENER)
1244                 inode->i_generation = id_gen(&body->id1);
1245         if (body->valid & OBD_MD_FLATIME)
1246                 LTIME_S(inode->i_atime) = body->atime;
1247         if (body->valid & OBD_MD_FLMTIME &&
1248             body->mtime > LTIME_S(inode->i_mtime)) {
1249                 CDEBUG(D_INODE, "setting ino %lu mtime from %lu to %u\n",
1250                        inode->i_ino, LTIME_S(inode->i_mtime), body->mtime);
1251                 LTIME_S(inode->i_mtime) = body->mtime;
1252         }
1253         if (body->valid & OBD_MD_FLCTIME &&
1254             body->ctime > LTIME_S(inode->i_ctime))
1255                 LTIME_S(inode->i_ctime) = body->ctime;
1256         if (body->valid & OBD_MD_FLMODE) {
1257                 inode->i_mode = (inode->i_mode & S_IFMT) |
1258                         (body->mode & ~S_IFMT);
1259         }
1260         if (body->valid & OBD_MD_FLTYPE) {
1261                 inode->i_mode = (inode->i_mode & ~S_IFMT) |
1262                         (body->mode & S_IFMT);
1263         }
1264         if (body->valid & OBD_MD_FLUID)
1265                 inode->i_uid = body->uid;
1266         if (body->valid & OBD_MD_FLGID)
1267                 inode->i_gid = body->gid;
1268         if (body->valid & OBD_MD_FLFLAGS)
1269                 inode->i_flags = body->flags;
1270         if (body->valid & OBD_MD_FLNLINK)
1271                 inode->i_nlink = body->nlink;
1272         if (body->valid & OBD_MD_FLRDEV)
1273 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1274                 inode->i_rdev = body->rdev;
1275 #else
1276                 inode->i_rdev = old_decode_dev(body->rdev);
1277 #endif
1278         if (body->valid & OBD_MD_FLSIZE)
1279                 inode->i_size = body->size;
1280         if (body->valid & OBD_MD_FLBLOCKS)
1281                 inode->i_blocks = body->blocks;
1282
1283         if (body->valid & OBD_MD_FLSIZE)
1284                 set_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
1285
1286 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1287         inode->i_dev = (kdev_t)id_group(&lli->lli_id);
1288 #endif
1289         LASSERT(id_fid(&lli->lli_id) != 0);
1290         
1291         LASSERT(!sbi->ll_lmv_desc.ld_tgt_count ||
1292                 id_group(&lli->lli_id) < sbi->ll_lmv_desc.ld_tgt_count);
1293 }
1294
1295 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
1296 static struct backing_dev_info ll_backing_dev_info = {
1297         .ra_pages       = 0,    /* No readahead */
1298         .memory_backed  = 0,    /* Does contribute to dirty memory */
1299 };
1300 #endif
1301
1302 void ll_read_inode2(struct inode *inode, void *opaque)
1303 {
1304         struct lustre_md *md = opaque;
1305         struct ll_inode_info *lli = ll_i2info(inode);
1306         ENTRY;
1307
1308         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1309                inode->i_generation, inode);
1310
1311         ll_lli_init(lli);
1312
1313         LASSERT(!lli->lli_smd);
1314
1315         /* Core attributes from the MDS first.  This is a new inode, and
1316          * the VFS doesn't zero times in the core inode so we have to do
1317          * it ourselves.  They will be overwritten by either MDS or OST
1318          * attributes - we just need to make sure they aren't newer. */
1319         LTIME_S(inode->i_mtime) = 0;
1320         LTIME_S(inode->i_atime) = 0;
1321         LTIME_S(inode->i_ctime) = 0;
1322
1323         inode->i_rdev = 0;
1324         ll_update_inode(inode, md);
1325
1326         /* OIDEBUG(inode); */
1327
1328         if (S_ISREG(inode->i_mode)) {
1329                 inode->i_op = &ll_file_inode_operations;
1330                 inode->i_fop = &ll_file_operations;
1331                 inode->i_mapping->a_ops = &ll_aops;
1332                 EXIT;
1333         } else if (S_ISDIR(inode->i_mode)) {
1334                 inode->i_op = &ll_dir_inode_operations;
1335                 inode->i_fop = &ll_dir_operations;
1336                 inode->i_mapping->a_ops = &ll_dir_aops;
1337                 EXIT;
1338         } else if (S_ISLNK(inode->i_mode)) {
1339                 inode->i_op = &ll_fast_symlink_inode_operations;
1340                 EXIT;
1341         } else {
1342                 inode->i_op = &ll_special_inode_operations;
1343
1344 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1345                 init_special_inode(inode, inode->i_mode,
1346                                    kdev_t_to_nr(inode->i_rdev));
1347
1348                 /* initializing backing dev info. */
1349                 inode->i_mapping->backing_dev_info = &ll_backing_dev_info;
1350 #else
1351                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1352 #endif
1353                 lli->ll_save_ifop = inode->i_fop;
1354
1355                 if (S_ISCHR(inode->i_mode))
1356                         inode->i_fop = &ll_special_chr_inode_fops;
1357                 else if (S_ISBLK(inode->i_mode))
1358                         inode->i_fop = &ll_special_blk_inode_fops;
1359                 else if (S_ISFIFO(inode->i_mode))
1360                         inode->i_fop = &ll_special_fifo_inode_fops;
1361                 else if (S_ISSOCK(inode->i_mode))
1362                         inode->i_fop = &ll_special_sock_inode_fops;
1363
1364                 CWARN("saved %p, replaced with %p\n", lli->ll_save_ifop,
1365                       inode->i_fop);
1366
1367                 if (lli->ll_save_ifop->owner) {
1368                         CWARN("%p has owner %p\n", lli->ll_save_ifop,
1369                               lli->ll_save_ifop->owner);
1370                 }
1371                 EXIT;
1372         }
1373 }
1374
1375 void ll_delete_inode(struct inode *inode)
1376 {
1377         int rc;
1378         struct lustre_id id;
1379         struct ll_sb_info *sbi = ll_i2sbi(inode);
1380         ENTRY;
1381         
1382         ll_inode2id(&id, inode);
1383
1384         rc = md_delete_object(sbi->ll_lmv_exp, &id);
1385         if (rc) {
1386                 CERROR("md_delete_object() failed, error %d.\n",
1387                        rc);
1388         }
1389
1390         clear_inode(inode);
1391         EXIT;
1392 }
1393
1394 int ll_iocontrol(struct inode *inode, struct file *file,
1395                  unsigned int cmd, unsigned long arg)
1396 {
1397         struct ll_sb_info *sbi = ll_i2sbi(inode);
1398         struct ptlrpc_request *req = NULL;
1399         int rc, flags = 0;
1400         ENTRY;
1401
1402         switch(cmd) {
1403         case EXT3_IOC_GETFLAGS: {
1404                 struct lustre_id id;
1405                 unsigned long valid = OBD_MD_FLFLAGS;
1406                 struct mds_body *body;
1407
1408                 ll_inode2id(&id, inode);
1409                 rc = md_getattr(sbi->ll_lmv_exp, &id, valid, 0, &req);
1410                 if (rc) {
1411                         CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1412                         RETURN(-abs(rc));
1413                 }
1414
1415                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1416
1417                 if (body->flags & S_APPEND)
1418                         flags |= EXT3_APPEND_FL;
1419                 if (body->flags & S_IMMUTABLE)
1420                         flags |= EXT3_IMMUTABLE_FL;
1421                 if (body->flags & S_NOATIME)
1422                         flags |= EXT3_NOATIME_FL;
1423
1424                 ptlrpc_req_finished (req);
1425
1426                 RETURN(put_user(flags, (int *)arg));
1427         }
1428         case EXT3_IOC_SETFLAGS: {
1429                 struct mdc_op_data op_data;
1430                 struct iattr attr;
1431                 struct obdo *oa;
1432                 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1433
1434                 if (get_user(flags, (int *)arg))
1435                         RETURN(-EFAULT);
1436
1437                 oa = obdo_alloc();
1438                 if (!oa)
1439                         RETURN(-ENOMEM);
1440
1441                 ll_prepare_mdc_data(&op_data, inode, NULL, NULL, 0, 0);
1442
1443                 memset(&attr, 0x0, sizeof(attr));
1444                 attr.ia_attr_flags = flags;
1445                 attr.ia_valid |= ATTR_ATTR_FLAG;
1446
1447                 rc = md_setattr(sbi->ll_lmv_exp, &op_data,
1448                                 &attr, NULL, 0, NULL, 0, &req);
1449                 if (rc) {
1450                         ptlrpc_req_finished(req);
1451                         if (rc != -EPERM && rc != -EACCES)
1452                                 CERROR("md_setattr fails: rc = %d\n", rc);
1453                         obdo_free(oa);
1454                         RETURN(rc);
1455                 }
1456                 ptlrpc_req_finished(req);
1457
1458                 oa->o_id = lsm->lsm_object_id;
1459                 oa->o_gr = lsm->lsm_object_gr;
1460                 oa->o_flags = flags;
1461                 oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
1462
1463                 rc = obd_setattr(sbi->ll_lov_exp, oa, lsm, NULL);
1464                 obdo_free(oa);
1465                 if (rc) {
1466                         if (rc != -EPERM && rc != -EACCES)
1467                                 CERROR("md_setattr fails: rc = %d\n", rc);
1468                         RETURN(rc);
1469                 }
1470
1471                 if (flags & EXT3_APPEND_FL)
1472                         inode->i_flags |= S_APPEND;
1473                 else
1474                         inode->i_flags &= ~S_APPEND;
1475                 if (flags & EXT3_IMMUTABLE_FL)
1476                         inode->i_flags |= S_IMMUTABLE;
1477                 else
1478                         inode->i_flags &= ~S_IMMUTABLE;
1479                 if (flags & EXT3_NOATIME_FL)
1480                         inode->i_flags |= S_NOATIME;
1481                 else
1482                         inode->i_flags &= ~S_NOATIME;
1483
1484                 RETURN(0);
1485         }
1486         default:
1487                 RETURN(-ENOSYS);
1488         }
1489
1490         RETURN(0);
1491 }
1492
1493 /* this is only called in the case of forced umount. */
1494 void ll_umount_begin(struct super_block *sb)
1495 {
1496         struct ll_sb_info *sbi = ll_s2sbi(sb);
1497         struct obd_device *obd;
1498         struct obd_ioctl_data ioc_data = { 0 };
1499         ENTRY;
1500      
1501         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p count %d active %d\n", sb,
1502                sb->s_count, atomic_read(&sb->s_active));
1503         
1504         obd = class_exp2obd(sbi->ll_lmv_exp);
1505         if (obd == NULL) {
1506                 CERROR("Invalid MDC connection handle "LPX64"\n",
1507                        sbi->ll_lmv_exp->exp_handle.h_cookie);
1508                 EXIT;
1509                 return;
1510         }
1511         obd->obd_no_recov = 1;
1512         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_lmv_exp,
1513                       sizeof(ioc_data), &ioc_data, NULL);
1514
1515         obd = class_exp2obd(sbi->ll_lov_exp);
1516         if (obd == NULL) {
1517                 CERROR("Invalid LOV connection handle "LPX64"\n",
1518                        sbi->ll_lov_exp->exp_handle.h_cookie);
1519                 EXIT;
1520                 return;
1521         }
1522
1523         obd->obd_no_recov = 1;
1524         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_lov_exp,
1525                       sizeof(ioc_data), &ioc_data, NULL);
1526
1527         /*
1528          * really, we'd like to wait until there are no requests outstanding,
1529          * and then continue.  For now, we just invalidate the requests,
1530          * schedule, and hope.
1531          */
1532         schedule();
1533
1534         EXIT;
1535 }
1536
1537 int ll_prep_inode(struct obd_export *lov_exp, struct obd_export *lmv_exp,
1538                   struct inode **inode, struct ptlrpc_request *req,
1539                   int offset, struct super_block *sb)
1540 {
1541         struct lustre_md md;
1542         int rc = 0;
1543
1544         rc = mdc_req2lustre_md(lmv_exp, req, offset, lov_exp, &md);
1545         if (rc)
1546                 RETURN(rc);
1547
1548         if (*inode) {
1549                 ll_update_inode(*inode, &md);
1550         } else {
1551                 LASSERT(sb);
1552                 *inode = ll_iget(sb, id_ino(&md.body->id1), &md);
1553                 if (*inode == NULL || is_bad_inode(*inode)) {
1554                         /* free the lsm if we allocated one above */
1555                         if (md.lsm != NULL)
1556                                 obd_free_memmd(lov_exp, &md.lsm);
1557                         if (md.mea != NULL)
1558                                 obd_free_memmd(lmv_exp,
1559                                                (struct lov_stripe_md**)&md.mea);
1560                         rc = -ENOMEM;
1561                         CERROR("new_inode -fatal: rc %d\n", rc);
1562                 }
1563         }
1564
1565         RETURN(rc);
1566 }
1567
1568 int ll_get_fid(struct obd_export *exp, struct lustre_id *idp,
1569                char *filename, struct lustre_id *ret)
1570 {
1571         struct ptlrpc_request *request = NULL;
1572         struct mds_body *body;
1573         int valid = 0;
1574         int rc;
1575
1576         valid |= OBD_MD_FID;
1577         
1578         rc = md_getattr_name(exp, idp, filename, strlen(filename) + 1,
1579                              valid, 0, &request);
1580         if (rc < 0) {
1581                 CDEBUG(D_INFO, "md_getattr_name failed on %s: rc %d\n",
1582                        filename, rc);
1583                 return rc;
1584         }
1585
1586         body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
1587         LASSERT(body != NULL);
1588         LASSERT_REPSWABBED(request, 0);
1589
1590         *ret = body->id1;
1591         ptlrpc_req_finished(request);
1592
1593         return rc;
1594 }