blob: 29f11e10a7c7d213623063b12b5e98e5bd01a85f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/affs/amigaffs.c
4 *
5 * (c) 1996 Hans-Joachim Widmaier - Rewritten
6 *
7 * (C) 1993 Ray Burr - Amiga FFS filesystem.
8 *
9 * Please send bug reports to: hjw@zvw.de
10 */
11
DengChaodb39c162015-11-12 21:40:41 +080012#include <linux/math64.h>
Jeff Layton9dffe562017-12-11 06:35:10 -050013#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "affs.h"
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016/*
17 * Functions for accessing Amiga-FFS structures.
18 */
19
20
21/* Insert a header block bh into the directory dir
22 * caller must hold AFFS_DIR->i_hash_lock!
23 */
24
25int
26affs_insert_hash(struct inode *dir, struct buffer_head *bh)
27{
28 struct super_block *sb = dir->i_sb;
29 struct buffer_head *dir_bh;
30 u32 ino, hash_ino;
31 int offset;
32
33 ino = bh->b_blocknr;
34 offset = affs_hash_name(sb, AFFS_TAIL(sb, bh)->name + 1, AFFS_TAIL(sb, bh)->name[0]);
35
Geert Uytterhoeven08fe1002015-02-17 13:46:10 -080036 pr_debug("%s(dir=%lu, ino=%d)\n", __func__, dir->i_ino, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38 dir_bh = affs_bread(sb, dir->i_ino);
39 if (!dir_bh)
40 return -EIO;
41
42 hash_ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[offset]);
43 while (hash_ino) {
44 affs_brelse(dir_bh);
45 dir_bh = affs_bread(sb, hash_ino);
46 if (!dir_bh)
47 return -EIO;
48 hash_ino = be32_to_cpu(AFFS_TAIL(sb, dir_bh)->hash_chain);
49 }
50 AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino);
51 AFFS_TAIL(sb, bh)->hash_chain = 0;
52 affs_fix_checksum(sb, bh);
53
54 if (dir->i_ino == dir_bh->b_blocknr)
55 AFFS_HEAD(dir_bh)->table[offset] = cpu_to_be32(ino);
56 else
57 AFFS_TAIL(sb, dir_bh)->hash_chain = cpu_to_be32(ino);
58
59 affs_adjust_checksum(dir_bh, ino);
60 mark_buffer_dirty_inode(dir_bh, dir);
61 affs_brelse(dir_bh);
62
Deepa Dinamani02027d42016-09-14 07:48:05 -070063 dir->i_mtime = dir->i_ctime = current_time(dir);
Jeff Layton9dffe562017-12-11 06:35:10 -050064 inode_inc_iversion(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 mark_inode_dirty(dir);
66
67 return 0;
68}
69
70/* Remove a header block from its directory.
71 * caller must hold AFFS_DIR->i_hash_lock!
72 */
73
74int
75affs_remove_hash(struct inode *dir, struct buffer_head *rem_bh)
76{
77 struct super_block *sb;
78 struct buffer_head *bh;
79 u32 rem_ino, hash_ino;
80 __be32 ino;
81 int offset, retval;
82
83 sb = dir->i_sb;
84 rem_ino = rem_bh->b_blocknr;
85 offset = affs_hash_name(sb, AFFS_TAIL(sb, rem_bh)->name+1, AFFS_TAIL(sb, rem_bh)->name[0]);
Geert Uytterhoeven08fe1002015-02-17 13:46:10 -080086 pr_debug("%s(dir=%lu, ino=%d, hashval=%d)\n", __func__, dir->i_ino,
87 rem_ino, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 bh = affs_bread(sb, dir->i_ino);
90 if (!bh)
91 return -EIO;
92
93 retval = -ENOENT;
94 hash_ino = be32_to_cpu(AFFS_HEAD(bh)->table[offset]);
95 while (hash_ino) {
96 if (hash_ino == rem_ino) {
97 ino = AFFS_TAIL(sb, rem_bh)->hash_chain;
98 if (dir->i_ino == bh->b_blocknr)
99 AFFS_HEAD(bh)->table[offset] = ino;
100 else
101 AFFS_TAIL(sb, bh)->hash_chain = ino;
102 affs_adjust_checksum(bh, be32_to_cpu(ino) - hash_ino);
103 mark_buffer_dirty_inode(bh, dir);
104 AFFS_TAIL(sb, rem_bh)->parent = 0;
105 retval = 0;
106 break;
107 }
108 affs_brelse(bh);
109 bh = affs_bread(sb, hash_ino);
110 if (!bh)
111 return -EIO;
112 hash_ino = be32_to_cpu(AFFS_TAIL(sb, bh)->hash_chain);
113 }
114
115 affs_brelse(bh);
116
Deepa Dinamani02027d42016-09-14 07:48:05 -0700117 dir->i_mtime = dir->i_ctime = current_time(dir);
Jeff Layton9dffe562017-12-11 06:35:10 -0500118 inode_inc_iversion(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 mark_inode_dirty(dir);
120
121 return retval;
122}
123
124static void
Al Viro12447c42012-06-09 13:06:09 -0400125affs_fix_dcache(struct inode *inode, u32 entry_ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Al Viro12447c42012-06-09 13:06:09 -0400127 struct dentry *dentry;
Nick Piggin873feea2011-01-07 17:50:06 +1100128 spin_lock(&inode->i_lock);
Al Viro946e51f2014-10-26 19:19:16 -0400129 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 if (entry_ino == (u32)(long)dentry->d_fsdata) {
Al Viro12447c42012-06-09 13:06:09 -0400131 dentry->d_fsdata = (void *)inode->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 break;
133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
Nick Piggin873feea2011-01-07 17:50:06 +1100135 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
138
139/* Remove header from link chain */
140
141static int
142affs_remove_link(struct dentry *dentry)
143{
David Howells2b0143b2015-03-17 22:25:59 +0000144 struct inode *dir, *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 struct super_block *sb = inode->i_sb;
Fabian Frederick47091872015-06-30 14:57:58 -0700146 struct buffer_head *bh, *link_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 u32 link_ino, ino;
148 int retval;
149
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700150 pr_debug("%s(key=%ld)\n", __func__, inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 retval = -EIO;
152 bh = affs_bread(sb, inode->i_ino);
153 if (!bh)
154 goto done;
155
156 link_ino = (u32)(long)dentry->d_fsdata;
157 if (inode->i_ino == link_ino) {
158 /* we can't remove the head of the link, as its blocknr is still used as ino,
159 * so we remove the block of the first link instead.
160 */
161 link_ino = be32_to_cpu(AFFS_TAIL(sb, bh)->link_chain);
162 link_bh = affs_bread(sb, link_ino);
163 if (!link_bh)
164 goto done;
165
David Howells210f8552008-02-07 00:15:29 -0800166 dir = affs_iget(sb, be32_to_cpu(AFFS_TAIL(sb, link_bh)->parent));
167 if (IS_ERR(dir)) {
168 retval = PTR_ERR(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 goto done;
David Howells210f8552008-02-07 00:15:29 -0800170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 affs_lock_dir(dir);
Al Viro12447c42012-06-09 13:06:09 -0400173 /*
174 * if there's a dentry for that block, make it
175 * refer to inode itself.
176 */
177 affs_fix_dcache(inode, link_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 retval = affs_remove_hash(dir, link_bh);
Christoph Hellwigec1ab0a2008-05-09 12:35:29 +0200179 if (retval) {
180 affs_unlock_dir(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 goto done;
Christoph Hellwigec1ab0a2008-05-09 12:35:29 +0200182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 mark_buffer_dirty_inode(link_bh, inode);
184
185 memcpy(AFFS_TAIL(sb, bh)->name, AFFS_TAIL(sb, link_bh)->name, 32);
186 retval = affs_insert_hash(dir, bh);
Christoph Hellwigec1ab0a2008-05-09 12:35:29 +0200187 if (retval) {
188 affs_unlock_dir(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 goto done;
Christoph Hellwigec1ab0a2008-05-09 12:35:29 +0200190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 mark_buffer_dirty_inode(bh, inode);
192
193 affs_unlock_dir(dir);
194 iput(dir);
195 } else {
196 link_bh = affs_bread(sb, link_ino);
197 if (!link_bh)
198 goto done;
199 }
200
201 while ((ino = be32_to_cpu(AFFS_TAIL(sb, bh)->link_chain)) != 0) {
202 if (ino == link_ino) {
203 __be32 ino2 = AFFS_TAIL(sb, link_bh)->link_chain;
204 AFFS_TAIL(sb, bh)->link_chain = ino2;
205 affs_adjust_checksum(bh, be32_to_cpu(ino2) - link_ino);
206 mark_buffer_dirty_inode(bh, inode);
207 retval = 0;
208 /* Fix the link count, if bh is a normal header block without links */
209 switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
210 case ST_LINKDIR:
211 case ST_LINKFILE:
212 break;
213 default:
214 if (!AFFS_TAIL(sb, bh)->link_chain)
Miklos Szeredibfe86842011-10-28 14:13:29 +0200215 set_nlink(inode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217 affs_free_block(sb, link_ino);
218 goto done;
219 }
220 affs_brelse(bh);
221 bh = affs_bread(sb, ino);
222 if (!bh)
223 goto done;
224 }
225 retval = -ENOENT;
226done:
227 affs_brelse(link_bh);
228 affs_brelse(bh);
229 return retval;
230}
231
232
233static int
234affs_empty_dir(struct inode *inode)
235{
236 struct super_block *sb = inode->i_sb;
237 struct buffer_head *bh;
238 int retval, size;
239
240 retval = -EIO;
241 bh = affs_bread(sb, inode->i_ino);
242 if (!bh)
243 goto done;
244
245 retval = -ENOTEMPTY;
246 for (size = AFFS_SB(sb)->s_hashsize - 1; size >= 0; size--)
247 if (AFFS_HEAD(bh)->table[size])
248 goto not_empty;
249 retval = 0;
250not_empty:
251 affs_brelse(bh);
252done:
253 return retval;
254}
255
256
257/* Remove a filesystem object. If the object to be removed has
258 * links to it, one of the links must be changed to inherit
259 * the file or directory. As above, any inode will do.
260 * The buffer will not be freed. If the header is a link, the
261 * block will be marked as free.
262 * This function returns a negative error number in case of
263 * an error, else 0 if the inode is to be deleted or 1 if not.
264 */
265
266int
267affs_remove_header(struct dentry *dentry)
268{
269 struct super_block *sb;
270 struct inode *inode, *dir;
271 struct buffer_head *bh = NULL;
272 int retval;
273
David Howells2b0143b2015-03-17 22:25:59 +0000274 dir = d_inode(dentry->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 sb = dir->i_sb;
276
277 retval = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +0000278 inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (!inode)
280 goto done;
281
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700282 pr_debug("%s(key=%ld)\n", __func__, inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 retval = -EIO;
284 bh = affs_bread(sb, (u32)(long)dentry->d_fsdata);
285 if (!bh)
286 goto done;
287
288 affs_lock_link(inode);
289 affs_lock_dir(dir);
290 switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
291 case ST_USERDIR:
292 /* if we ever want to support links to dirs
293 * i_hash_lock of the inode must only be
294 * taken after some checks
295 */
296 affs_lock_dir(inode);
297 retval = affs_empty_dir(inode);
298 affs_unlock_dir(inode);
299 if (retval)
300 goto done_unlock;
301 break;
302 default:
303 break;
304 }
305
306 retval = affs_remove_hash(dir, bh);
307 if (retval)
308 goto done_unlock;
309 mark_buffer_dirty_inode(bh, inode);
310
311 affs_unlock_dir(dir);
312
313 if (inode->i_nlink > 1)
314 retval = affs_remove_link(dentry);
315 else
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200316 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 affs_unlock_link(inode);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700318 inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 mark_inode_dirty(inode);
320
321done:
322 affs_brelse(bh);
323 return retval;
324
325done_unlock:
326 affs_unlock_dir(dir);
327 affs_unlock_link(inode);
328 goto done;
329}
330
331/* Checksum a block, do various consistency checks and optionally return
332 the blocks type number. DATA points to the block. If their pointers
333 are non-null, *PTYPE and *STYPE are set to the primary and secondary
334 block types respectively, *HASHSIZE is set to the size of the hashtable
335 (which lets us calculate the block size).
336 Returns non-zero if the block is not consistent. */
337
338u32
339affs_checksum_block(struct super_block *sb, struct buffer_head *bh)
340{
341 __be32 *ptr = (__be32 *)bh->b_data;
342 u32 sum;
343 int bsize;
344
345 sum = 0;
346 for (bsize = sb->s_blocksize / sizeof(__be32); bsize > 0; bsize--)
347 sum += be32_to_cpu(*ptr++);
348 return sum;
349}
350
351/*
352 * Calculate the checksum of a disk block and store it
353 * at the indicated position.
354 */
355
356void
357affs_fix_checksum(struct super_block *sb, struct buffer_head *bh)
358{
359 int cnt = sb->s_blocksize / sizeof(__be32);
360 __be32 *ptr = (__be32 *)bh->b_data;
361 u32 checksum;
362 __be32 *checksumptr;
363
364 checksumptr = ptr + 5;
365 *checksumptr = 0;
366 for (checksum = 0; cnt > 0; ptr++, cnt--)
367 checksum += be32_to_cpu(*ptr);
368 *checksumptr = cpu_to_be32(-checksum);
369}
370
371void
Fabian Frederickc1618202017-02-27 14:27:54 -0800372affs_secs_to_datestamp(time64_t secs, struct affs_date *ds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 u32 days;
375 u32 minute;
DengChaodb39c162015-11-12 21:40:41 +0800376 s32 rem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Deepa Dinamani487b25b2019-03-21 14:05:12 -0700378 secs -= sys_tz.tz_minuteswest * 60 + AFFS_EPOCH_DELTA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (secs < 0)
380 secs = 0;
DengChaodb39c162015-11-12 21:40:41 +0800381 days = div_s64_rem(secs, 86400, &rem);
382 minute = rem / 60;
383 rem -= minute * 60;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 ds->days = cpu_to_be32(days);
386 ds->mins = cpu_to_be32(minute);
DengChaodb39c162015-11-12 21:40:41 +0800387 ds->ticks = cpu_to_be32(rem * 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Al Viroa760b032011-07-26 03:04:30 -0400390umode_t
Fabian Frederickc1618202017-02-27 14:27:54 -0800391affs_prot_to_mode(u32 prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Al Viroa760b032011-07-26 03:04:30 -0400393 umode_t mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 if (!(prot & FIBF_NOWRITE))
Fabian Frederick1bafd6f2017-02-27 14:27:52 -0800396 mode |= 0200;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (!(prot & FIBF_NOREAD))
Fabian Frederick1bafd6f2017-02-27 14:27:52 -0800398 mode |= 0400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (!(prot & FIBF_NOEXECUTE))
Fabian Frederick1bafd6f2017-02-27 14:27:52 -0800400 mode |= 0100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (prot & FIBF_GRP_WRITE)
Fabian Frederick1bafd6f2017-02-27 14:27:52 -0800402 mode |= 0020;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (prot & FIBF_GRP_READ)
Fabian Frederick1bafd6f2017-02-27 14:27:52 -0800404 mode |= 0040;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (prot & FIBF_GRP_EXECUTE)
Fabian Frederick1bafd6f2017-02-27 14:27:52 -0800406 mode |= 0010;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (prot & FIBF_OTR_WRITE)
Fabian Frederick1bafd6f2017-02-27 14:27:52 -0800408 mode |= 0002;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (prot & FIBF_OTR_READ)
Fabian Frederick1bafd6f2017-02-27 14:27:52 -0800410 mode |= 0004;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (prot & FIBF_OTR_EXECUTE)
Fabian Frederick1bafd6f2017-02-27 14:27:52 -0800412 mode |= 0001;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 return mode;
415}
416
417void
Fabian Frederickc1618202017-02-27 14:27:54 -0800418affs_mode_to_prot(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 u32 prot = AFFS_I(inode)->i_protect;
Al Viroa760b032011-07-26 03:04:30 -0400421 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Max Staudtb6e4827c02020-08-27 17:49:00 +0200423 /*
424 * First, clear all RWED bits for owner, group, other.
425 * Then, recalculate them afresh.
426 *
427 * We'll always clear the delete-inhibit bit for the owner, as that is
428 * the classic single-user mode AmigaOS protection bit and we need to
429 * stay compatible with all scenarios.
430 *
431 * Since multi-user AmigaOS is an extension, we'll only set the
432 * delete-allow bit if any of the other bits in the same user class
433 * (group/other) are used.
434 */
435 prot &= ~(FIBF_NOEXECUTE | FIBF_NOREAD
436 | FIBF_NOWRITE | FIBF_NODELETE
437 | FIBF_GRP_EXECUTE | FIBF_GRP_READ
438 | FIBF_GRP_WRITE | FIBF_GRP_DELETE
439 | FIBF_OTR_EXECUTE | FIBF_OTR_READ
440 | FIBF_OTR_WRITE | FIBF_OTR_DELETE);
441
442 /* Classic single-user AmigaOS flags. These are inverted. */
Fabian Frederick1bafd6f2017-02-27 14:27:52 -0800443 if (!(mode & 0100))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 prot |= FIBF_NOEXECUTE;
Fabian Frederick1bafd6f2017-02-27 14:27:52 -0800445 if (!(mode & 0400))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 prot |= FIBF_NOREAD;
Fabian Frederick1bafd6f2017-02-27 14:27:52 -0800447 if (!(mode & 0200))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 prot |= FIBF_NOWRITE;
Max Staudtb6e4827c02020-08-27 17:49:00 +0200449
450 /* Multi-user extended flags. Not inverted. */
Fabian Frederick1bafd6f2017-02-27 14:27:52 -0800451 if (mode & 0010)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 prot |= FIBF_GRP_EXECUTE;
Fabian Frederick1bafd6f2017-02-27 14:27:52 -0800453 if (mode & 0040)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 prot |= FIBF_GRP_READ;
Fabian Frederick1bafd6f2017-02-27 14:27:52 -0800455 if (mode & 0020)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 prot |= FIBF_GRP_WRITE;
Max Staudtb6e4827c02020-08-27 17:49:00 +0200457 if (mode & 0070)
458 prot |= FIBF_GRP_DELETE;
459
Fabian Frederick1bafd6f2017-02-27 14:27:52 -0800460 if (mode & 0001)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 prot |= FIBF_OTR_EXECUTE;
Fabian Frederick1bafd6f2017-02-27 14:27:52 -0800462 if (mode & 0004)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 prot |= FIBF_OTR_READ;
Fabian Frederick1bafd6f2017-02-27 14:27:52 -0800464 if (mode & 0002)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 prot |= FIBF_OTR_WRITE;
Max Staudtb6e4827c02020-08-27 17:49:00 +0200466 if (mode & 0007)
467 prot |= FIBF_OTR_DELETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 AFFS_I(inode)->i_protect = prot;
470}
471
472void
473affs_error(struct super_block *sb, const char *function, const char *fmt, ...)
474{
Fabian Frederick1ee54b02014-12-12 16:57:49 -0800475 struct va_format vaf;
476 va_list args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Fabian Frederick1ee54b02014-12-12 16:57:49 -0800478 va_start(args, fmt);
479 vaf.fmt = fmt;
480 vaf.va = &args;
481 pr_crit("error (device %s): %s(): %pV\n", sb->s_id, function, &vaf);
David Howellsbc98a422017-07-17 08:45:34 +0100482 if (!sb_rdonly(sb))
Fabian Frederick0158de12014-06-06 14:37:24 -0700483 pr_warn("Remounting filesystem read-only\n");
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800484 sb->s_flags |= SB_RDONLY;
Fabian Frederick1ee54b02014-12-12 16:57:49 -0800485 va_end(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
488void
489affs_warning(struct super_block *sb, const char *function, const char *fmt, ...)
490{
Fabian Frederick1ee54b02014-12-12 16:57:49 -0800491 struct va_format vaf;
492 va_list args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Fabian Frederick1ee54b02014-12-12 16:57:49 -0800494 va_start(args, fmt);
495 vaf.fmt = fmt;
496 vaf.va = &args;
497 pr_warn("(device %s): %s(): %pV\n", sb->s_id, function, &vaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 va_end(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
Fabian Frederick8ca57722014-04-07 15:39:01 -0700501bool
502affs_nofilenametruncate(const struct dentry *dentry)
503{
Al Viroe0b3f592016-07-29 18:22:49 -0400504 return affs_test_opt(AFFS_SB(dentry->d_sb)->s_flags, SF_NO_TRUNCATE);
Fabian Frederick8ca57722014-04-07 15:39:01 -0700505}
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507/* Check if the name is valid for a affs object. */
508
509int
Fabian Frederick8ca57722014-04-07 15:39:01 -0700510affs_check_name(const unsigned char *name, int len, bool notruncate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
512 int i;
513
Fabian Frederickf1578532015-02-17 13:46:23 -0800514 if (len > AFFSNAMEMAX) {
Fabian Frederick8ca57722014-04-07 15:39:01 -0700515 if (notruncate)
516 return -ENAMETOOLONG;
Fabian Frederickb4478e32015-02-17 13:46:25 -0800517 len = AFFSNAMEMAX;
Fabian Frederick8ca57722014-04-07 15:39:01 -0700518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 for (i = 0; i < len; i++) {
520 if (name[i] < ' ' || name[i] == ':'
521 || (name[i] > 0x7e && name[i] < 0xa0))
522 return -EINVAL;
523 }
524
525 return 0;
526}
527
528/* This function copies name to bstr, with at most 30
529 * characters length. The bstr will be prepended by
530 * a length byte.
531 * NOTE: The name will must be already checked by
532 * affs_check_name()!
533 */
534
535int
536affs_copy_name(unsigned char *bstr, struct dentry *dentry)
537{
Fabian Frederickf1578532015-02-17 13:46:23 -0800538 u32 len = min(dentry->d_name.len, AFFSNAMEMAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 *bstr++ = len;
541 memcpy(bstr, dentry->d_name.name, len);
542 return len;
543}