]> git.proxmox.com Git - mirror_zfs.git/blob - include/linux/vfs_compat.h
8ec1bccad6691b45ab0588b12a9653745f05e199
[mirror_zfs.git] / include / linux / vfs_compat.h
1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (C) 2011 Lawrence Livermore National Security, LLC.
24 */
25
26 #ifndef _ZFS_VFS_H
27 #define _ZFS_VFS_H
28
29 /*
30 * 2.6.28 API change,
31 * Added insert_inode_locked() helper function, prior to this most callers
32 * used insert_inode_hash(). The older method doesn't check for collisions
33 * in the inode_hashtable but it still acceptible for use.
34 */
35 #ifndef HAVE_INSERT_INODE_LOCKED
36 static inline int
37 insert_inode_locked(struct inode *ip)
38 {
39 insert_inode_hash(ip);
40 return (0);
41 }
42 #endif /* HAVE_INSERT_INODE_LOCKED */
43
44 /*
45 * 2.6.35 API change,
46 * Add truncate_setsize() if it is not exported by the Linux kernel.
47 *
48 * Truncate the inode and pages associated with the inode. The pages are
49 * unmapped and removed from cache.
50 */
51 #ifndef HAVE_TRUNCATE_SETSIZE
52 static inline void
53 truncate_setsize(struct inode *ip, loff_t new)
54 {
55 struct address_space *mapping = ip->i_mapping;
56
57 i_size_write(ip, new);
58
59 unmap_mapping_range(mapping, new + PAGE_SIZE - 1, 0, 1);
60 truncate_inode_pages(mapping, new);
61 unmap_mapping_range(mapping, new + PAGE_SIZE - 1, 0, 1);
62 }
63 #endif /* HAVE_TRUNCATE_SETSIZE */
64
65 #if defined(HAVE_BDI) && !defined(HAVE_BDI_SETUP_AND_REGISTER)
66 /*
67 * 2.6.34 API change,
68 * Add bdi_setup_and_register() function if not yet provided by kernel.
69 * It is used to quickly initialize and register a BDI for the filesystem.
70 */
71 extern atomic_long_t zfs_bdi_seq;
72
73 static inline int
74 bdi_setup_and_register(struct backing_dev_info *bdi,char *name,unsigned int cap)
75 {
76 char tmp[32];
77 int error;
78
79 bdi->name = name;
80 bdi->capabilities = cap;
81 error = bdi_init(bdi);
82 if (error)
83 return (error);
84
85 sprintf(tmp, "%.28s%s", name, "-%d");
86 error = bdi_register(bdi, NULL, tmp,
87 atomic_long_inc_return(&zfs_bdi_seq));
88 if (error) {
89 bdi_destroy(bdi);
90 return (error);
91 }
92
93 return (error);
94 }
95 #endif /* HAVE_BDI && !HAVE_BDI_SETUP_AND_REGISTER */
96
97 /*
98 * 2.6.38 API change,
99 * LOOKUP_RCU flag introduced to distinguish rcu-walk from ref-walk cases.
100 */
101 #ifndef LOOKUP_RCU
102 #define LOOKUP_RCU 0x0
103 #endif /* LOOKUP_RCU */
104
105 /*
106 * 3.2-rc1 API change,
107 * Add set_nlink() if it is not exported by the Linux kernel.
108 *
109 * i_nlink is read-only in Linux 3.2, but it can be set directly in
110 * earlier kernels.
111 */
112 #ifndef HAVE_SET_NLINK
113 static inline void
114 set_nlink(struct inode *inode, unsigned int nlink)
115 {
116 inode->i_nlink = nlink;
117 }
118 #endif /* HAVE_SET_NLINK */
119
120 /*
121 * 3.3 API change,
122 * The VFS .create, .mkdir and .mknod callbacks were updated to take a
123 * umode_t type rather than an int. To cleanly handle both definitions
124 * the zpl_umode_t type is introduced and set accordingly.
125 */
126 #ifdef HAVE_MKDIR_UMODE_T
127 typedef umode_t zpl_umode_t;
128 #else
129 typedef int zpl_umode_t;
130 #endif
131
132 /*
133 * 3.5 API change,
134 * The clear_inode() function replaces end_writeback() and introduces an
135 * ordering change regarding when the inode_sync_wait() occurs. See the
136 * configure check in config/kernel-clear-inode.m4 for full details.
137 */
138 #if defined(HAVE_EVICT_INODE) && !defined(HAVE_CLEAR_INODE)
139 #define clear_inode(ip) end_writeback(ip)
140 #endif /* HAVE_EVICT_INODE && !HAVE_CLEAR_INODE */
141
142 /*
143 * 3.6 API change,
144 * The sget() helper function now takes the mount flags as an argument.
145 */
146 #ifdef HAVE_5ARG_SGET
147 #define zpl_sget(type, cmp, set, fl, mtd) sget(type, cmp, set, fl, mtd)
148 #else
149 #define zpl_sget(type, cmp, set, fl, mtd) sget(type, cmp, set, mtd)
150 #endif /* HAVE_5ARG_SGET */
151
152 #define ZFS_IOC_GETFLAGS FS_IOC_GETFLAGS
153 #define ZFS_IOC_SETFLAGS FS_IOC_SETFLAGS
154
155 #if defined(SEEK_HOLE) && defined(SEEK_DATA) && !defined(HAVE_LSEEK_EXECUTE)
156 static inline loff_t
157 lseek_execute(struct file *filp, struct inode *inode,
158 loff_t offset, loff_t maxsize)
159 {
160 if (offset < 0 && !(filp->f_mode & FMODE_UNSIGNED_OFFSET))
161 return (-EINVAL);
162
163 if (offset > maxsize)
164 return (-EINVAL);
165
166 if (offset != filp->f_pos) {
167 spin_lock(&filp->f_lock);
168 filp->f_pos = offset;
169 filp->f_version = 0;
170 spin_unlock(&filp->f_lock);
171 }
172
173 return (offset);
174 }
175 #endif /* SEEK_HOLE && SEEK_DATA && !HAVE_LSEEK_EXECUTE */
176
177 #if defined(CONFIG_FS_POSIX_ACL)
178 /*
179 * These functions safely approximates the behavior of posix_acl_release()
180 * which cannot be used because it calls the GPL-only symbol kfree_rcu().
181 * The in-kernel version, which can access the RCU, frees the ACLs after
182 * the grace period expires. Because we're unsure how long that grace
183 * period may be this implementation conservatively delays for 60 seconds.
184 * This is several orders of magnitude larger than expected grace period.
185 * At 60 seconds the kernel will also begin issuing RCU stall warnings.
186 */
187 #include <linux/posix_acl.h>
188 #ifndef HAVE_POSIX_ACL_CACHING
189 #define ACL_NOT_CACHED ((void *)(-1))
190 #endif /* HAVE_POSIX_ACL_CACHING */
191
192 #if defined(HAVE_POSIX_ACL_RELEASE) && !defined(HAVE_POSIX_ACL_RELEASE_GPL_ONLY)
193
194 #define zpl_posix_acl_release(arg) posix_acl_release(arg)
195 #define zpl_set_cached_acl(ip, ty, n) set_cached_acl(ip, ty, n)
196 #define zpl_forget_cached_acl(ip, ty) forget_cached_acl(ip, ty)
197
198 #else
199
200 static inline void
201 zpl_posix_acl_free(void *arg) {
202 kfree(arg);
203 }
204
205 static inline void
206 zpl_posix_acl_release(struct posix_acl *acl)
207 {
208 if ((acl == NULL) || (acl == ACL_NOT_CACHED))
209 return;
210
211 if (atomic_dec_and_test(&acl->a_refcount)) {
212 taskq_dispatch_delay(system_taskq, zpl_posix_acl_free, acl,
213 TQ_SLEEP, ddi_get_lbolt() + 60*HZ);
214 }
215 }
216
217 static inline void
218 zpl_set_cached_acl(struct inode *ip, int type, struct posix_acl *newer) {
219 #ifdef HAVE_POSIX_ACL_CACHING
220 struct posix_acl *older = NULL;
221
222 spin_lock(&ip->i_lock);
223
224 if ((newer != ACL_NOT_CACHED) && (newer != NULL))
225 posix_acl_dup(newer);
226
227 switch(type) {
228 case ACL_TYPE_ACCESS:
229 older = ip->i_acl;
230 rcu_assign_pointer(ip->i_acl,newer);
231 break;
232 case ACL_TYPE_DEFAULT:
233 older = ip->i_default_acl;
234 rcu_assign_pointer(ip->i_default_acl,newer);
235 break;
236 }
237
238 spin_unlock(&ip->i_lock);
239
240 zpl_posix_acl_release(older);
241 #endif /* HAVE_POSIX_ACL_CACHING */
242 }
243
244 static inline void
245 zpl_forget_cached_acl(struct inode *ip, int type) {
246 zpl_set_cached_acl(ip, type, (struct posix_acl *)ACL_NOT_CACHED);
247 }
248 #endif /* HAVE_POSIX_ACL_RELEASE */
249
250 /*
251 * 2.6.38 API change,
252 * The is_owner_or_cap() function was renamed to inode_owner_or_capable().
253 */
254 #ifdef HAVE_INODE_OWNER_OR_CAPABLE
255 #define zpl_inode_owner_or_capable(ip) inode_owner_or_capable(ip)
256 #else
257 #define zpl_inode_owner_or_capable(ip) is_owner_or_cap(ip)
258 #endif /* HAVE_INODE_OWNER_OR_CAPABLE */
259
260 #ifndef HAVE_POSIX_ACL_CHMOD
261 static inline int
262 posix_acl_chmod(struct posix_acl **acl, int flags, umode_t umode) {
263 struct posix_acl *oldacl = *acl;
264 mode_t mode = umode;
265 int error;
266
267 *acl = posix_acl_clone(*acl, flags);
268 zpl_posix_acl_release(oldacl);
269
270 if (!(*acl))
271 return (-ENOMEM);
272
273 error = posix_acl_chmod_masq(*acl, mode);
274 if (error) {
275 zpl_posix_acl_release(*acl);
276 *acl = NULL;
277 }
278
279 return (error);
280 }
281
282 static inline int
283 posix_acl_create(struct posix_acl** acl, int flags, umode_t* umodep) {
284 struct posix_acl *oldacl = *acl;
285 mode_t mode = *umodep;
286 int error;
287
288 *acl = posix_acl_clone(*acl, flags);
289 zpl_posix_acl_release(oldacl);
290
291 if (!(*acl))
292 return (-ENOMEM);
293
294 error = posix_acl_create_masq(*acl, &mode);
295 *umodep = mode;
296
297 if (error < 0) {
298 zpl_posix_acl_release(*acl);
299 *acl = NULL;
300 }
301
302 return (error);
303 }
304 #endif /* HAVE_POSIX_ACL_CHMOD */
305
306 #ifndef HAVE_CURRENT_UMASK
307 static inline int
308 current_umask(void)
309 {
310 return (current->fs->umask);
311 }
312 #endif /* HAVE_CURRENT_UMASK */
313
314 #ifdef HAVE_POSIX_ACL_EQUIV_MODE_UMODE_T
315 typedef umode_t zpl_equivmode_t;
316 #else
317 typedef mode_t zpl_equivmode_t;
318 #endif /* HAVE_POSIX_ACL_EQUIV_MODE_UMODE_T */
319 #endif /* CONFIG_FS_POSIX_ACL */
320
321 #endif /* _ZFS_VFS_H */