Whamcloud - gitweb
land lustre part of b_hd_sec on HEAD.
[fs/lustre-release.git] / lustre / llite / llite_nfs.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *   NFS export of Lustre Light File System 
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 #include <linux/lustre_lite.h>
26 #include "llite_internal.h"
27
28 __u32 get_uuid2int(const char *name, int len)
29 {
30         __u32 key0 = 0x12a3fe2d, key1 = 0x37abe8f9;
31         while (len--) {
32                 __u32 key = key1 + (key0 ^ (*name++ * 7152373));
33                 if (key & 0x80000000) key -= 0x7fffffff;
34                 key1 = key0;
35                 key0 = key;
36         }
37         return (key0 << 1);
38 }
39
40 static struct inode *search_inode_for_lustre(struct super_block *sb,
41                                              unsigned long ino,
42                                              unsigned long generation,
43                                              int mode)
44 {
45         struct ptlrpc_request *req = NULL;
46         struct ll_sb_info *sbi = ll_s2sbi(sb);
47         struct lustre_id id;
48         __u64 valid = 0;
49         int eadatalen = 0, rc;
50         struct inode *inode = NULL;
51
52         inode = ILOOKUP(sb, ino, NULL, NULL);
53
54         if (inode)
55                 return inode;
56         if (S_ISREG(mode)) {
57                 eadatalen = obd_size_diskmd(sbi->ll_dt_exp, NULL);
58                 valid |= OBD_MD_FLEASIZE;
59         }
60         id_type(&id) = mode;
61         id_ino(&id) = (__u64)ino;
62         id_gen(&id) = generation;
63
64         rc = md_getattr(sbi->ll_md_exp, &id, valid, NULL, 0, 
65                         eadatalen, &req);
66         if (rc) {
67                 CERROR("failure %d inode %lu\n", rc, ino);
68                 return ERR_PTR(rc);
69         }
70
71         rc = ll_prep_inode(sbi->ll_dt_exp, sbi->ll_md_exp,
72                            &inode, req, 0, sb);
73         if (rc) {
74                 ptlrpc_req_finished(req);
75                 return ERR_PTR(rc);
76         }
77         ptlrpc_req_finished(req);
78
79         return inode;
80 }
81
82 extern struct dentry_operations ll_d_ops;
83
84 static struct dentry *ll_iget_for_nfs(struct super_block *sb, unsigned long ino,
85                                       __u32 generation, umode_t mode)
86 {
87         struct inode *inode;
88         struct dentry *result;
89         struct list_head *lp;
90
91         if (ino == 0)
92                 return ERR_PTR(-ESTALE);
93
94         inode = search_inode_for_lustre(sb, ino, generation, mode);
95         if (IS_ERR(inode)) {
96                 return ERR_PTR(PTR_ERR(inode));
97         }
98         if (is_bad_inode(inode) 
99             || (generation && inode->i_generation != generation)
100             ){
101                 /* we didn't find the right inode.. */
102               CERROR(" Inode %lu, Bad count: %lu %d or version  %u %u\n",
103                         inode->i_ino, 
104                         (unsigned long)inode->i_nlink, 
105                         atomic_read(&inode->i_count), 
106                         inode->i_generation, 
107                         generation);
108                 iput(inode);
109                 return ERR_PTR(-ESTALE);
110         }
111         
112         /* now to find a dentry.
113          * If possible, get a well-connected one
114          */
115         spin_lock(&dcache_lock);
116         for (lp = inode->i_dentry.next; lp != &inode->i_dentry ; lp=lp->next) {
117                 result = list_entry(lp,struct dentry, d_alias);
118                 if (!(result->d_flags & DCACHE_DISCONNECTED)) {
119                         dget_locked(result);
120                         ll_set_dflags(result, DCACHE_REFERENCED);
121                         spin_unlock(&dcache_lock);
122                         iput(inode);
123                         return result;
124                 }
125         }
126         spin_unlock(&dcache_lock);
127         result = d_alloc_root(inode);
128         if (result == NULL) {
129                 iput(inode);
130                 return ERR_PTR(-ENOMEM);
131         }
132         result->d_flags |= DCACHE_DISCONNECTED;
133         
134         ll_set_dd(result);
135         result->d_op = &ll_d_ops;
136         return result;
137 }
138
139 struct dentry *ll_fh_to_dentry(struct super_block *sb, __u32 *data, int len,
140                                int fhtype, int parent)
141 {
142         switch (fhtype) {
143                 case 2:
144                         if (len < 5)
145                                 break;
146                         if (parent)
147                                 return ll_iget_for_nfs(sb, data[3], 0, data[4]);
148                 case 1:
149                         if (len < 3)
150                                 break;
151                         if (parent)
152                                 break;
153                         return ll_iget_for_nfs(sb, data[0], data[1], data[2]);
154                 default: break;
155         }
156         return ERR_PTR(-EINVAL);
157 }
158
159 int ll_dentry_to_fh(struct dentry *dentry, __u32 *datap, int *lenp,
160                     int need_parent)
161 {
162         if (*lenp < 3)
163                 return 255;
164         *datap++ = dentry->d_inode->i_ino;
165         *datap++ = dentry->d_inode->i_generation;
166         *datap++ = (__u32)(S_IFMT & dentry->d_inode->i_mode);
167
168         if (*lenp == 3 || S_ISDIR(dentry->d_inode->i_mode)) {
169                 *lenp = 3;
170                 return 1;
171         }
172         if (dentry->d_parent) { 
173                 *datap++ = dentry->d_parent->d_inode->i_ino;
174                 *datap++ = (__u32)(S_IFMT & dentry->d_parent->d_inode->i_mode);
175          
176                 *lenp = 5;
177                 return 2;
178         }
179         *lenp = 3;
180         return 1;
181 }