Whamcloud - gitweb
cf515f402c55227c0c54e31042c41ed3d02f4d94
[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 #include <linux/seq_file.h>
31
32 #include <linux/lustre_lite.h>
33 #include <linux/lustre_ha.h>
34 #include <linux/lustre_dlm.h>
35 #include <linux/lprocfs_status.h>
36 #include <linux/lustre_acl.h>
37 #include <linux/lustre_sec.h>
38 #include "llite_internal.h"
39
40 kmem_cache_t *ll_file_data_slab;
41 kmem_cache_t *ll_intent_slab;
42
43 extern struct address_space_operations ll_aops;
44 extern struct address_space_operations ll_dir_aops;
45
46 #ifndef log2
47 #define log2(n) ffz(~(n))
48 #endif
49
50 struct ll_sb_info *lustre_init_sbi(struct super_block *sb)
51 {
52         struct ll_sb_info *sbi = NULL;
53         class_uuid_t uuid;
54         ENTRY;
55
56         OBD_ALLOC(sbi, sizeof(*sbi));
57         if (!sbi)
58                 RETURN(NULL);
59
60         spin_lock_init(&sbi->ll_lock);
61         INIT_LIST_HEAD(&sbi->ll_pglist);
62         sbi->ll_pglist_gen = 0;
63         if (num_physpages < SBI_DEFAULT_RA_MAX / 4)
64                 sbi->ll_ra_info.ra_max_pages = num_physpages / 4;
65         else
66                 sbi->ll_ra_info.ra_max_pages = SBI_DEFAULT_RA_MAX;
67         INIT_LIST_HEAD(&sbi->ll_conn_chain);
68         INIT_HLIST_HEAD(&sbi->ll_orphan_dentry_list);
69         INIT_LIST_HEAD(&sbi->ll_mnt_list);
70         
71         sema_init(&sbi->ll_gns_sem, 1);
72         spin_lock_init(&sbi->ll_gns_lock);
73         INIT_LIST_HEAD(&sbi->ll_gns_sbi_head);
74         init_waitqueue_head(&sbi->ll_gns_waitq);
75         init_completion(&sbi->ll_gns_mount_finished);
76
77         /* this later may be reset via /proc/fs/... */
78         memcpy(sbi->ll_gns_oname, ".mntinfo", strlen(".mntinfo"));
79         sbi->ll_gns_oname[strlen(sbi->ll_gns_oname)] = '\0';
80         
81         /* this later may be reset via /proc/fs/... */
82         memcpy(sbi->ll_gns_upcall, "/usr/sbin/gns_upcall",
83                strlen("/usr/sbin/gns_upcall"));
84         sbi->ll_gns_upcall[strlen(sbi->ll_gns_upcall)] = '\0';
85
86         /* default values, may be changed via /proc/fs/... */
87         sbi->ll_gns_state = LL_GNS_IDLE;
88         sbi->ll_gns_pending_dentry = NULL;
89         atomic_set(&sbi->ll_gns_enabled, 1);
90         sbi->ll_gns_tick = GNS_TICK_TIMEOUT;
91         sbi->ll_gns_timeout = GNS_MOUNT_TIMEOUT;
92
93         sbi->ll_gns_timer.data = (unsigned long)sbi;
94         sbi->ll_gns_timer.function = ll_gns_timer_callback;
95         init_timer(&sbi->ll_gns_timer);
96
97         ll_set_sbi(sb, sbi);
98
99         generate_random_uuid(uuid);
100         class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
101         RETURN(sbi);
102 }
103
104 void lustre_free_sbi(struct super_block *sb)
105 {
106         struct ll_sb_info *sbi = ll_s2sbi(sb);
107         ENTRY;
108
109         if (sbi != NULL) {
110                 list_del(&sbi->ll_gns_sbi_head);
111                 del_timer(&sbi->ll_gns_timer);
112                 OBD_FREE(sbi, sizeof(*sbi));
113         }
114         ll_set_sbi(sb, NULL);
115         EXIT;
116 }
117
118 int lustre_init_dt_desc(struct ll_sb_info *sbi)
119 {
120         __u32 valsize;
121         int rc = 0;
122         ENTRY;
123         
124         valsize = sizeof(sbi->ll_dt_desc);
125         memset(&sbi->ll_dt_desc, 0, sizeof(sbi->ll_dt_desc));
126         rc = obd_get_info(sbi->ll_dt_exp, strlen("lovdesc") + 1,
127                           "lovdesc", &valsize, &sbi->ll_dt_desc);
128         RETURN(rc);
129 }
130
131 extern struct dentry_operations ll_d_ops;
132
133 int lustre_common_fill_super(struct super_block *sb, char *lmv, char *lov,
134                              int async,  char *mds_security,  char *oss_security,
135                              __u32 *nllu, int pag, __u64 *remote)
136 {
137         struct ll_sb_info *sbi = ll_s2sbi(sb);
138         struct ptlrpc_request *request = NULL;
139         struct lustre_handle dt_conn = {0, };
140         struct lustre_handle md_conn = {0, };
141         struct obd_connect_data *data;
142         struct inode *root = NULL;
143         struct obd_device *obd;
144         struct obd_statfs osfs;
145         struct lustre_md md;
146         unsigned long sec_flags;
147         __u32 valsize;
148         int err;
149         ENTRY;
150
151         obd = class_name2obd(lmv);
152         if (!obd) {
153                 CERROR("MDC %s: not setup or attached\n", lmv);
154                 RETURN(-EINVAL);
155         }
156         obd_set_info(obd->obd_self_export, strlen("async"), "async",
157                      sizeof(async), &async);
158
159         if ((*remote & (OBD_CONNECT_LOCAL | OBD_CONNECT_REMOTE)) ==
160             (OBD_CONNECT_LOCAL | OBD_CONNECT_REMOTE)) {
161                 CERROR("wrong remote flag "LPX64"\n", *remote);
162                 RETURN(-EINVAL);
163         }
164
165         OBD_ALLOC(data, sizeof(*data));
166         if (!data)
167                 RETURN(-ENOMEM);
168
169         data->ocd_connect_flags |= *remote & (OBD_CONNECT_LOCAL |
170                                               OBD_CONNECT_REMOTE);
171         memcpy(data->ocd_nllu, nllu, sizeof(data->ocd_nllu));
172
173         if (mds_security == NULL)
174                 mds_security = "null";
175
176         err = obd_set_info(obd->obd_self_export, strlen("sec"), "sec",
177                            strlen(mds_security), mds_security);
178         if (err) {
179                 CERROR("LMV %s: failed to set security %s, err %d\n",
180                         lmv, mds_security, err);
181                 OBD_FREE(data, sizeof(*data));
182                 RETURN(err);
183         }
184
185         if (pag) {
186                 sec_flags = PTLRPC_SEC_FL_PAG;
187                 err = obd_set_info(obd->obd_self_export,
188                                    strlen("sec_flags"), "sec_flags",
189                                    sizeof(sec_flags), &sec_flags);
190                 if (err) {
191                         OBD_FREE(data, sizeof(*data));
192                         RETURN(err);
193                 }
194         }
195
196         if (proc_lustre_fs_root) {
197                 err = lprocfs_register_mountpoint(proc_lustre_fs_root,
198                                                   sb, lov, lmv);
199                 if (err < 0)
200                         CERROR("could not register mount in /proc/lustre");
201         }
202
203         err = obd_connect(&md_conn, obd, &sbi->ll_sb_uuid, data,
204                           OBD_OPT_REAL_CLIENT);
205         if (err == -EBUSY) {
206                 CERROR("An MDS (lmv %s) is performing recovery, of which this"
207                        " client is not a part.  Please wait for recovery to "
208                        "complete, abort, or time out.\n", lmv);
209                 GOTO(out, err);
210         } else if (err) {
211                 CERROR("cannot connect to %s: rc = %d\n", lmv, err);
212                 GOTO(out, err);
213         }
214         sbi->ll_md_exp = class_conn2export(&md_conn);
215         err = obd_statfs(obd, &osfs, jiffies - HZ);
216         if (err)
217                 GOTO(out_lmv, err);
218
219         if (!osfs.os_bsize) {
220                 CERROR("Invalid block size is detected.");
221                 GOTO(out_lmv, err);
222         }
223
224         sb->s_magic = LL_SUPER_MAGIC;
225         sb->s_blocksize = osfs.os_bsize;
226         sb->s_blocksize_bits = log2(osfs.os_bsize);
227         sb->s_maxbytes = PAGE_CACHE_MAXBYTES;
228
229         /* in 2.6.x FS is not allowed to form s_dev */
230 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
231         {
232                 kdev_t devno;
233                 
234                 devno = get_uuid2int((char *)sbi->ll_md_exp->exp_obd->obd_uuid.uuid, 
235                                      strlen((char *)sbi->ll_md_exp->exp_obd->obd_uuid.uuid));
236                 
237                 sb->s_dev = devno;
238         }
239 #endif
240
241         /* after statfs, we are supposed to have connected to MDSs,
242          * so it's ok to check remote flag returned.
243          */
244         valsize = sizeof(&sbi->ll_remote);
245         err = obd_get_info(sbi->ll_md_exp, strlen("remote_flag"), "remote_flag",
246                            &valsize, &sbi->ll_remote);
247         if (err) {
248                 CERROR("fail to obtain remote flag\n");
249                 GOTO(out, err);
250         }
251
252         obd = class_name2obd(lov);
253         if (!obd) {
254                 CERROR("OSC %s: not setup or attached\n", lov);
255                 GOTO(out_lmv, err);
256         }
257         obd_set_info(obd->obd_self_export, strlen("async"), "async",
258                      sizeof(async), &async);
259
260        if (oss_security == NULL)
261                 oss_security = "null";
262
263         err = obd_set_info(obd->obd_self_export, strlen("sec"), "sec",
264                            strlen(oss_security), oss_security);
265         if (err) {
266                 CERROR("LOV %s: failed to set security %s, err %d\n",
267                         lov, oss_security, err);
268                 OBD_FREE(data, sizeof(*data));
269                 RETURN(err);
270         }
271
272         if (pag) {
273                 sec_flags = PTLRPC_SEC_FL_PAG;
274                 err = obd_set_info(obd->obd_self_export,
275                                    strlen("sec_flags"), "sec_flags",
276                                    sizeof(sec_flags), &sec_flags);
277                 if (err) {
278                         OBD_FREE(data, sizeof(*data));
279                         RETURN(err);
280                 }
281         }
282
283         err = obd_connect(&dt_conn, obd, &sbi->ll_sb_uuid, data, 0);
284         if (err == -EBUSY) {
285                 CERROR("An OST (lov %s) is performing recovery, of which this"
286                        " client is not a part.  Please wait for recovery to "
287                        "complete, abort, or time out.\n", lov);
288                 GOTO(out, err);
289         } else if (err) {
290                 CERROR("cannot connect to %s: rc = %d\n", lov, err);
291                 GOTO(out_lmv, err);
292         }
293         sbi->ll_dt_exp = class_conn2export(&dt_conn);
294
295         err = lustre_init_dt_desc(sbi);
296         if (err == 0) {
297                 int mdsize = obd_size_diskmd(sbi->ll_dt_exp, NULL);
298                 obd_init_ea_size(sbi->ll_md_exp, mdsize,
299                                  sbi->ll_dt_desc.ld_tgt_count *
300                                  sizeof(struct llog_cookie));
301         }
302         
303         err = md_getstatus(sbi->ll_md_exp, &sbi->ll_rootid);
304         if (err) {
305                 CERROR("cannot mds_connect: rc = %d\n", err);
306                 GOTO(out_lov, err);
307         }
308         CDEBUG(D_SUPER, "rootid "DLID4"\n", OLID4(&sbi->ll_rootid));
309
310         sb->s_op = &lustre_super_operations;
311
312         /* make root inode */
313         err = md_getattr(sbi->ll_md_exp, &sbi->ll_rootid,
314                          (OBD_MD_FLNOTOBD | OBD_MD_FLBLOCKS | OBD_MD_FID),
315                          NULL, NULL, 0, 0, &request);
316         if (err) {
317                 CERROR("md_getattr failed for root: rc = %d\n", err);
318                 GOTO(out_lov, err);
319         }
320
321         err = mdc_req2lustre_md(sbi->ll_md_exp, request, 0, 
322                                 sbi->ll_dt_exp, &md);
323         if (err) {
324                 CERROR("failed to understand root inode md: rc = %d\n", err);
325                 ptlrpc_req_finished(request);
326                 GOTO(out_lov, err);
327         }
328
329         LASSERT(id_ino(&sbi->ll_rootid) != 0);
330         root = ll_iget(sb, id_ino(&sbi->ll_rootid), &md);
331
332         ptlrpc_req_finished(request);
333
334         if (root == NULL || is_bad_inode(root)) {
335                 if (md.lsm != NULL)
336                     obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
337                 if (md.mea != NULL)
338                     obd_free_memmd(sbi->ll_md_exp,
339                                    (struct lov_stripe_md**)&md.mea);
340                 CERROR("lustre_lite: bad iget4 for root\n");
341                 GOTO(out_root, err = -EBADF);
342         }
343
344         err = ll_close_thread_start(&sbi->ll_lcq);
345         if (err) {
346                 CERROR("cannot start close thread: rc %d\n", err);
347                 GOTO(out_root, err);
348         }
349
350         ll_gns_add_timer(sbi);
351
352         /* making vm readahead 0 for 2.4.x. In the case of 2.6.x,
353            backing dev info assigned to inode mapping is used for
354            determining maximal readahead. */
355 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)) && \
356     !defined(KERNEL_HAS_AS_MAX_READAHEAD)
357         /* bug 2805 - set VM readahead to zero */
358         vm_max_readahead = vm_min_readahead = 0;
359 #endif
360
361         sb->s_root = d_alloc_root(root);
362         sb->s_root->d_op = &ll_d_ops;
363
364         sb->s_flags |= MS_POSIXACL;
365 #ifdef S_PDIROPS
366         CWARN("Enabling PDIROPS\n");
367         sb->s_flags |= S_PDIROPS;
368 #endif
369
370         if (data != NULL)
371                 OBD_FREE(data, sizeof(*data));
372         RETURN(err);
373 out_root:
374         if (root)
375                 iput(root);
376 out_lov:
377         obd_disconnect(sbi->ll_dt_exp, 0);
378 out_lmv:
379         obd_disconnect(sbi->ll_md_exp, 0);
380 out:
381         if (data != NULL)
382                 OBD_FREE(data, sizeof(*data));
383         lprocfs_unregister_mountpoint(sbi);
384         return err;
385 }
386
387 void lustre_common_put_super(struct super_block *sb)
388 {
389         struct ll_sb_info *sbi = ll_s2sbi(sb);
390         struct hlist_node *tmp, *next;
391         ENTRY;
392
393         ll_gns_del_timer(sbi);
394         ll_close_thread_shutdown(sbi->ll_lcq);
395
396         list_del(&sbi->ll_conn_chain);
397         obd_disconnect(sbi->ll_dt_exp, 0);
398
399         lprocfs_unregister_mountpoint(sbi);
400         if (sbi->ll_proc_root) {
401                 lprocfs_remove(sbi->ll_proc_root);
402                 sbi->ll_proc_root = NULL;
403         }
404
405         obd_disconnect(sbi->ll_md_exp, 0);
406
407         // We do this to get rid of orphaned dentries. That is not really trw.
408         hlist_for_each_safe(tmp, next, &sbi->ll_orphan_dentry_list) {
409                 struct dentry *dentry = hlist_entry(tmp, struct dentry, d_hash);
410                 CWARN("orphan dentry %.*s (%p->%p) at unmount\n",
411                       dentry->d_name.len, dentry->d_name.name, dentry, next);
412                 shrink_dcache_parent(dentry);
413         }
414         EXIT;
415 }
416
417 char *ll_read_opt(const char *opt, char *data)
418 {
419         char *value;
420         char *retval;
421         ENTRY;
422
423         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
424         if (strncmp(opt, data, strlen(opt)))
425                 RETURN(NULL);
426         if ((value = strchr(data, '=')) == NULL)
427                 RETURN(NULL);
428
429         value++;
430         OBD_ALLOC(retval, strlen(value) + 1);
431         if (!retval) {
432                 CERROR("out of memory!\n");
433                 RETURN(NULL);
434         }
435
436         memcpy(retval, value, strlen(value)+1);
437         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
438         RETURN(retval);
439 }
440
441 int ll_set_opt(const char *opt, char *data, int fl)
442 {
443         ENTRY;
444
445         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
446         if (strncmp(opt, data, strlen(opt)))
447                 RETURN(0);
448         else
449                 RETURN(fl);
450 }
451
452 void ll_options(char *options, char **lov, char **lmv, char **mds_sec,
453                 char **oss_sec, int *async, int *flags)
454 {
455         char *this_char;
456 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
457         char *opt_ptr = options;
458 #endif
459         ENTRY;
460
461         if (!options) {
462                 EXIT;
463                 return;
464         }
465
466         *async = 0;
467 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
468         for (this_char = strtok (options, ",");
469              this_char != NULL;
470              this_char = strtok (NULL, ",")) {
471 #else
472         while ((this_char = strsep (&opt_ptr, ",")) != NULL) {
473 #endif
474                 CDEBUG(D_SUPER, "this_char %s\n", this_char);
475                 if (!*lov && (*lov = ll_read_opt("osc", this_char)))
476                         continue;
477                 if (!*lmv && (*lmv = ll_read_opt("mdc", this_char)))
478                         continue;
479                 if (!strncmp(this_char, "lasync", strlen("lasync"))) {
480                         *async = 1;
481                         continue;
482                 }
483                 if (!*mds_sec && (*mds_sec = ll_read_opt("mds_sec", this_char)))
484                         continue;
485                 if (!*oss_sec && (*oss_sec = ll_read_opt("oss_sec", this_char)))
486                         continue;
487                 if (!(*flags & LL_SBI_NOLCK) &&
488                     ((*flags) = (*flags) |
489                                 ll_set_opt("nolock", this_char,
490                                            LL_SBI_NOLCK)))
491                         continue;
492         }
493         
494         EXIT;
495 }
496
497 void ll_lli_init(struct ll_inode_info *lli)
498 {
499         sema_init(&lli->lli_open_sem, 1);
500         sema_init(&lli->lli_size_sem, 1);
501         lli->lli_flags = 0;
502         lli->lli_size_pid = 0;
503         lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
504         spin_lock_init(&lli->lli_lock);
505         INIT_LIST_HEAD(&lli->lli_pending_write_llaps);
506         INIT_LIST_HEAD(&lli->lli_close_item);
507         lli->lli_inode_magic = LLI_INODE_MAGIC;
508         memset(&lli->lli_id, 0, sizeof(lli->lli_id));
509         sema_init(&lli->lli_och_sem, 1);
510         lli->lli_mds_read_och = lli->lli_mds_write_och = NULL;
511         lli->lli_mds_exec_och = NULL;
512         lli->lli_open_fd_read_count = lli->lli_open_fd_write_count = 0;
513         lli->lli_open_fd_exec_count = 0;
514 }
515
516 int ll_fill_super(struct super_block *sb, void *data, int silent)
517 {
518         struct ll_sb_info *sbi;
519         char *lov = NULL;
520         char *lmv = NULL;
521         char *mds_sec = NULL;
522         char *oss_sec = NULL;
523         int async, err;
524         __u32 nllu[2] = { NOBODY_UID, NOBODY_GID };
525         __u64 remote_flag = 0;    
526         ENTRY;
527
528         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
529
530         sbi = lustre_init_sbi(sb);
531         if (!sbi)
532                 RETURN(-ENOMEM);
533
534         sbi->ll_flags |= LL_SBI_READAHEAD;
535         ll_options(data, &lov, &lmv, &mds_sec, &oss_sec,
536                    &async, &sbi->ll_flags);
537
538         if (!lov) {
539                 CERROR("no osc\n");
540                 GOTO(out, err = -EINVAL);
541         }
542
543         if (!lmv) {
544                 CERROR("no mdc\n");
545                 GOTO(out, err = -EINVAL);
546         }
547         
548         err = lustre_common_fill_super(sb, lmv, lov, async, mds_sec, oss_sec,
549                                        nllu, 0, &remote_flag);
550         EXIT;
551 out:
552         if (err)
553                 lustre_free_sbi(sb);
554
555         if (lmv)
556                 OBD_FREE(lmv, strlen(lmv) + 1);
557         if (lov)
558                 OBD_FREE(lov, strlen(lov) + 1);
559         if (mds_sec)
560                 OBD_FREE(mds_sec, strlen(mds_sec) + 1);
561         if (oss_sec)
562                 OBD_FREE(oss_sec, strlen(oss_sec) + 1);
563
564         return err;
565 } /* ll_read_super */
566
567 static int lustre_process_log(struct lustre_mount_data *lmd, char *profile,
568                               struct config_llog_instance *cfg, int allow_recov)
569 {
570         struct lustre_cfg *lcfg = NULL;
571         struct lustre_cfg_bufs bufs;
572         struct portals_cfg pcfg;
573         char *peer = "MDS_PEER_UUID";
574         struct obd_device *obd;
575         struct lustre_handle md_conn = {0, };
576         struct obd_export *exp;
577         char *name = "mdc_dev";
578         class_uuid_t uuid;
579         struct obd_uuid lmv_uuid;
580         struct llog_ctxt *ctxt;
581         int rc = 0, err = 0;
582         ENTRY;
583
584         if (lmd_bad_magic(lmd))
585                 RETURN(-EINVAL);
586
587         generate_random_uuid(uuid);
588         class_uuid_unparse(uuid, &lmv_uuid);
589
590         if (lmd->lmd_local_nid) {
591                 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
592                 pcfg.pcfg_nal = lmd->lmd_nal;
593                 pcfg.pcfg_nid = lmd->lmd_local_nid;
594                 rc = libcfs_nal_cmd(&pcfg);
595                 if (rc < 0)
596                         GOTO(out, rc);
597         }
598
599         if (lmd->lmd_nal == SOCKNAL ||
600             lmd->lmd_nal == OPENIBNAL ||
601             lmd->lmd_nal == IIBNAL ||
602             lmd->lmd_nal == VIBNAL ||
603             lmd->lmd_nal == RANAL) {
604                 PCFG_INIT(pcfg, NAL_CMD_ADD_PEER);
605                 pcfg.pcfg_nal     = lmd->lmd_nal;
606                 pcfg.pcfg_nid     = lmd->lmd_server_nid;
607                 pcfg.pcfg_id      = lmd->lmd_server_ipaddr;
608                 pcfg.pcfg_misc    = lmd->lmd_port;
609                 rc = libcfs_nal_cmd(&pcfg);
610                 if (rc < 0)
611                         GOTO(out, rc);
612         }
613         lustre_cfg_bufs_reset(&bufs, name);
614         lustre_cfg_bufs_set_string(&bufs, 1, peer);
615
616         lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
617         lcfg->lcfg_nal = lmd->lmd_nal;
618         lcfg->lcfg_nid = lmd->lmd_server_nid;
619         LASSERT(lcfg->lcfg_nal);
620         LASSERT(lcfg->lcfg_nid);
621         err = class_process_config(lcfg);
622         lustre_cfg_free(lcfg);
623         if (err < 0)
624                 GOTO(out_del_conn, err);
625
626         lustre_cfg_bufs_reset(&bufs, name);
627         lustre_cfg_bufs_set_string(&bufs, 1, OBD_MDC_DEVICENAME);
628         lustre_cfg_bufs_set_string(&bufs, 2, (char *)lmv_uuid.uuid);
629
630         lcfg = lustre_cfg_new(LCFG_ATTACH, &bufs);
631         err = class_process_config(lcfg);
632         lustre_cfg_free(lcfg);
633         if (err < 0)
634                 GOTO(out_del_uuid, err);
635
636         lustre_cfg_bufs_reset(&bufs, name);
637         lustre_cfg_bufs_set_string(&bufs, 1, lmd->lmd_mds);
638         lustre_cfg_bufs_set_string(&bufs, 2, peer);
639
640         lcfg = lustre_cfg_new(LCFG_SETUP, &bufs);
641         err = class_process_config(lcfg);
642         lustre_cfg_free(lcfg);
643         if (err < 0)
644                 GOTO(out_detach, err);
645
646         obd = class_name2obd(name);
647         if (obd == NULL)
648                 GOTO(out_cleanup, rc = -EINVAL);
649
650         rc = obd_set_info(obd->obd_self_export, strlen("sec"), "sec",
651                           strlen(lmd->lmd_mds_security), lmd->lmd_mds_security);
652         if (rc)
653                 GOTO(out_cleanup, rc);
654
655         if (lmd->lmd_pag) {
656                 unsigned long sec_flags = PTLRPC_SEC_FL_PAG;
657                 rc = obd_set_info(obd->obd_self_export,
658                                   strlen("sec_flags"), "sec_flags",
659                                   sizeof(sec_flags), &sec_flags);
660                 if (rc)
661                         GOTO(out_cleanup, rc);
662         }
663
664         /* Disable initial recovery on this import */
665         rc = obd_set_info(obd->obd_self_export,
666                           strlen("initial_recov"), "initial_recov",
667                           sizeof(allow_recov), &allow_recov);
668         if (rc)
669                 GOTO(out_cleanup, rc);
670
671         rc = obd_connect(&md_conn, obd, &lmv_uuid, NULL, 0);
672         if (rc) {
673                 CERROR("cannot connect to %s: rc = %d\n", lmd->lmd_mds, rc);
674                 GOTO(out_cleanup, rc);
675         }
676
677         exp = class_conn2export(&md_conn);
678
679         ctxt = llog_get_context(&exp->exp_obd->obd_llogs,LLOG_CONFIG_REPL_CTXT);
680         rc = class_config_process_llog(ctxt, profile, cfg);
681         if (rc)
682                 CERROR("class_config_process_llog failed: rc = %d\n", rc);
683
684         err = obd_disconnect(exp, 0);
685         
686         EXIT;
687 out_cleanup:
688         lustre_cfg_bufs_reset(&bufs, name);
689         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
690         err = class_process_config(lcfg);
691         lustre_cfg_free(lcfg);
692         if (err < 0)
693                 GOTO(out, err);
694 out_detach:
695         lustre_cfg_bufs_reset(&bufs, name);
696         lcfg = lustre_cfg_new(LCFG_DETACH, &bufs);
697         err = class_process_config(lcfg);
698         lustre_cfg_free(lcfg);
699         if (err < 0)
700                 GOTO(out, err);
701
702 out_del_uuid:
703         lustre_cfg_bufs_reset(&bufs, name);
704         lustre_cfg_bufs_set_string(&bufs, 1, peer);
705         lcfg = lustre_cfg_new(LCFG_DEL_UUID, &bufs);
706         err = class_process_config(lcfg);
707         lustre_cfg_free(lcfg);
708
709 out_del_conn:
710         if (lmd->lmd_nal == SOCKNAL ||
711             lmd->lmd_nal == OPENIBNAL ||
712             lmd->lmd_nal == IIBNAL ||
713             lmd->lmd_nal == VIBNAL ||
714             lmd->lmd_nal == RANAL) {
715                 int err2;
716
717                 PCFG_INIT(pcfg, NAL_CMD_DEL_PEER);
718                 pcfg.pcfg_nal     = lmd->lmd_nal;
719                 pcfg.pcfg_nid     = lmd->lmd_server_nid;
720                 pcfg.pcfg_flags   = 1;          /* single_share */
721                 err2 = libcfs_nal_cmd(&pcfg);
722                 if (err2 && !err)
723                         err = err2;
724                 if (err < 0)
725                         GOTO(out, err);
726         }
727 out:
728         if (rc == 0)
729                 rc = err;
730
731         return rc;
732 }
733
734 static void lustre_manual_cleanup(struct ll_sb_info *sbi)
735 {
736         struct lustre_cfg *lcfg;
737         struct lustre_cfg_bufs bufs;
738         struct obd_device *obd;
739         int next = 0;
740
741         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) != NULL)
742         {
743                 int err;
744
745                 lustre_cfg_bufs_reset(&bufs, obd->obd_name);
746                 lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
747                 err = class_process_config(lcfg);
748                 if (err) {
749                         CERROR("cleanup failed: %s\n", obd->obd_name);
750                         //continue;
751                 }
752                 
753                 lcfg->lcfg_command = LCFG_DETACH;
754                 err = class_process_config(lcfg);
755                 lustre_cfg_free(lcfg);
756                 if (err) {
757                         CERROR("detach failed: %s\n", obd->obd_name);
758                         //continue;
759                 }
760         }
761
762         if (sbi->ll_lmd != NULL)
763                 class_del_profile(sbi->ll_lmd->lmd_profile);
764 }
765
766 int lustre_fill_super(struct super_block *sb, void *data, int silent)
767 {
768         struct lustre_mount_data * lmd = data;
769         char *lov = NULL, *lmv = NULL;
770         struct ll_sb_info *sbi;
771         int err;
772         ENTRY;
773
774         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
775         if (lmd_bad_magic(lmd))
776                 RETURN(-EINVAL);
777
778         sbi = lustre_init_sbi(sb);
779         if (!sbi)
780                 RETURN(-ENOMEM);
781
782         sbi->ll_flags |= LL_SBI_READAHEAD;
783
784         if (lmd->lmd_profile) {
785                 struct lustre_profile *lprof;
786                 struct config_llog_instance cfg;
787                 int len;
788
789                 if (lmd->lmd_mds[0] == '\0') {
790                         CERROR("no mds name\n");
791                         GOTO(out_free, err = -EINVAL);
792                 }
793                 lmd->lmd_mds_security[sizeof(lmd->lmd_mds_security) - 1] = 0;
794                 lmd->lmd_oss_security[sizeof(lmd->lmd_oss_security) - 1] = 0;
795
796                 OBD_ALLOC(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
797                 if (sbi->ll_lmd == NULL)
798                         GOTO(out_free, err = -ENOMEM);
799                 memcpy(sbi->ll_lmd, lmd, sizeof(*lmd));
800
801                 /* generate a string unique to this super, let's try
802                  the address of the super itself.*/
803                 len = (sizeof(sb) * 2) + 1;
804                 OBD_ALLOC(sbi->ll_instance, len);
805                 if (sbi->ll_instance == NULL)
806                         GOTO(out_free, err = -ENOMEM);
807                 sprintf(sbi->ll_instance, "%p", sb);
808
809                 cfg.cfg_instance = sbi->ll_instance;
810                 cfg.cfg_uuid = sbi->ll_sb_uuid;
811                 cfg.cfg_local_nid = lmd->lmd_local_nid;
812                 err = lustre_process_log(lmd, lmd->lmd_profile, &cfg, 0);
813                 if (err < 0) {
814                         CERROR("Unable to process log: %s\n", lmd->lmd_profile);
815                         GOTO(out_free, err);
816                 }
817
818                 lprof = class_get_profile(lmd->lmd_profile);
819                 if (lprof == NULL) {
820                         CERROR("No profile found: %s\n", lmd->lmd_profile);
821                         GOTO(out_free, err = -EINVAL);
822                 }
823                 if (lov)
824                         OBD_FREE(lov, strlen(lov) + 1);
825                 OBD_ALLOC(lov, strlen(lprof->lp_lov) +
826                           strlen(sbi->ll_instance) + 2);
827                 sprintf(lov, "%s-%s", lprof->lp_lov, sbi->ll_instance);
828
829                 if (lmv)
830                         OBD_FREE(lmv, strlen(lmv) + 1);
831                 OBD_ALLOC(lmv, strlen(lprof->lp_lmv) +
832                           strlen(sbi->ll_instance) + 2);
833                 sprintf(lmv, "%s-%s", lprof->lp_lmv, sbi->ll_instance);
834         }
835
836         if (!lov) {
837                 CERROR("no osc\n");
838                 GOTO(out_free, err = -EINVAL);
839         }
840
841         if (!lmv) {
842                 CERROR("no mdc\n");
843                 GOTO(out_free, err = -EINVAL);
844         }
845
846         err = lustre_common_fill_super(sb, lmv, lov, lmd->lmd_async,
847                                        lmd->lmd_mds_security,
848                                        lmd->lmd_oss_security,
849                                        &lmd->lmd_nllu, lmd->lmd_pag,
850                                        &lmd->lmd_remote_flag);
851
852         if (err)
853                 GOTO(out_free, err);
854         
855 out_dev:
856         if (lmv)
857                 OBD_FREE(lmv, strlen(lmv) + 1);
858         if (lov)
859                 OBD_FREE(lov, strlen(lov) + 1);
860
861         RETURN(err);
862
863 out_free:
864         if (sbi->ll_lmd) {
865                 int len = strlen(sbi->ll_lmd->lmd_profile) + sizeof("-clean")+1;
866                 int err;
867
868                 if (sbi->ll_instance != NULL) {
869                         struct lustre_mount_data *lmd = sbi->ll_lmd;
870                         struct config_llog_instance cfg;
871                         char *cl_prof;
872
873                         cfg.cfg_instance = sbi->ll_instance;
874                         cfg.cfg_uuid = sbi->ll_sb_uuid;
875
876                         OBD_ALLOC(cl_prof, len);
877                         sprintf(cl_prof, "%s-clean", lmd->lmd_profile);
878                         err = lustre_process_log(lmd, cl_prof, &cfg, 0);
879                         if (err < 0) {
880                                 CERROR("Unable to process log: %s\n", cl_prof);
881                                 lustre_manual_cleanup(sbi);
882                         }
883                         OBD_FREE(cl_prof, len);
884                         OBD_FREE(sbi->ll_instance, strlen(sbi->ll_instance) + 1);
885                 }
886                 OBD_FREE(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
887         }
888         lustre_free_sbi(sb);
889         goto out_dev;
890 } /* lustre_fill_super */
891
892 void lustre_put_super(struct super_block *sb)
893 {
894         struct obd_device *obd;
895         struct ll_sb_info *sbi = ll_s2sbi(sb);
896         int force_umount = 0;
897         ENTRY;
898
899         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
900         obd = class_exp2obd(sbi->ll_md_exp);
901         if (obd)
902                 force_umount = obd->obd_no_recov;
903         obd = NULL;
904
905         lustre_common_put_super(sb);
906         if (sbi->ll_lmd != NULL) {
907                 char *cl_prof;
908                 int len = strlen(sbi->ll_lmd->lmd_profile) + sizeof("-clean")+1;
909                 int err;
910                 struct config_llog_instance cfg;
911
912                 if (force_umount) {
913                         CERROR("force umount, doing manual cleanup\n");
914                         lustre_manual_cleanup(sbi);
915                         GOTO(free_lmd, 0);
916                 }
917
918                 cfg.cfg_instance = sbi->ll_instance;
919                 cfg.cfg_uuid = sbi->ll_sb_uuid;
920
921                 OBD_ALLOC(cl_prof, len);
922                 sprintf(cl_prof, "%s-clean", sbi->ll_lmd->lmd_profile);
923                 err = lustre_process_log(sbi->ll_lmd, cl_prof, &cfg, 0);
924                 if (err < 0) {
925                         CERROR("Unable to process log: %s, doing manual cleanup"
926                                "\n", cl_prof);
927                         lustre_manual_cleanup(sbi);
928                 }
929
930                 OBD_FREE(cl_prof, len);
931         free_lmd:
932                 OBD_FREE(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
933                 OBD_FREE(sbi->ll_instance, strlen(sbi->ll_instance) + 1);
934         }
935
936         lustre_free_sbi(sb);
937
938         EXIT;
939 } /* lustre_put_super */
940
941 int ll_process_config_update(struct ll_sb_info *sbi, int clean)
942 {
943         struct lustre_mount_data *lmd = sbi->ll_lmd;
944         char *profile = lmd->lmd_profile, *name = NULL;
945         struct config_llog_instance cfg;
946         int rc, namelen =  0, version;
947         struct llog_ctxt *ctxt;
948         ENTRY;
949
950         if (profile == NULL)
951                 RETURN(0);
952         if (lmd == NULL) {
953                 CERROR("Client not mounted with zero-conf; cannot "
954                        "process update log.\n");
955                 RETURN(0);
956         }
957
958         rc = obd_cancel_unused(sbi->ll_md_exp, NULL,
959                                LDLM_FL_CONFIG_CHANGE, NULL);
960         if (rc != 0)
961                 CWARN("obd_cancel_unused(mdc): %d\n", rc);
962
963         rc = obd_cancel_unused(sbi->ll_dt_exp, NULL,
964                                LDLM_FL_CONFIG_CHANGE, NULL);
965         if (rc != 0)
966                 CWARN("obd_cancel_unused(lov): %d\n", rc);
967
968         cfg.cfg_instance = sbi->ll_instance;
969         cfg.cfg_uuid = sbi->ll_sb_uuid;
970         cfg.cfg_local_nid = lmd->lmd_local_nid;
971
972         namelen = strlen(profile) + 20; /* -clean-######### */
973         OBD_ALLOC(name, namelen);
974         if (name == NULL)
975                 RETURN(-ENOMEM);
976
977         if (clean) {
978                 version = sbi->ll_config_version - 1;
979                 sprintf(name, "%s-clean-%d", profile, version);
980         } else {
981                 version = sbi->ll_config_version + 1;
982                 sprintf(name, "%s-%d", profile, version);
983         }
984
985         CWARN("Applying configuration log %s\n", name);
986
987         ctxt = llog_get_context(&sbi->ll_md_exp->exp_obd->obd_llogs,
988                                 LLOG_CONFIG_REPL_CTXT);
989         rc = class_config_process_llog(ctxt, name, &cfg);
990         if (rc == 0)
991                 sbi->ll_config_version = version;
992         CWARN("Finished applying configuration log %s: %d\n", name, rc);
993
994         if (rc == 0 && clean == 0) {
995                 struct lov_desc desc;
996                 __u32 valsize;
997                 int rc = 0;
998                 
999                 valsize = sizeof(desc);
1000                 rc = obd_get_info(sbi->ll_dt_exp, strlen("lovdesc") + 1,
1001                                   "lovdesc", &valsize, &desc);
1002
1003                 rc = obd_init_ea_size(sbi->ll_md_exp,
1004                                       obd_size_diskmd(sbi->ll_dt_exp, NULL),
1005                                       (desc.ld_tgt_count *
1006                                        sizeof(struct llog_cookie)));
1007         }
1008         OBD_FREE(name, namelen);
1009         RETURN(rc);
1010 }
1011
1012 struct inode *ll_inode_from_lock(struct ldlm_lock *lock)
1013 {
1014         struct inode *inode = NULL;
1015
1016         /* NOTE: we depend on atomic igrab() -bzzz */
1017         lock_res_and_lock(lock);
1018         if (lock->l_ast_data) {
1019                 struct ll_inode_info *lli = ll_i2info(lock->l_ast_data);
1020                 if (lli->lli_inode_magic == LLI_INODE_MAGIC) {
1021                         inode = igrab(lock->l_ast_data);
1022                 } else {
1023                         struct timeval now;
1024                         do_gettimeofday(&now);
1025                         inode = lock->l_ast_data;
1026                         LDLM_ERROR(lock, "granted at %lu.%lu, now %lu.%lu",
1027                                    lock->l_enqueued_time.tv_sec,
1028                                    lock->l_enqueued_time.tv_usec,
1029                                    now.tv_sec, now.tv_usec);
1030                         CDEBUG(inode->i_state & I_FREEING ? D_INFO : D_WARNING,
1031                                "l_ast_data %p is bogus: magic %0x8\n",
1032                                lock->l_ast_data, lli->lli_inode_magic);
1033                         CDEBUG(D_ERROR, "i_state = 0x%lx, l_ast_data %p is bogus: magic %0x8\n",
1034                                inode->i_state, lock->l_ast_data, lli->lli_inode_magic);
1035                         inode = NULL;
1036                         unlock_res_and_lock(lock);
1037                         LBUG();
1038                 }
1039         }
1040         unlock_res_and_lock(lock);
1041         return inode;
1042 }
1043
1044 int null_if_equal(struct ldlm_lock *lock, void *data)
1045 {
1046         if (data == lock->l_ast_data) {
1047                 lock->l_ast_data = NULL;
1048
1049                 if (lock->l_req_mode != lock->l_granted_mode)
1050                         LDLM_ERROR(lock,"clearing inode with ungranted lock\n");
1051         }
1052
1053         return LDLM_ITER_CONTINUE;
1054 }
1055
1056 static void remote_acl_free(struct remote_acl *racl);
1057
1058 void ll_clear_inode(struct inode *inode)
1059 {
1060         struct lustre_id id;
1061         struct ll_inode_info *lli = ll_i2info(inode);
1062         struct ll_sb_info *sbi = ll_i2sbi(inode);
1063         ENTRY;
1064
1065         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1066                inode->i_generation, inode);
1067
1068         ll_inode2id(&id, inode);
1069         
1070         clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &(ll_i2info(inode)->lli_flags));
1071         md_change_cbdata(sbi->ll_md_exp, &id, null_if_equal, inode);
1072
1073         LASSERT(!lli->lli_open_fd_write_count);
1074         LASSERT(!lli->lli_open_fd_read_count);
1075         LASSERT(!lli->lli_open_fd_exec_count);
1076         if (lli->lli_mds_write_och)
1077                 ll_md_real_close(sbi->ll_md_exp, inode, FMODE_WRITE);
1078         if (lli->lli_mds_exec_och)
1079                 ll_md_real_close(sbi->ll_md_exp, inode, FMODE_EXEC);
1080         if (lli->lli_mds_read_och)
1081                 ll_md_real_close(sbi->ll_md_exp, inode, FMODE_READ);
1082         if (lli->lli_smd)
1083                 obd_change_cbdata(sbi->ll_dt_exp, lli->lli_smd,
1084                                   null_if_equal, inode);
1085
1086         if (lli->lli_smd) {
1087                 obd_free_memmd(sbi->ll_dt_exp, &lli->lli_smd);
1088                 lli->lli_smd = NULL;
1089         }
1090
1091         if (lli->lli_mea) {
1092                 obd_free_memmd(sbi->ll_md_exp,
1093                                (struct lov_stripe_md **) &lli->lli_mea);
1094                 lli->lli_mea = NULL;
1095         }
1096
1097         if (lli->lli_symlink_name) {
1098                 OBD_FREE(lli->lli_symlink_name,
1099                          strlen(lli->lli_symlink_name) + 1);
1100                 lli->lli_symlink_name = NULL;
1101         }
1102
1103         if (lli->lli_posix_acl) {
1104                 LASSERT(lli->lli_remote_acl == NULL);
1105                 posix_acl_release(lli->lli_posix_acl);
1106                 lli->lli_posix_acl = NULL;
1107         }
1108
1109         if (lli->lli_remote_acl) {
1110                 LASSERT(lli->lli_posix_acl == NULL);
1111                 remote_acl_free(lli->lli_remote_acl);
1112                 lli->lli_remote_acl = NULL;
1113         }
1114
1115         lli->lli_inode_magic = LLI_INODE_DEAD;
1116
1117         EXIT;
1118 }
1119
1120 /* If this inode has objects allocated to it (lsm != NULL), then the OST
1121  * object(s) determine the file size and mtime.  Otherwise, the MDS will
1122  * keep these values until such a time that objects are allocated for it.
1123  * We do the MDS operations first, as it is checking permissions for us.
1124  * We don't to the MDS RPC if there is nothing that we want to store there,
1125  * otherwise there is no harm in updating mtime/atime on the MDS if we are
1126  * going to do an RPC anyways.
1127  *
1128  * If we are doing a truncate, we will send the mtime and ctime updates
1129  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
1130  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
1131  * at the same time.
1132  */
1133 int ll_setattr_raw(struct inode *inode, struct iattr *attr)
1134 {
1135         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1136         struct ll_inode_info *lli = ll_i2info(inode);
1137         struct ll_sb_info *sbi = ll_i2sbi(inode);
1138         struct ptlrpc_request *request = NULL;
1139         struct mdc_op_data *op_data;
1140         int ia_valid = attr->ia_valid;
1141         int err, rc = 0;
1142         ENTRY;
1143
1144         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", inode->i_ino);
1145         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_SETATTR);
1146
1147         if (ia_valid & ATTR_SIZE) {
1148                 if (attr->ia_size > ll_file_maxbytes(inode)) {
1149                         CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
1150                                attr->ia_size, ll_file_maxbytes(inode));
1151                         RETURN(-EFBIG);
1152                 }
1153
1154                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1155         }
1156
1157         /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
1158         if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
1159                 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
1160                         RETURN(-EPERM);
1161         }
1162
1163         /* We mark all of the fields "set" so MDS/OST does not re-set them */
1164         if (attr->ia_valid & ATTR_CTIME) {
1165                 attr->ia_ctime = CURRENT_TIME;
1166                 attr->ia_valid |= ATTR_CTIME_SET;
1167         }
1168         if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
1169                 attr->ia_atime = CURRENT_TIME;
1170                 attr->ia_valid |= ATTR_ATIME_SET;
1171         }
1172         if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
1173                 attr->ia_mtime = CURRENT_TIME;
1174                 attr->ia_valid |= ATTR_MTIME_SET;
1175         }
1176
1177         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
1178                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
1179                        LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
1180                        LTIME_S(CURRENT_TIME));
1181
1182         if (lsm)
1183                 attr->ia_valid &= ~ATTR_SIZE;
1184
1185         /* If only OST attributes being set on objects, don't do MDS RPC.
1186          * In that case, we need to check permissions and update the local
1187          * inode ourselves so we can call obdo_from_inode() always. */
1188         if (ia_valid & (lsm ? ~(ATTR_SIZE | ATTR_FROM_OPEN /*| ATTR_RAW*/) : ~0)) {
1189                 struct lustre_md md;
1190
1191                 OBD_ALLOC(op_data, sizeof(*op_data));
1192                 if (op_data == NULL)
1193                         RETURN(-ENOMEM);
1194                 ll_prepare_mdc_data(op_data, inode, NULL, NULL, 0, 0);
1195
1196                 rc = md_setattr(sbi->ll_md_exp, op_data,
1197                                 attr, NULL, 0, NULL, 0, &request);
1198                 OBD_FREE(op_data, sizeof(*op_data));
1199                 if (rc) {
1200                         ptlrpc_req_finished(request);
1201                         if (rc != -EPERM && rc != -EACCES)
1202                                 CERROR("md_setattr fails: rc = %d\n", rc);
1203                         RETURN(rc);
1204                 }
1205
1206                 rc = mdc_req2lustre_md(sbi->ll_md_exp, request, 0, 
1207                                        sbi->ll_dt_exp, &md);
1208                 if (rc) {
1209                         ptlrpc_req_finished(request);
1210                         RETURN(rc);
1211                 }
1212
1213                 /* We call inode_setattr to adjust timestamps, but we first
1214                  * clear ATTR_SIZE to avoid invoking vmtruncate.
1215                  *
1216                  * NB: ATTR_SIZE will only be set at this point if the size
1217                  * resides on the MDS, ie, this file has no objects. */
1218                 attr->ia_valid &= ~ATTR_SIZE;
1219
1220                 /* 
1221                  * assigning inode_setattr() to @err to disable warning that
1222                  * function's result should be checked by by caller. error is
1223                  * impossible here, as vmtruncate() control path is disabled.
1224                  */
1225                 err = inode_setattr(inode, attr);
1226                 ll_update_inode(inode, &md);
1227                 ptlrpc_req_finished(request);
1228
1229                 if (!lsm || !S_ISREG(inode->i_mode)) {
1230                         CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
1231                         RETURN(0);
1232                 }
1233         } else {
1234                 /* The OST doesn't check permissions, but the alternative is
1235                  * a gratuitous RPC to the MDS.  We already rely on the client
1236                  * to do read/write/truncate permission checks, so is mtime OK?
1237                  */
1238                 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
1239                         /* from sys_utime() */
1240                         if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
1241                                 if (current->fsuid != inode->i_uid &&
1242                                     (rc = ll_permission(inode, MAY_WRITE, NULL)) != 0)
1243                                         RETURN(rc);
1244                         } else {
1245                                 /* from inode_change_ok() */
1246                                 if (current->fsuid != inode->i_uid &&
1247                                     !capable(CAP_FOWNER))
1248                                         RETURN(-EPERM);
1249                         }
1250                 }
1251
1252                 /* won't invoke vmtruncate, as we already cleared ATTR_SIZE */
1253                 err = inode_setattr(inode, attr);
1254                 /* 
1255                  * assigning inode_setattr() to @err to disable warning that
1256                  * function's result should be checked by by caller. error is
1257                  * impossible here, as vmtruncate() control path is disabled.
1258                  */
1259         }
1260
1261         /* We really need to get our PW lock before we change inode->i_size.
1262          * If we don't we can race with other i_size updaters on our node, like
1263          * ll_file_read.  We can also race with i_size propogation to other
1264          * nodes through dirtying and writeback of final cached pages.  This
1265          * last one is especially bad for racing o_append users on other
1266          * nodes. */
1267         if (ia_valid & ATTR_SIZE) {
1268                 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
1269                                                            OBD_OBJECT_EOF } };
1270                 struct lustre_handle lockh = { 0 };
1271                 int err, ast_flags = 0;
1272                 /* XXX when we fix the AST intents to pass the discard-range
1273                  * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
1274                  * XXX here. */
1275                 if (attr->ia_size == 0)
1276                         ast_flags = LDLM_AST_DISCARD_DATA;
1277
1278                 rc = ll_extent_lock(NULL, inode, lsm, LCK_PW, &policy, &lockh,
1279                                     ast_flags, &ll_i2sbi(inode)->ll_seek_stime);
1280
1281                 if (rc != 0)
1282                         RETURN(rc);
1283
1284                 down(&lli->lli_size_sem);
1285                 lli->lli_size_pid = current->pid;
1286                 rc = vmtruncate(inode, attr->ia_size);
1287                 if (rc != 0) {
1288                         LASSERT(atomic_read(&lli->lli_size_sem.count) <= 0);
1289                         lli->lli_size_pid = 0;
1290                         up(&lli->lli_size_sem);
1291                 }
1292
1293                 err = ll_extent_unlock(NULL, inode, lsm, LCK_PW, &lockh);
1294                 if (err) {
1295                         CERROR("ll_extent_unlock failed: %d\n", err);
1296                         if (!rc)
1297                                 rc = err;
1298                 }
1299         } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET | ATTR_UID | ATTR_GID)) {
1300                 struct obdo *oa = NULL;
1301
1302                 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
1303                        inode->i_ino, LTIME_S(attr->ia_mtime));
1304
1305                 oa = obdo_alloc();
1306                 if (oa == NULL)
1307                         RETURN(-ENOMEM);
1308
1309                 oa->o_id = lsm->lsm_object_id;
1310                 oa->o_gr = lsm->lsm_object_gr;
1311                 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1312
1313                 /* adding uid and gid, needed for quota */
1314                 if (ia_valid & ATTR_UID) {
1315                         oa->o_uid = inode->i_uid;
1316                         oa->o_valid |= OBD_MD_FLUID;
1317                 }
1318
1319                 if (ia_valid & ATTR_GID) {
1320                         oa->o_gid = inode->i_gid;
1321                         oa->o_valid |= OBD_MD_FLGID;
1322                 }
1323
1324                 *(obdo_id(oa)) = lli->lli_id;
1325                 oa->o_valid |= OBD_MD_FLIFID;
1326
1327                 obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
1328                                 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
1329                 rc = obd_setattr(sbi->ll_dt_exp, oa, lsm, NULL);
1330                 obdo_free(oa);
1331                 if (rc)
1332                         CERROR("obd_setattr fails: rc = %d\n", rc);
1333         }
1334
1335         RETURN(rc);
1336 }
1337
1338 int ll_setattr(struct dentry *de, struct iattr *attr)
1339 {
1340         LASSERT(de->d_inode);
1341         return ll_setattr_raw(de->d_inode, attr);
1342 }
1343
1344 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
1345                        unsigned long max_age)
1346 {
1347         struct ll_sb_info *sbi = ll_s2sbi(sb);
1348         struct obd_statfs obd_osfs;
1349         int rc;
1350         ENTRY;
1351
1352         rc = obd_statfs(class_exp2obd(sbi->ll_md_exp), osfs, max_age);
1353         if (rc) {
1354                 CERROR("obd_statfs fails: rc = %d\n", rc);
1355                 RETURN(rc);
1356         }
1357
1358         osfs->os_type = sb->s_magic;
1359
1360         CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1361                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1362
1363         rc = obd_statfs(class_exp2obd(sbi->ll_dt_exp), &obd_osfs, max_age);
1364         if (rc) {
1365                 CERROR("obd_statfs fails: rc = %d\n", rc);
1366                 RETURN(rc);
1367         }
1368
1369         CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1370                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1371                obd_osfs.os_files);
1372
1373         osfs->os_blocks = obd_osfs.os_blocks;
1374         osfs->os_bfree = obd_osfs.os_bfree;
1375         osfs->os_bavail = obd_osfs.os_bavail;
1376
1377         /* If we don't have as many objects free on the OST as inodes
1378          * on the MDS, we reduce the total number of inodes to
1379          * compensate, so that the "inodes in use" number is correct.
1380          */
1381         if (obd_osfs.os_ffree < osfs->os_ffree) {
1382                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1383                         obd_osfs.os_ffree;
1384                 osfs->os_ffree = obd_osfs.os_ffree;
1385         }
1386
1387         RETURN(rc);
1388 }
1389
1390 int ll_statfs(struct super_block *sb, struct kstatfs *sfs)
1391 {
1392         struct obd_statfs osfs;
1393         int rc;
1394
1395         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p\n", sb);
1396         lprocfs_counter_incr(ll_s2sbi(sb)->ll_stats, LPROC_LL_STAFS);
1397
1398         /* For now we will always get up-to-date statfs values, but in the
1399          * future we may allow some amount of caching on the client (e.g.
1400          * from QOS or lprocfs updates). */
1401         rc = ll_statfs_internal(sb, &osfs, jiffies - 1);
1402         if (rc)
1403                 return rc;
1404
1405         statfs_unpack(sfs, &osfs);
1406
1407         if (sizeof(sfs->f_blocks) == 4) {
1408                 while (osfs.os_blocks > ~0UL) {
1409                         sfs->f_bsize <<= 1;
1410
1411                         osfs.os_blocks >>= 1;
1412                         osfs.os_bfree >>= 1;
1413                         osfs.os_bavail >>= 1;
1414                 }
1415         }
1416
1417         sfs->f_blocks = osfs.os_blocks;
1418         sfs->f_bfree = osfs.os_bfree;
1419         sfs->f_bavail = osfs.os_bavail;
1420
1421         return 0;
1422 }
1423
1424
1425 /********************************
1426  * remote acl                   *
1427  ********************************/
1428
1429 static struct remote_acl *remote_acl_alloc(void)
1430 {
1431         struct remote_acl *racl;
1432         int i;
1433
1434         OBD_ALLOC(racl, sizeof(*racl));
1435         if (!racl)
1436                 return NULL;
1437
1438         spin_lock_init(&racl->ra_lock);
1439         init_MUTEX(&racl->ra_update_sem);
1440
1441         for (i = 0; i < REMOTE_ACL_HASHSIZE; i++)
1442                 INIT_LIST_HEAD(&racl->ra_perm_cache[i]);
1443
1444         return racl;
1445 }
1446
1447 /*
1448  * caller should guarantee no race here.
1449  */
1450 static void remote_perm_flush_xperms(struct lustre_remote_perm *perm)
1451 {
1452         struct remote_perm_setxid *xperm;
1453
1454         while (!list_empty(&perm->lrp_setxid_perms)) {
1455                 xperm = list_entry(perm->lrp_setxid_perms.next,
1456                                    struct remote_perm_setxid,
1457                                    list);
1458                 list_del(&xperm->list);
1459                 OBD_FREE(xperm, sizeof(*xperm));
1460         }
1461 }
1462
1463 /*
1464  * caller should guarantee no race here.
1465  */
1466 static void remote_acl_flush(struct remote_acl *racl)
1467 {
1468         struct list_head *head;
1469         struct lustre_remote_perm *perm, *tmp;
1470         int i;
1471
1472         for (i = 0; i < REMOTE_ACL_HASHSIZE; i++) {
1473                 head = &racl->ra_perm_cache[i];
1474
1475                 list_for_each_entry_safe(perm, tmp, head, lrp_list) {
1476                         remote_perm_flush_xperms(perm);
1477                         list_del(&perm->lrp_list);
1478                         OBD_FREE(perm, sizeof(*perm));
1479                 }
1480         }
1481 }
1482
1483 static void remote_acl_free(struct remote_acl *racl)
1484 {
1485         if (!racl)
1486                 return;
1487
1488         down(&racl->ra_update_sem);
1489         spin_lock(&racl->ra_lock);
1490         remote_acl_flush(racl);
1491         spin_unlock(&racl->ra_lock);
1492         up(&racl->ra_update_sem);
1493
1494         OBD_FREE(racl, sizeof(*racl));
1495 }
1496
1497 static inline int remote_acl_hashfunc(__u32 id)
1498 {
1499         return (id & (REMOTE_ACL_HASHSIZE - 1));
1500 }
1501
1502 static
1503 int __remote_acl_check(struct remote_acl *racl, unsigned int *perm)
1504 {
1505         struct list_head *head;
1506         struct lustre_remote_perm *lperm;
1507         struct remote_perm_setxid *xperm;
1508         int found = 0, rc = -ENOENT;
1509
1510         LASSERT(racl);
1511         head = &racl->ra_perm_cache[remote_acl_hashfunc(current->uid)];
1512         spin_lock(&racl->ra_lock);
1513
1514         list_for_each_entry(lperm, head, lrp_list) {
1515                 if (lperm->lrp_auth_uid == current->uid) {
1516                         found = 1;
1517                         break;
1518                 }
1519         }
1520
1521         if (!found)
1522                 goto out;
1523
1524         if (lperm->lrp_auth_uid == current->fsuid &&
1525             lperm->lrp_auth_gid == current->fsgid) {
1526                 if (lperm->lrp_valid) {
1527                         *perm = lperm->lrp_perm;
1528                         rc = 0;
1529                 }
1530                 goto out;
1531         } else if ((!lperm->lrp_setuid &&
1532                     lperm->lrp_auth_uid != current->fsuid) ||
1533                    (!lperm->lrp_setgid &&
1534                     lperm->lrp_auth_gid != current->fsgid))  {
1535                 *perm = 0;
1536                 rc = 0;
1537                 goto out;
1538         }
1539
1540         list_for_each_entry(xperm, &lperm->lrp_setxid_perms, list) {
1541                 if (xperm->uid == current->fsuid &&
1542                     xperm->gid == current->fsgid) {
1543                         *perm = xperm->perm;
1544                         rc = 0;
1545                         goto out;
1546                 }
1547         }
1548
1549 out:
1550         spin_unlock(&racl->ra_lock);
1551         return rc;
1552 }
1553
1554 static
1555 int __remote_acl_update(struct remote_acl *racl,
1556                         struct mds_remote_perm *mperm,
1557                         struct lustre_remote_perm *lperm,
1558                         struct remote_perm_setxid *xperm)
1559 {
1560         struct list_head *head;
1561         struct lustre_remote_perm *lp;
1562         struct remote_perm_setxid *xp;
1563         int found = 0, setuid = 0, setgid = 0;
1564
1565         LASSERT(racl);
1566         LASSERT(mperm);
1567         LASSERT(lperm);
1568         LASSERT(current->uid == mperm->mrp_auth_uid);
1569
1570         if (current->fsuid != mperm->mrp_auth_uid)
1571                 setuid = 1;
1572         if (current->fsgid != mperm->mrp_auth_gid)
1573                 setgid = 1;
1574
1575         head = &racl->ra_perm_cache[remote_acl_hashfunc(current->uid)];
1576         spin_lock(&racl->ra_lock);
1577
1578         list_for_each_entry(lp, head, lrp_list) {
1579                 if (lp->lrp_auth_uid == current->uid) {
1580                         found = 1;
1581                         break;
1582                 }
1583         }
1584
1585         if (found) {
1586                 OBD_FREE(lperm, sizeof(*lperm));
1587
1588                 if (!lp->lrp_valid && !setuid && !setgid) {
1589                         lp->lrp_perm = mperm->mrp_perm;
1590                         lp->lrp_valid = 1;
1591                 }
1592
1593                 /* sanity check for changes of setxid rules */
1594                 if ((lp->lrp_setuid != 0) != (mperm->mrp_allow_setuid != 0)) {
1595                         CWARN("setuid changes: %d => %d\n",
1596                               (lp->lrp_setuid != 0),
1597                               (mperm->mrp_allow_setuid != 0));
1598                         lp->lrp_setuid = (mperm->mrp_allow_setuid != 0);
1599                 }
1600
1601                 if ((lp->lrp_setgid != 0) != (mperm->mrp_allow_setgid != 0)) {
1602                         CWARN("setgid changes: %d => %d\n",
1603                               (lp->lrp_setgid != 0),
1604                               (mperm->mrp_allow_setgid != 0));
1605                         lp->lrp_setgid = (mperm->mrp_allow_setgid != 0);
1606                 }
1607
1608                 if (!lp->lrp_setuid && !lp->lrp_setgid &&
1609                     !list_empty(&lp->lrp_setxid_perms)) {
1610                         remote_perm_flush_xperms(lp);
1611                 }
1612         } else {
1613                 /* initialize lperm and linked into hashtable
1614                  */
1615                 INIT_LIST_HEAD(&lperm->lrp_setxid_perms);
1616                 lperm->lrp_auth_uid = mperm->mrp_auth_uid;
1617                 lperm->lrp_auth_gid = mperm->mrp_auth_gid;
1618                 lperm->lrp_setuid = (mperm->mrp_allow_setuid != 0);
1619                 lperm->lrp_setgid = (mperm->mrp_allow_setgid != 0);
1620                 list_add(&lperm->lrp_list, head);
1621
1622                 if (!setuid && !setgid) {
1623                         /* in this case, i'm the authenticated user,
1624                          * and mrp_perm is for me.
1625                          */
1626                         lperm->lrp_perm = mperm->mrp_perm;
1627                         lperm->lrp_valid = 1;
1628                         spin_unlock(&racl->ra_lock);
1629
1630                         if (xperm)
1631                                 OBD_FREE(xperm, sizeof(*xperm));
1632                         return 0;
1633                 }
1634
1635                 lp = lperm;
1636                 /* fall through */
1637         }
1638
1639         LASSERT(lp->lrp_setuid || lp->lrp_setgid ||
1640                 list_empty(&lp->lrp_setxid_perms));
1641
1642         /* if no xperm supplied, we are all done here */
1643         if (!xperm) {
1644                 spin_unlock(&racl->ra_lock);
1645                 return 0;
1646         }
1647
1648         /* whether we allow setuid/setgid */
1649         if ((!lp->lrp_setuid && setuid) || (!lp->lrp_setgid && setgid)) {
1650                 OBD_FREE(xperm, sizeof(*xperm));
1651                 spin_unlock(&racl->ra_lock);
1652                 return 0;
1653         }
1654
1655         /* traverse xperm list */
1656         list_for_each_entry(xp, &lp->lrp_setxid_perms, list) {
1657                 if (xp->uid == current->fsuid &&
1658                     xp->gid == current->fsgid) {
1659                         if (xp->perm != mperm->mrp_perm) {
1660                                 /* actually this should not happen */
1661                                 CWARN("perm changed: %o => %o\n",
1662                                       xp->perm, mperm->mrp_perm);
1663                                 xp->perm = mperm->mrp_perm;
1664                         }
1665                         OBD_FREE(xperm, sizeof(*xperm));
1666                         spin_unlock(&racl->ra_lock);
1667                         return 0;
1668                 }
1669         }
1670
1671         /* finally insert this xperm */
1672         xperm->uid = current->fsuid;
1673         xperm->gid = current->fsgid;
1674         xperm->perm = mperm->mrp_perm;
1675         list_add(&xperm->list, &lp->lrp_setxid_perms);
1676
1677         spin_unlock(&racl->ra_lock);
1678         return 0;
1679 }
1680
1681 /*
1682  * remote_acl semaphore must be held by caller
1683  */
1684 static
1685 int remote_acl_update_locked(struct remote_acl *racl,
1686                              struct mds_remote_perm *mperm)
1687 {
1688         struct lustre_remote_perm *lperm;
1689         struct remote_perm_setxid *xperm;
1690         int setuid = 0, setgid = 0;
1691
1692         might_sleep();
1693
1694         if (current->uid != mperm->mrp_auth_uid) {
1695                 CERROR("current uid %u while authenticated as %u\n",
1696                        current->uid, mperm->mrp_auth_uid);
1697                 return -EINVAL;
1698         }
1699
1700         if (current->fsuid != mperm->mrp_auth_uid)
1701                 setuid = 1;
1702         if (current->fsgid == mperm->mrp_auth_gid)
1703                 setgid = 1;
1704
1705         OBD_ALLOC(lperm, sizeof(*lperm));
1706         if (!lperm)
1707                 return -ENOMEM;
1708
1709         if ((setuid || setgid) &&
1710             !(setuid && !mperm->mrp_allow_setuid) &&
1711             !(setgid && !mperm->mrp_allow_setgid)) {
1712                 OBD_ALLOC(xperm, sizeof(*xperm));
1713                 if (!xperm) {
1714                         OBD_FREE(lperm, sizeof(*lperm));
1715                         return -ENOMEM;
1716                 }
1717         } else
1718                 xperm = NULL;
1719
1720         return __remote_acl_update(racl, mperm, lperm, xperm);
1721 }
1722
1723 /*
1724  * return -EACCES at any error cases
1725  */
1726 int ll_remote_acl_permission(struct inode *inode, int mode)
1727 {
1728         struct ll_sb_info *sbi = ll_i2sbi(inode);
1729         struct remote_acl *racl = ll_i2info(inode)->lli_remote_acl;
1730         struct ptlrpc_request *req = NULL;
1731         struct lustre_id id;
1732         struct mds_remote_perm *mperm;
1733         int rc = -EACCES, perm;
1734
1735         if (!racl)
1736                 return -EACCES;
1737
1738         if (__remote_acl_check(racl, &perm) == 0) {
1739                 return ((perm & mode) == mode ? 0 : -EACCES);
1740         }
1741
1742         might_sleep();
1743
1744         /* doing update
1745          */
1746         down(&racl->ra_update_sem);
1747
1748         /* we might lose the race when obtain semaphore,
1749          * so check again.
1750          */
1751         if (__remote_acl_check(racl, &perm) == 0) {
1752                 if ((perm & mode) == mode)
1753                         rc = 0;
1754                 goto out;
1755         }
1756
1757         /* really fetch from mds
1758          */
1759         ll_inode2id(&id, inode);
1760         if (md_access_check(sbi->ll_md_exp, &id, &req))
1761                 goto out;
1762
1763         /* status non-zero indicate there's more apparent error
1764          * detected by mds, e.g. didn't allow this user at all.
1765          * we simply ignore and didn't cache it.
1766          */
1767         if (req->rq_repmsg->status)
1768                 goto out;
1769
1770         mperm = lustre_swab_repbuf(req, 1, sizeof(*mperm),
1771                                    lustre_swab_remote_perm);
1772         LASSERT(mperm);
1773         LASSERT_REPSWABBED(req, 1);
1774
1775         if ((mperm->mrp_perm & mode) == mode)
1776                 rc = 0;
1777
1778         remote_acl_update_locked(racl, mperm);
1779 out:
1780         if (req)
1781                 ptlrpc_req_finished(req);
1782
1783         up(&racl->ra_update_sem);
1784         return rc;
1785 }
1786
1787 int ll_remote_acl_update(struct inode *inode, struct mds_remote_perm *perm)
1788 {
1789         struct remote_acl *racl = ll_i2info(inode)->lli_remote_acl;
1790         int rc;
1791
1792         LASSERT(perm);
1793
1794         if (!racl)
1795                 return -EACCES;
1796
1797         down(&racl->ra_update_sem);
1798         rc = remote_acl_update_locked(racl, perm);
1799         up(&racl->ra_update_sem);
1800
1801         return rc;
1802 }
1803
1804 void ll_inode_invalidate_acl(struct inode *inode)
1805 {
1806         struct ll_sb_info *sbi = ll_i2sbi(inode);
1807         struct ll_inode_info *lli = ll_i2info(inode);
1808
1809         if (sbi->ll_remote) {
1810                 struct remote_acl *racl = lli->lli_remote_acl;
1811
1812                 LASSERT(!lli->lli_posix_acl);
1813                 if (racl) {
1814                         down(&racl->ra_update_sem);
1815                         spin_lock(&racl->ra_lock);
1816                         remote_acl_flush(lli->lli_remote_acl);
1817                         spin_unlock(&racl->ra_lock);
1818                         up(&racl->ra_update_sem);
1819                 }
1820         } else {
1821                 LASSERT(!lli->lli_remote_acl);
1822                 spin_lock(&lli->lli_lock);
1823                 posix_acl_release(lli->lli_posix_acl);
1824                 lli->lli_posix_acl = NULL;
1825                 spin_unlock(&lli->lli_lock);
1826         }
1827 }
1828
1829 void ll_update_inode(struct inode *inode, struct lustre_md *md)
1830 {
1831         struct ll_inode_info *lli = ll_i2info(inode);
1832         struct lov_stripe_md *lsm = md->lsm;
1833         struct mds_body *body = md->body;
1834         struct mea *mea = md->mea;
1835         struct posix_acl *posix_acl = md->posix_acl;
1836         struct ll_sb_info *sbi = ll_i2sbi(inode);
1837         ENTRY;
1838
1839         LASSERT((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
1840
1841         if (md->lsm && md->lsm->lsm_magic != LOV_MAGIC) {
1842                 /* check for default striping info for dir. */
1843                 LASSERT((mea != NULL) == ((body->valid & OBD_MD_FLDIREA) != 0));
1844         }
1845         
1846         if (lsm != NULL) {
1847                 LASSERT(lsm->lsm_object_gr > 0);
1848                 if (lli->lli_smd == NULL) {
1849                         lli->lli_smd = lsm;
1850                         lli->lli_maxbytes = lsm->lsm_maxbytes;
1851                         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1852                                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1853                 } else {
1854                         int i;
1855                         if (memcmp(lli->lli_smd, lsm, sizeof(*lsm))) {
1856                                 CERROR("lsm mismatch for inode %ld\n",
1857                                        inode->i_ino);
1858                                 CERROR("lli_smd:\n");
1859                                 dump_lsm(D_ERROR, lli->lli_smd);
1860                                 CERROR("lsm:\n");
1861                                 dump_lsm(D_ERROR, lsm);
1862                                 LBUG();
1863                         }
1864                         /* XXX FIXME -- We should decide on a safer (atomic) and
1865                          * more elegant way to update the lsm */
1866                         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1867                                 lli->lli_smd->lsm_oinfo[i].loi_id =
1868                                         lsm->lsm_oinfo[i].loi_id;
1869                                 lli->lli_smd->lsm_oinfo[i].loi_gr =
1870                                         lsm->lsm_oinfo[i].loi_gr;
1871                                 lli->lli_smd->lsm_oinfo[i].loi_ost_idx =
1872                                         lsm->lsm_oinfo[i].loi_ost_idx;
1873                                 lli->lli_smd->lsm_oinfo[i].loi_ost_gen =
1874                                         lsm->lsm_oinfo[i].loi_ost_gen;
1875                         }
1876                 }
1877                 /* bug 2844 - limit i_blksize for broken user-space apps */
1878                 LASSERTF(lsm->lsm_xfersize != 0, "%lu\n", lsm->lsm_xfersize);
1879                 inode->i_blksize = min(lsm->lsm_xfersize, LL_MAX_BLKSIZE);
1880                 if (lli->lli_smd != lsm)
1881                         obd_free_memmd(ll_i2dtexp(inode), &lsm);
1882         }
1883
1884         if (mea != NULL) {
1885                 if (lli->lli_mea == NULL) {
1886                         lli->lli_mea = mea;
1887                 } else {
1888                         if (memcmp(lli->lli_mea, mea, body->eadatasize)) {
1889                                 CERROR("mea mismatch for inode %lu\n",
1890                                         inode->i_ino);
1891                                 LBUG();
1892                         }
1893                 }
1894                 if (lli->lli_mea != mea)
1895                         obd_free_memmd(ll_i2mdexp(inode),
1896                                        (struct lov_stripe_md **) &mea);
1897         }
1898
1899         if (body->valid & OBD_MD_FID)
1900                 id_assign_fid(&lli->lli_id, &body->id1);
1901         
1902         if (body->valid & OBD_MD_FLID)
1903                 id_ino(&lli->lli_id) = id_ino(&body->id1);
1904
1905         if (body->valid & OBD_MD_FLGENER)
1906                 id_gen(&lli->lli_id) = id_gen(&body->id1);
1907
1908         /* local/remote ACL */
1909         if (sbi->ll_remote) {
1910                 LASSERT(md->posix_acl == NULL);
1911                 if (md->remote_perm) {
1912                         ll_remote_acl_update(inode, md->remote_perm);
1913                         OBD_FREE(md->remote_perm, sizeof(*md->remote_perm));
1914                         md->remote_perm = NULL;
1915                 }
1916         } else {
1917                 LASSERT(md->remote_perm == NULL);
1918                 spin_lock(&lli->lli_lock);
1919                 if (posix_acl != NULL) {
1920                         if (lli->lli_posix_acl != NULL)
1921                                 posix_acl_release(lli->lli_posix_acl);
1922                         lli->lli_posix_acl = posix_acl;
1923                 }
1924                 spin_unlock(&lli->lli_lock);
1925         }
1926
1927         if (body->valid & OBD_MD_FLID)
1928                 inode->i_ino = id_ino(&body->id1);
1929         if (body->valid & OBD_MD_FLGENER)
1930                 inode->i_generation = id_gen(&body->id1);
1931         if (body->valid & OBD_MD_FLATIME)
1932                 LTIME_S(inode->i_atime) = body->atime;
1933         if (body->valid & OBD_MD_FLMTIME &&
1934             body->mtime > LTIME_S(inode->i_mtime)) {
1935                 CDEBUG(D_INODE, "setting ino %lu mtime from %lu to %u\n",
1936                        inode->i_ino, LTIME_S(inode->i_mtime), body->mtime);
1937                 LTIME_S(inode->i_mtime) = body->mtime;
1938         }
1939         if (body->valid & OBD_MD_FLCTIME &&
1940             body->ctime > LTIME_S(inode->i_ctime))
1941                 LTIME_S(inode->i_ctime) = body->ctime;
1942         if (body->valid & OBD_MD_FLMODE) {
1943                 inode->i_mode = (inode->i_mode & S_IFMT) |
1944                         (body->mode & ~S_IFMT);
1945         }
1946         if (body->valid & OBD_MD_FLTYPE) {
1947                 inode->i_mode = (inode->i_mode & ~S_IFMT) |
1948                         (body->mode & S_IFMT);
1949         }
1950         if (body->valid & OBD_MD_FLUID)
1951                 inode->i_uid = body->uid;
1952         if (body->valid & OBD_MD_FLGID)
1953                 inode->i_gid = body->gid;
1954         if (body->valid & OBD_MD_FLFLAGS)
1955                 inode->i_flags = body->flags;
1956         if (body->valid & OBD_MD_FLNLINK)
1957                 inode->i_nlink = body->nlink;
1958         if (body->valid & OBD_MD_FLRDEV)
1959 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1960                 inode->i_rdev = body->rdev;
1961 #else
1962                 inode->i_rdev = old_decode_dev(body->rdev);
1963 #endif
1964         if (body->valid & OBD_MD_FLSIZE)
1965                 inode->i_size = body->size;
1966         if (body->valid & OBD_MD_FLBLOCKS)
1967                 inode->i_blocks = body->blocks;
1968
1969         if (body->valid & OBD_MD_FLSIZE)
1970                 set_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
1971
1972 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1973         inode->i_dev = (kdev_t)id_group(&lli->lli_id);
1974 #endif
1975         LASSERT(id_fid(&lli->lli_id) != 0);
1976 }
1977
1978 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
1979 static struct backing_dev_info ll_backing_dev_info = {
1980         .ra_pages       = 0,    /* No readahead */
1981         .memory_backed  = 0,    /* Does contribute to dirty memory */
1982 };
1983 #endif
1984
1985 void ll_read_inode2(struct inode *inode, void *opaque)
1986 {
1987         struct lustre_md *md = opaque;
1988         struct ll_inode_info *lli = ll_i2info(inode);
1989         ENTRY;
1990
1991         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1992                inode->i_generation, inode);
1993
1994         ll_lli_init(lli);
1995
1996         LASSERT(!lli->lli_smd);
1997
1998         if (ll_i2sbi(inode)->ll_remote) {
1999                 lli->lli_remote_acl = remote_acl_alloc();
2000                 /* if failed alloc, nobody will be able to access this inode */
2001         }
2002
2003         /* Core attributes from the MDS first.  This is a new inode, and
2004          * the VFS doesn't zero times in the core inode so we have to do
2005          * it ourselves.  They will be overwritten by either MDS or OST
2006          * attributes - we just need to make sure they aren't newer. */
2007         LTIME_S(inode->i_mtime) = 0;
2008         LTIME_S(inode->i_atime) = 0;
2009         LTIME_S(inode->i_ctime) = 0;
2010
2011         inode->i_rdev = 0;
2012         ll_update_inode(inode, md);
2013
2014         /* OIDEBUG(inode); */
2015
2016         if (S_ISREG(inode->i_mode)) {
2017                 inode->i_op = &ll_file_inode_operations;
2018                 inode->i_fop = &ll_file_operations;
2019                 inode->i_mapping->a_ops = &ll_aops;
2020                 EXIT;
2021         } else if (S_ISDIR(inode->i_mode)) {
2022                 inode->i_op = &ll_dir_inode_operations;
2023                 inode->i_fop = &ll_dir_operations;
2024                 inode->i_mapping->a_ops = &ll_dir_aops;
2025                 EXIT;
2026         } else if (S_ISLNK(inode->i_mode)) {
2027                 inode->i_op = &ll_fast_symlink_inode_operations;
2028                 EXIT;
2029         } else {
2030                 inode->i_op = &ll_special_inode_operations;
2031
2032 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2033                 init_special_inode(inode, inode->i_mode,
2034                                    kdev_t_to_nr(inode->i_rdev));
2035
2036                 /* initializing backing dev info. */
2037                 inode->i_mapping->backing_dev_info = &ll_backing_dev_info;
2038 #else
2039                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
2040 #endif
2041                 lli->ll_save_ifop = inode->i_fop;
2042
2043                 if (S_ISCHR(inode->i_mode))
2044                         inode->i_fop = &ll_special_chr_inode_fops;
2045                 else if (S_ISBLK(inode->i_mode))
2046                         inode->i_fop = &ll_special_blk_inode_fops;
2047                 else if (S_ISFIFO(inode->i_mode))
2048                         inode->i_fop = &ll_special_fifo_inode_fops;
2049                 else if (S_ISSOCK(inode->i_mode))
2050                         inode->i_fop = &ll_special_sock_inode_fops;
2051
2052                 CWARN("saved %p, replaced with %p\n", lli->ll_save_ifop,
2053                       inode->i_fop);
2054
2055                 if (lli->ll_save_ifop->owner) {
2056                         CWARN("%p has owner %p\n", lli->ll_save_ifop,
2057                               lli->ll_save_ifop->owner);
2058                 }
2059                 EXIT;
2060         }
2061 }
2062
2063 void ll_delete_inode(struct inode *inode)
2064 {
2065         struct ll_sb_info *sbi = ll_i2sbi(inode);
2066         struct lustre_id id;
2067         int rc;
2068         ENTRY;
2069
2070         ll_inode2id(&id, inode);
2071
2072         rc = md_delete_inode(sbi->ll_md_exp, &id);
2073         if (rc) {
2074                 CERROR("md_delete_inode() failed, error %d\n", 
2075                        rc);
2076         }
2077
2078         clear_inode(inode);
2079         EXIT;
2080 }
2081
2082 int ll_iocontrol(struct inode *inode, struct file *file,
2083                  unsigned int cmd, unsigned long arg)
2084 {
2085         struct ll_sb_info *sbi = ll_i2sbi(inode);
2086         struct ptlrpc_request *req = NULL;
2087         int rc, flags = 0;
2088         ENTRY;
2089
2090         switch(cmd) {
2091         case EXT3_IOC_GETFLAGS: {
2092                 struct lustre_id id;
2093                 __u64 valid = OBD_MD_FLFLAGS;
2094                 struct mds_body *body;
2095
2096                 ll_inode2id(&id, inode);
2097                 rc = md_getattr(sbi->ll_md_exp, &id, valid, NULL, NULL, 0, 0,
2098                                 &req);
2099                 if (rc) {
2100                         CERROR("failure %d inode %lu\n", rc, inode->i_ino);
2101                         RETURN(-abs(rc));
2102                 }
2103
2104                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
2105
2106                 if (body->flags & S_APPEND)
2107                         flags |= EXT3_APPEND_FL;
2108                 if (body->flags & S_IMMUTABLE)
2109                         flags |= EXT3_IMMUTABLE_FL;
2110                 if (body->flags & S_NOATIME)
2111                         flags |= EXT3_NOATIME_FL;
2112
2113                 ptlrpc_req_finished (req);
2114
2115                 RETURN(put_user(flags, (int *)arg));
2116         }
2117         case EXT3_IOC_SETFLAGS: {
2118                 struct mdc_op_data *op_data;
2119                 struct iattr attr;
2120                 struct obdo *oa;
2121                 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
2122
2123                 if (get_user(flags, (int *)arg))
2124                         RETURN(-EFAULT);
2125
2126                 oa = obdo_alloc();
2127                 if (!oa)
2128                         RETURN(-ENOMEM);
2129
2130                 OBD_ALLOC(op_data, sizeof(*op_data));
2131                 if (op_data == NULL) {
2132                         obdo_free(oa);
2133                         RETURN(-ENOMEM);
2134                 }
2135                 ll_prepare_mdc_data(op_data, inode, NULL, NULL, 0, 0);
2136
2137                 memset(&attr, 0x0, sizeof(attr));
2138                 attr.ia_attr_flags = flags;
2139                 attr.ia_valid |= ATTR_ATTR_FLAG;
2140
2141                 rc = md_setattr(sbi->ll_md_exp, op_data,
2142                                 &attr, NULL, 0, NULL, 0, &req);
2143                 OBD_FREE(op_data, sizeof(*op_data));
2144                 if (rc) {
2145                         ptlrpc_req_finished(req);
2146                         if (rc != -EPERM && rc != -EACCES)
2147                                 CERROR("md_setattr fails: rc = %d\n", rc);
2148                         obdo_free(oa);
2149                         RETURN(rc);
2150                 }
2151                 ptlrpc_req_finished(req);
2152
2153                 oa->o_id = lsm->lsm_object_id;
2154                 oa->o_gr = lsm->lsm_object_gr;
2155                 oa->o_flags = flags;
2156                 *(obdo_id(oa)) = ll_i2info(inode)->lli_id;
2157                 oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP 
2158                               | OBD_MD_FLIFID;
2159
2160                 rc = obd_setattr(sbi->ll_dt_exp, oa, lsm, NULL);
2161                 obdo_free(oa);
2162                 if (rc) {
2163                         if (rc != -EPERM && rc != -EACCES)
2164                                 CERROR("md_setattr fails: rc = %d\n", rc);
2165                         RETURN(rc);
2166                 }
2167
2168                 if (flags & EXT3_APPEND_FL)
2169                         inode->i_flags |= S_APPEND;
2170                 else
2171                         inode->i_flags &= ~S_APPEND;
2172                 if (flags & EXT3_IMMUTABLE_FL)
2173                         inode->i_flags |= S_IMMUTABLE;
2174                 else
2175                         inode->i_flags &= ~S_IMMUTABLE;
2176                 if (flags & EXT3_NOATIME_FL)
2177                         inode->i_flags |= S_NOATIME;
2178                 else
2179                         inode->i_flags &= ~S_NOATIME;
2180
2181                 RETURN(0);
2182         }
2183         default:
2184                 RETURN(-ENOSYS);
2185         }
2186
2187         RETURN(0);
2188 }
2189
2190 /* this is only called in the case of forced umount. */
2191 void ll_umount_begin(struct super_block *sb)
2192 {
2193         struct ll_sb_info *sbi = ll_s2sbi(sb);
2194         struct obd_ioctl_data ioc_data = { 0 };
2195         struct obd_device *obd;
2196         ENTRY;
2197      
2198         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p count %d active %d\n", sb,
2199                sb->s_count, atomic_read(&sb->s_active));
2200         
2201         obd = class_exp2obd(sbi->ll_md_exp);
2202         if (obd == NULL) {
2203                 CERROR("Invalid MDC connection handle "LPX64"\n",
2204                        sbi->ll_md_exp->exp_handle.h_cookie);
2205                 EXIT;
2206                 return;
2207         }
2208         obd->obd_no_recov = 1;
2209         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_md_exp,
2210                       sizeof(ioc_data), &ioc_data, NULL);
2211
2212         obd = class_exp2obd(sbi->ll_dt_exp);
2213         if (obd == NULL) {
2214                 CERROR("Invalid LOV connection handle "LPX64"\n",
2215                        sbi->ll_dt_exp->exp_handle.h_cookie);
2216                 EXIT;
2217                 return;
2218         }
2219
2220         obd->obd_no_recov = 1;
2221         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_dt_exp,
2222                       sizeof(ioc_data), &ioc_data, NULL);
2223
2224         /*
2225          * really, we'd like to wait until there are no requests outstanding,
2226          * and then continue.  For now, we just invalidate the requests,
2227          * schedule, and hope.
2228          */
2229         schedule();
2230
2231         EXIT;
2232 }
2233
2234 int ll_prep_inode(struct obd_export *dt_exp, struct obd_export *md_exp,
2235                   struct inode **inode, struct ptlrpc_request *req,
2236                   int offset, struct super_block *sb)
2237 {
2238         struct lustre_md md;
2239         int rc = 0;
2240
2241         rc = mdc_req2lustre_md(md_exp, req, offset, dt_exp, &md);
2242         if (rc)
2243                 RETURN(rc);
2244
2245         if (*inode) {
2246                 ll_update_inode(*inode, &md);
2247         } else {
2248                 LASSERT(sb);
2249                 *inode = ll_iget(sb, id_ino(&md.body->id1), &md);
2250                 if (*inode == NULL || is_bad_inode(*inode)) {
2251                         /* free the lsm if we allocated one above */
2252                         if (md.lsm != NULL)
2253                                 obd_free_memmd(dt_exp, &md.lsm);
2254                         if (md.mea != NULL)
2255                                 obd_free_memmd(md_exp,
2256                                                (struct lov_stripe_md**)&md.mea);
2257                         rc = -ENOMEM;
2258                         CERROR("new_inode -fatal: rc %d\n", rc);
2259                 }
2260         }
2261
2262         RETURN(rc);
2263 }
2264
2265 int ll_show_options(struct seq_file *m, struct vfsmount *mnt)
2266 {
2267         struct ll_sb_info *sbi = ll_s2sbi(mnt->mnt_sb);
2268         struct lustre_mount_data *lmd = sbi->ll_lmd;
2269
2270         if (lmd) {
2271                 seq_printf(m, ",mds_sec=%s,oss_sec=%s",
2272                            lmd->lmd_mds_security, lmd->lmd_oss_security);
2273         }
2274         seq_printf(m, ",%s", sbi->ll_remote ? "remote" : "local");
2275         if (sbi->ll_remote && lmd)
2276                 seq_printf(m, ",nllu=%u:%u", lmd->lmd_nllu, lmd->lmd_nllg);
2277
2278         if (lmd && lmd->lmd_pag)
2279                 seq_printf(m, ",pag");
2280
2281         return 0;
2282 }
2283
2284 int ll_get_fid(struct obd_export *exp, struct lustre_id *idp,
2285                char *filename, struct lustre_id *ret)
2286 {
2287         struct ptlrpc_request *request = NULL;
2288         struct mds_body *body;
2289         int rc;
2290
2291         rc = md_getattr_lock(exp, idp, filename, strlen(filename) + 1,
2292                              OBD_MD_FID, 0, &request);
2293         if (rc < 0) {
2294                 CDEBUG(D_INFO, "md_getattr_lock failed on %s: rc %d\n",
2295                        filename, rc);
2296                 return rc;
2297         }
2298
2299         body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
2300         LASSERT(body != NULL);
2301         LASSERT_REPSWABBED(request, 0);
2302
2303         *ret = body->id1;
2304         ptlrpc_req_finished(request);
2305
2306         return rc;
2307 }
2308
2309 int ll_flush_cred(struct inode *inode)
2310 {
2311         struct ll_sb_info *sbi = ll_i2sbi(inode);
2312         int rc = 0;
2313
2314         /* XXX to avoid adding api, we simply use set_info() interface
2315          * to notify underlying obds. set_info() is more like a ioctl() now...
2316          */
2317         if (sbi->ll_md_exp) {
2318                 rc = obd_set_info(sbi->ll_md_exp,
2319                                   strlen("flush_cred"), "flush_cred",
2320                                   0, NULL);
2321                 if (rc)
2322                         return rc;
2323         }
2324
2325         if (sbi->ll_dt_exp) {
2326                 rc = obd_set_info(sbi->ll_dt_exp,
2327                                   strlen("flush_cred"), "flush_cred",
2328                                   0, NULL);
2329                 if (rc)
2330                         return rc;
2331         }
2332
2333         return rc;
2334 }