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