Whamcloud - gitweb
land b1_5 onto HEAD
[fs/lustre-release.git] / lustre / obdclass / linux / linux-obdo.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Object Devices Class Driver
5  *
6  *  Copyright (C) 2001-2003 Cluster File Systems, Inc.
7  *
8  *   This file is part of the Lustre file system, http://www.lustre.org
9  *   Lustre is a trademark of Cluster File Systems, Inc.
10  *
11  *   You may have signed or agreed to another license before downloading
12  *   this software.  If so, you are bound by the terms and conditions
13  *   of that agreement, and the following does not apply to you.  See the
14  *   LICENSE file included with this distribution for more information.
15  *
16  *   If you did not agree to a different license, then this copy of Lustre
17  *   is open source software; you can redistribute it and/or modify it
18  *   under the terms of version 2 of the GNU General Public License as
19  *   published by the Free Software Foundation.
20  *
21  *   In either case, Lustre is distributed in the hope that it will be
22  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
23  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  *   license text for more details.
25  *
26  * These are the only exported functions, they provide some generic
27  * infrastructure for managing object devices
28  */
29
30 #define DEBUG_SUBSYSTEM S_CLASS
31 #ifndef EXPORT_SYMTAB
32 # define EXPORT_SYMTAB
33 #endif
34
35 #ifndef __KERNEL__
36 #include <liblustre.h>
37 #else
38 #include <linux/module.h>
39 #include <obd_class.h>
40 #include <lustre/lustre_idl.h>
41 #endif
42
43 #ifdef __KERNEL__
44 #include <linux/fs.h>
45 #include <linux/pagemap.h> /* for PAGE_CACHE_SIZE */
46
47 void obdo_from_iattr(struct obdo *oa, struct iattr *attr, unsigned int ia_valid)
48 {
49         if (ia_valid & ATTR_ATIME) {
50                 oa->o_atime = LTIME_S(attr->ia_atime);
51                 oa->o_valid |= OBD_MD_FLATIME;
52         }
53         if (ia_valid & ATTR_MTIME) {
54                 oa->o_mtime = LTIME_S(attr->ia_mtime);
55                 oa->o_valid |= OBD_MD_FLMTIME;
56         }
57         if (ia_valid & ATTR_CTIME) {
58                 oa->o_ctime = LTIME_S(attr->ia_ctime);
59                 oa->o_valid |= OBD_MD_FLCTIME;
60         }
61         if (ia_valid & ATTR_SIZE) {
62                 oa->o_size = attr->ia_size;
63                 oa->o_valid |= OBD_MD_FLSIZE;
64         }
65         if (ia_valid & ATTR_MODE) {
66                 oa->o_mode = attr->ia_mode;
67                 oa->o_valid |= OBD_MD_FLTYPE | OBD_MD_FLMODE;
68                 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
69                         oa->o_mode &= ~S_ISGID;
70         }
71         if (ia_valid & ATTR_UID) {
72                 oa->o_uid = attr->ia_uid;
73                 oa->o_valid |= OBD_MD_FLUID;
74         }
75         if (ia_valid & ATTR_GID) {
76                 oa->o_gid = attr->ia_gid;
77                 oa->o_valid |= OBD_MD_FLGID;
78         }
79 }
80 EXPORT_SYMBOL(obdo_from_iattr);
81
82 void iattr_from_obdo(struct iattr *attr, struct obdo *oa, obd_flag valid)
83 {
84         valid &= oa->o_valid;
85
86         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
87                 CDEBUG(D_INODE, "valid "LPX64", new time "LPU64"/"LPU64"\n",
88                        oa->o_valid, oa->o_mtime, oa->o_ctime);
89
90         attr->ia_valid = 0;
91         if (valid & OBD_MD_FLATIME) {
92                 LTIME_S(attr->ia_atime) = oa->o_atime;
93                 attr->ia_valid |= ATTR_ATIME;
94         }
95         if (valid & OBD_MD_FLMTIME) {
96                 LTIME_S(attr->ia_mtime) = oa->o_mtime;
97                 attr->ia_valid |= ATTR_MTIME;
98         }
99         if (valid & OBD_MD_FLCTIME) {
100                 LTIME_S(attr->ia_ctime) = oa->o_ctime;
101                 attr->ia_valid |= ATTR_CTIME;
102         }
103         if (valid & OBD_MD_FLSIZE) {
104                 attr->ia_size = oa->o_size;
105                 attr->ia_valid |= ATTR_SIZE;
106         }
107 #if 0   /* you shouldn't be able to change a file's type with setattr */
108         if (valid & OBD_MD_FLTYPE) {
109                 attr->ia_mode = (attr->ia_mode & ~S_IFMT)|(oa->o_mode & S_IFMT);
110                 attr->ia_valid |= ATTR_MODE;
111         }
112 #endif
113         if (valid & OBD_MD_FLMODE) {
114                 attr->ia_mode = (attr->ia_mode & S_IFMT)|(oa->o_mode & ~S_IFMT);
115                 attr->ia_valid |= ATTR_MODE;
116                 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
117                         attr->ia_mode &= ~S_ISGID;
118         }
119         if (valid & OBD_MD_FLUID) {
120                 attr->ia_uid = oa->o_uid;
121                 attr->ia_valid |= ATTR_UID;
122         }
123         if (valid & OBD_MD_FLGID) {
124                 attr->ia_gid = oa->o_gid;
125                 attr->ia_valid |= ATTR_GID;
126         }
127 }
128 EXPORT_SYMBOL(iattr_from_obdo);
129
130 /* WARNING: the file systems must take care not to tinker with
131    attributes they don't manage (such as blocks). */
132 void obdo_from_inode(struct obdo *dst, struct inode *src, obd_flag valid)
133 {
134         obd_flag newvalid = 0;
135
136         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
137                 CDEBUG(D_INODE, "valid %x, new time %lu/%lu\n",
138                        valid, LTIME_S(src->i_mtime), 
139                        LTIME_S(src->i_ctime));
140
141         if (valid & OBD_MD_FLATIME) {
142                 dst->o_atime = LTIME_S(src->i_atime);
143                 newvalid |= OBD_MD_FLATIME;
144         }
145         if (valid & OBD_MD_FLMTIME) {
146                 dst->o_mtime = LTIME_S(src->i_mtime);
147                 newvalid |= OBD_MD_FLMTIME;
148         }
149         if (valid & OBD_MD_FLCTIME) {
150                 dst->o_ctime = LTIME_S(src->i_ctime);
151                 newvalid |= OBD_MD_FLCTIME;
152         }
153         if (valid & OBD_MD_FLSIZE) {
154                 dst->o_size = src->i_size;
155                 newvalid |= OBD_MD_FLSIZE;
156         }
157         if (valid & OBD_MD_FLBLOCKS) {  /* allocation of space (x512 bytes) */
158                 dst->o_blocks = src->i_blocks;
159                 newvalid |= OBD_MD_FLBLOCKS;
160         }
161         if (valid & OBD_MD_FLBLKSZ) {   /* optimal block size */
162                 dst->o_blksize = 1<<src->i_blkbits;
163                 newvalid |= OBD_MD_FLBLKSZ;
164         }
165         if (valid & OBD_MD_FLTYPE) {
166                 dst->o_mode = (dst->o_mode & S_IALLUGO)|(src->i_mode & S_IFMT);
167                 newvalid |= OBD_MD_FLTYPE;
168         }
169         if (valid & OBD_MD_FLMODE) {
170                 dst->o_mode = (dst->o_mode & S_IFMT)|(src->i_mode & S_IALLUGO);
171                 newvalid |= OBD_MD_FLMODE;
172         }
173         if (valid & OBD_MD_FLUID) {
174                 dst->o_uid = src->i_uid;
175                 newvalid |= OBD_MD_FLUID;
176         }
177         if (valid & OBD_MD_FLGID) {
178                 dst->o_gid = src->i_gid;
179                 newvalid |= OBD_MD_FLGID;
180         }
181         if (valid & OBD_MD_FLFLAGS) {
182                 dst->o_flags = src->i_flags;
183                 newvalid |= OBD_MD_FLFLAGS;
184         }
185         if (valid & OBD_MD_FLGENER) {
186                 dst->o_generation = src->i_generation;
187                 newvalid |= OBD_MD_FLGENER;
188         }
189         if (valid & OBD_MD_FLFID) {
190                 dst->o_fid = src->i_ino;
191                 newvalid |= OBD_MD_FLFID;
192         }
193
194         dst->o_valid |= newvalid;
195 }
196 EXPORT_SYMBOL(obdo_from_inode);
197
198 void obdo_refresh_inode(struct inode *dst, struct obdo *src, obd_flag valid)
199 {
200         valid &= src->o_valid;
201
202         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
203                 CDEBUG(D_INODE,
204                        "valid "LPX64", cur time %lu/%lu, new "LPU64"/"LPU64"\n",
205                        src->o_valid, LTIME_S(dst->i_mtime),
206                        LTIME_S(dst->i_ctime), src->o_mtime, src->o_ctime);
207
208         if (valid & OBD_MD_FLATIME && src->o_atime > LTIME_S(dst->i_atime))
209                 LTIME_S(dst->i_atime) = src->o_atime;
210         
211         /* mtime is always updated with ctime, but can be set in past.
212            As write and utime(2) may happen within 1 second, and utime's
213            mtime has a priority over write's one, leave mtime from mds 
214            for the same ctimes. */
215         if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(dst->i_ctime)) {
216                 LTIME_S(dst->i_ctime) = src->o_ctime;
217                 if (valid & OBD_MD_FLMTIME)
218                         LTIME_S(dst->i_mtime) = src->o_mtime;
219         }
220         if (valid & OBD_MD_FLSIZE) 
221                 dst->i_size = src->o_size;
222         /* optimum IO size */
223         if (valid & OBD_MD_FLBLKSZ && src->o_blksize > (1<<dst->i_blkbits)) {
224                 dst->i_blkbits = ffs(src->o_blksize)-1;
225 #ifdef HAVE_INODE_BLKSIZE
226                 dst->i_blksize = src->o_blksize;
227 #endif
228         }
229
230         if (dst->i_blkbits < CFS_PAGE_SHIFT) {
231 #ifdef HAVE_INODE_BLKSIZE
232                 dst->i_blksize = CFS_PAGE_SIZE;
233 #endif
234                 dst->i_blkbits = CFS_PAGE_SHIFT;
235         }
236
237         /* allocation of space */
238         if (valid & OBD_MD_FLBLOCKS && src->o_blocks > dst->i_blocks)
239                 dst->i_blocks = src->o_blocks;
240 }
241 EXPORT_SYMBOL(obdo_refresh_inode);
242
243 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_flag valid)
244 {
245         valid &= src->o_valid;
246
247         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
248                 CDEBUG(D_INODE,
249                        "valid "LPX64", cur time %lu/%lu, new "LPU64"/"LPU64"\n",
250                        src->o_valid, LTIME_S(dst->i_mtime),
251                        LTIME_S(dst->i_ctime), src->o_mtime, src->o_ctime);
252
253         if (valid & OBD_MD_FLATIME)
254                 LTIME_S(dst->i_atime) = src->o_atime;
255         if (valid & OBD_MD_FLMTIME)
256                 LTIME_S(dst->i_mtime) = src->o_mtime;
257         if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(dst->i_ctime))
258                 LTIME_S(dst->i_ctime) = src->o_ctime;
259         if (valid & OBD_MD_FLSIZE)
260                 dst->i_size = src->o_size;
261         if (valid & OBD_MD_FLBLOCKS) { /* allocation of space */
262                 dst->i_blocks = src->o_blocks;
263                 if (dst->i_blocks < src->o_blocks) /* overflow */
264                         dst->i_blocks = -1;
265
266         }
267         if (valid & OBD_MD_FLBLKSZ) {
268                 dst->i_blkbits = ffs(src->o_blksize)-1;
269 #ifdef HAVE_INODE_BLKSIZE
270                 dst->i_blksize = src->o_blksize;
271 #endif
272         }
273         if (valid & OBD_MD_FLTYPE)
274                 dst->i_mode = (dst->i_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
275         if (valid & OBD_MD_FLMODE)
276                 dst->i_mode = (dst->i_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
277         if (valid & OBD_MD_FLUID)
278                 dst->i_uid = src->o_uid;
279         if (valid & OBD_MD_FLGID)
280                 dst->i_gid = src->o_gid;
281         if (valid & OBD_MD_FLFLAGS)
282                 dst->i_flags = src->o_flags;
283         if (valid & OBD_MD_FLGENER)
284                 dst->i_generation = src->o_generation;
285 }
286 EXPORT_SYMBOL(obdo_to_inode);
287 #endif
288