]> granicus.if.org Git - zfs/blob - include/linux/vfs_compat.h
Fix multi-line error messages in blkdev_compat.h
[zfs] / 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  * Copyright (C) 2015 Jörg Thalheim.
25  */
26
27 #ifndef _ZFS_VFS_H
28 #define _ZFS_VFS_H
29
30 #include <sys/taskq.h>
31 #include <sys/cred.h>
32 #include <linux/backing-dev.h>
33
34 /*
35  * 2.6.28 API change,
36  * Added insert_inode_locked() helper function, prior to this most callers
37  * used insert_inode_hash().  The older method doesn't check for collisions
38  * in the inode_hashtable but it still acceptible for use.
39  */
40 #ifndef HAVE_INSERT_INODE_LOCKED
41 static inline int
42 insert_inode_locked(struct inode *ip)
43 {
44         insert_inode_hash(ip);
45         return (0);
46 }
47 #endif /* HAVE_INSERT_INODE_LOCKED */
48
49 /*
50  * 2.6.35 API change,
51  * Add truncate_setsize() if it is not exported by the Linux kernel.
52  *
53  * Truncate the inode and pages associated with the inode. The pages are
54  * unmapped and removed from cache.
55  */
56 #ifndef HAVE_TRUNCATE_SETSIZE
57 static inline void
58 truncate_setsize(struct inode *ip, loff_t new)
59 {
60         struct address_space *mapping = ip->i_mapping;
61
62         i_size_write(ip, new);
63
64         unmap_mapping_range(mapping, new + PAGE_SIZE - 1, 0, 1);
65         truncate_inode_pages(mapping, new);
66         unmap_mapping_range(mapping, new + PAGE_SIZE - 1, 0, 1);
67 }
68 #endif /* HAVE_TRUNCATE_SETSIZE */
69
70 /*
71  * 2.6.32 - 2.6.33, bdi_setup_and_register() is not available.
72  * 2.6.34 - 3.19, bdi_setup_and_register() takes 3 arguments.
73  * 4.0 - x.y, bdi_setup_and_register() takes 2 arguments.
74  */
75 #if defined(HAVE_2ARGS_BDI_SETUP_AND_REGISTER)
76 static inline int
77 zpl_bdi_setup_and_register(struct backing_dev_info *bdi, char *name)
78 {
79         return (bdi_setup_and_register(bdi, name));
80 }
81 #elif defined(HAVE_3ARGS_BDI_SETUP_AND_REGISTER)
82 static inline int
83 zpl_bdi_setup_and_register(struct backing_dev_info *bdi, char *name)
84 {
85         return (bdi_setup_and_register(bdi, name, BDI_CAP_MAP_COPY));
86 }
87 #else
88 extern atomic_long_t zfs_bdi_seq;
89
90 static inline int
91 zpl_bdi_setup_and_register(struct backing_dev_info *bdi, char *name)
92 {
93         char tmp[32];
94         int error;
95
96         bdi->name = name;
97         bdi->capabilities = BDI_CAP_MAP_COPY;
98
99         error = bdi_init(bdi);
100         if (error)
101                 return (error);
102
103         sprintf(tmp, "%.28s%s", name, "-%d");
104         error = bdi_register(bdi, NULL, tmp,
105             atomic_long_inc_return(&zfs_bdi_seq));
106         if (error) {
107                 bdi_destroy(bdi);
108                 return (error);
109         }
110
111         return (error);
112 }
113 #endif
114
115 /*
116  * 2.6.38 API change,
117  * LOOKUP_RCU flag introduced to distinguish rcu-walk from ref-walk cases.
118  */
119 #ifndef LOOKUP_RCU
120 #define LOOKUP_RCU      0x0
121 #endif /* LOOKUP_RCU */
122
123 /*
124  * 3.2-rc1 API change,
125  * Add set_nlink() if it is not exported by the Linux kernel.
126  *
127  * i_nlink is read-only in Linux 3.2, but it can be set directly in
128  * earlier kernels.
129  */
130 #ifndef HAVE_SET_NLINK
131 static inline void
132 set_nlink(struct inode *inode, unsigned int nlink)
133 {
134         inode->i_nlink = nlink;
135 }
136 #endif /* HAVE_SET_NLINK */
137
138 /*
139  * 3.3 API change,
140  * The VFS .create, .mkdir and .mknod callbacks were updated to take a
141  * umode_t type rather than an int.  To cleanly handle both definitions
142  * the zpl_umode_t type is introduced and set accordingly.
143  */
144 #ifdef HAVE_MKDIR_UMODE_T
145 typedef umode_t         zpl_umode_t;
146 #else
147 typedef int             zpl_umode_t;
148 #endif
149
150 /*
151  * 3.5 API change,
152  * The clear_inode() function replaces end_writeback() and introduces an
153  * ordering change regarding when the inode_sync_wait() occurs.  See the
154  * configure check in config/kernel-clear-inode.m4 for full details.
155  */
156 #if defined(HAVE_EVICT_INODE) && !defined(HAVE_CLEAR_INODE)
157 #define clear_inode(ip)         end_writeback(ip)
158 #endif /* HAVE_EVICT_INODE && !HAVE_CLEAR_INODE */
159
160 /*
161  * 3.6 API change,
162  * The sget() helper function now takes the mount flags as an argument.
163  */
164 #ifdef HAVE_5ARG_SGET
165 #define zpl_sget(type, cmp, set, fl, mtd)       sget(type, cmp, set, fl, mtd)
166 #else
167 #define zpl_sget(type, cmp, set, fl, mtd)       sget(type, cmp, set, mtd)
168 #endif /* HAVE_5ARG_SGET */
169
170 #if defined(SEEK_HOLE) && defined(SEEK_DATA) && !defined(HAVE_LSEEK_EXECUTE)
171 static inline loff_t
172 lseek_execute(
173         struct file *filp,
174         struct inode *inode,
175         loff_t offset,
176         loff_t maxsize)
177 {
178         if (offset < 0 && !(filp->f_mode & FMODE_UNSIGNED_OFFSET))
179                 return (-EINVAL);
180
181         if (offset > maxsize)
182                 return (-EINVAL);
183
184         if (offset != filp->f_pos) {
185                 spin_lock(&filp->f_lock);
186                 filp->f_pos = offset;
187                 filp->f_version = 0;
188                 spin_unlock(&filp->f_lock);
189         }
190
191         return (offset);
192 }
193 #endif /* SEEK_HOLE && SEEK_DATA && !HAVE_LSEEK_EXECUTE */
194
195 #if defined(CONFIG_FS_POSIX_ACL)
196 /*
197  * These functions safely approximates the behavior of posix_acl_release()
198  * which cannot be used because it calls the GPL-only symbol kfree_rcu().
199  * The in-kernel version, which can access the RCU, frees the ACLs after
200  * the grace period expires.  Because we're unsure how long that grace
201  * period may be this implementation conservatively delays for 60 seconds.
202  * This is several orders of magnitude larger than expected grace period.
203  * At 60 seconds the kernel will also begin issuing RCU stall warnings.
204  */
205 #include <linux/posix_acl.h>
206
207 #if defined(HAVE_POSIX_ACL_RELEASE) && !defined(HAVE_POSIX_ACL_RELEASE_GPL_ONLY)
208 #define zpl_posix_acl_release(arg)              posix_acl_release(arg)
209 #else
210 void zpl_posix_acl_release_impl(struct posix_acl *);
211
212 static inline void
213 zpl_posix_acl_release(struct posix_acl *acl)
214 {
215         if ((acl == NULL) || (acl == ACL_NOT_CACHED))
216                 return;
217
218         if (atomic_dec_and_test(&acl->a_refcount))
219                 zpl_posix_acl_release_impl(acl);
220 }
221 #endif /* HAVE_POSIX_ACL_RELEASE */
222
223 #ifdef HAVE_SET_CACHED_ACL_USABLE
224 #define zpl_set_cached_acl(ip, ty, n)           set_cached_acl(ip, ty, n)
225 #define zpl_forget_cached_acl(ip, ty)           forget_cached_acl(ip, ty)
226 #else
227 static inline void
228 zpl_set_cached_acl(struct inode *ip, int type, struct posix_acl *newer)
229 {
230         struct posix_acl *older = NULL;
231
232         spin_lock(&ip->i_lock);
233
234         if ((newer != ACL_NOT_CACHED) && (newer != NULL))
235                 posix_acl_dup(newer);
236
237         switch (type) {
238         case ACL_TYPE_ACCESS:
239                 older = ip->i_acl;
240                 rcu_assign_pointer(ip->i_acl, newer);
241                 break;
242         case ACL_TYPE_DEFAULT:
243                 older = ip->i_default_acl;
244                 rcu_assign_pointer(ip->i_default_acl, newer);
245                 break;
246         }
247
248         spin_unlock(&ip->i_lock);
249
250         zpl_posix_acl_release(older);
251 }
252
253 static inline void
254 zpl_forget_cached_acl(struct inode *ip, int type)
255 {
256         zpl_set_cached_acl(ip, type, (struct posix_acl *)ACL_NOT_CACHED);
257 }
258 #endif /* HAVE_SET_CACHED_ACL_USABLE */
259
260 #ifndef HAVE___POSIX_ACL_CHMOD
261 #ifdef HAVE_POSIX_ACL_CHMOD
262 #define __posix_acl_chmod(acl, gfp, mode)       posix_acl_chmod(acl, gfp, mode)
263 #define __posix_acl_create(acl, gfp, mode)      posix_acl_create(acl, gfp, mode)
264 #else
265 static inline int
266 __posix_acl_chmod(struct posix_acl **acl, int flags, umode_t umode)
267 {
268         struct posix_acl *oldacl = *acl;
269         mode_t mode = umode;
270         int error;
271
272         *acl = posix_acl_clone(*acl, flags);
273         zpl_posix_acl_release(oldacl);
274
275         if (!(*acl))
276                 return (-ENOMEM);
277
278         error = posix_acl_chmod_masq(*acl, mode);
279         if (error) {
280                 zpl_posix_acl_release(*acl);
281                 *acl = NULL;
282         }
283
284         return (error);
285 }
286
287 static inline int
288 __posix_acl_create(struct posix_acl **acl, int flags, umode_t *umodep)
289 {
290         struct posix_acl *oldacl = *acl;
291         mode_t mode = *umodep;
292         int error;
293
294         *acl = posix_acl_clone(*acl, flags);
295         zpl_posix_acl_release(oldacl);
296
297         if (!(*acl))
298                 return (-ENOMEM);
299
300         error = posix_acl_create_masq(*acl, &mode);
301         *umodep = mode;
302
303         if (error < 0) {
304                 zpl_posix_acl_release(*acl);
305                 *acl = NULL;
306         }
307
308         return (error);
309 }
310 #endif /* HAVE_POSIX_ACL_CHMOD */
311 #endif /* HAVE___POSIX_ACL_CHMOD */
312
313 #ifdef HAVE_POSIX_ACL_EQUIV_MODE_UMODE_T
314 typedef umode_t zpl_equivmode_t;
315 #else
316 typedef mode_t zpl_equivmode_t;
317 #endif /* HAVE_POSIX_ACL_EQUIV_MODE_UMODE_T */
318
319 /*
320  * 4.8 API change,
321  * posix_acl_valid() now must be passed a namespace, the namespace from
322  * from super block associated with the given inode is used for this purpose.
323  */
324 #ifdef HAVE_POSIX_ACL_VALID_WITH_NS
325 #define zpl_posix_acl_valid(ip, acl)  posix_acl_valid(ip->i_sb->s_user_ns, acl)
326 #else
327 #define zpl_posix_acl_valid(ip, acl)  posix_acl_valid(acl)
328 #endif
329
330 #endif /* CONFIG_FS_POSIX_ACL */
331
332 /*
333  * 2.6.38 API change,
334  * The is_owner_or_cap() function was renamed to inode_owner_or_capable().
335  */
336 #ifdef HAVE_INODE_OWNER_OR_CAPABLE
337 #define zpl_inode_owner_or_capable(ip)          inode_owner_or_capable(ip)
338 #else
339 #define zpl_inode_owner_or_capable(ip)          is_owner_or_cap(ip)
340 #endif /* HAVE_INODE_OWNER_OR_CAPABLE */
341
342 /*
343  * 3.19 API change
344  * struct access f->f_dentry->d_inode was replaced by accessor function
345  * file_inode(f)
346  */
347 #ifndef HAVE_FILE_INODE
348 static inline struct inode *file_inode(const struct file *f)
349 {
350         return (f->f_dentry->d_inode);
351 }
352 #endif /* HAVE_FILE_INODE */
353
354 /*
355  * 4.1 API change
356  * struct access file->f_path.dentry was replaced by accessor function
357  * file_dentry(f)
358  */
359 #ifndef HAVE_FILE_DENTRY
360 static inline struct dentry *file_dentry(const struct file *f)
361 {
362         return (f->f_path.dentry);
363 }
364 #endif /* HAVE_FILE_DENTRY */
365
366 #ifdef HAVE_KUID_HELPERS
367 static inline uid_t zfs_uid_read_impl(struct inode *ip)
368 {
369 #ifdef HAVE_SUPER_USER_NS
370         return (from_kuid(ip->i_sb->s_user_ns, ip->i_uid));
371 #else
372         return (from_kuid(kcred->user_ns, ip->i_uid));
373 #endif
374 }
375
376 static inline uid_t zfs_uid_read(struct inode *ip)
377 {
378         return (zfs_uid_read_impl(ip));
379 }
380
381 static inline gid_t zfs_gid_read_impl(struct inode *ip)
382 {
383 #ifdef HAVE_SUPER_USER_NS
384         return (from_kgid(ip->i_sb->s_user_ns, ip->i_gid));
385 #else
386         return (from_kgid(kcred->user_ns, ip->i_gid));
387 #endif
388 }
389
390 static inline gid_t zfs_gid_read(struct inode *ip)
391 {
392         return (zfs_gid_read_impl(ip));
393 }
394
395 static inline void zfs_uid_write(struct inode *ip, uid_t uid)
396 {
397 #ifdef HAVE_SUPER_USER_NS
398         ip->i_uid = make_kuid(ip->i_sb->s_user_ns, uid);
399 #else
400         ip->i_uid = make_kuid(kcred->user_ns, uid);
401 #endif
402 }
403
404 static inline void zfs_gid_write(struct inode *ip, gid_t gid)
405 {
406 #ifdef HAVE_SUPER_USER_NS
407         ip->i_gid = make_kgid(ip->i_sb->s_user_ns, gid);
408 #else
409         ip->i_gid = make_kgid(kcred->user_ns, gid);
410 #endif
411 }
412
413 #else
414 static inline uid_t zfs_uid_read(struct inode *ip)
415 {
416         return (ip->i_uid);
417 }
418
419 static inline gid_t zfs_gid_read(struct inode *ip)
420 {
421         return (ip->i_gid);
422 }
423
424 static inline void zfs_uid_write(struct inode *ip, uid_t uid)
425 {
426         ip->i_uid = uid;
427 }
428
429 static inline void zfs_gid_write(struct inode *ip, gid_t gid)
430 {
431         ip->i_gid = gid;
432 }
433 #endif
434
435 /*
436  * 2.6.38 API change
437  */
438 #ifdef HAVE_FOLLOW_DOWN_ONE
439 #define zpl_follow_down_one(path)               follow_down_one(path)
440 #define zpl_follow_up(path)                     follow_up(path)
441 #else
442 #define zpl_follow_down_one(path)               follow_down(path)
443 #define zpl_follow_up(path)                     follow_up(path)
444 #endif
445
446 /*
447  * 4.9 API change
448  */
449 #ifndef HAVE_SETATTR_PREPARE
450 static inline int
451 setattr_prepare(struct dentry *dentry, struct iattr *ia)
452 {
453         return (inode_change_ok(dentry->d_inode, ia));
454 }
455 #endif
456
457 #endif /* _ZFS_VFS_H */