Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * QNX4 file system, Linux implementation. |
| 4 | * |
| 5 | * Version : 0.2.1 |
| 6 | * |
| 7 | * Using parts of the xiafs filesystem. |
| 8 | * |
| 9 | * History : |
| 10 | * |
| 11 | * 01-06-1998 by Richard Frowijn : first release. |
| 12 | * 21-06-1998 by Frank Denis : dcache support, fixed error codes. |
| 13 | * 04-07-1998 by Frank Denis : first step for rmdir/unlink. |
| 14 | */ |
| 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/buffer_head.h> |
Al Viro | 964f536 | 2009-06-07 09:47:13 -0400 | [diff] [blame] | 17 | #include "qnx4.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | |
| 20 | /* |
| 21 | * check if the filename is correct. For some obscure reason, qnx writes a |
| 22 | * new file twice in the directory entry, first with all possible options at 0 |
| 23 | * and for a second time the way it is, they want us not to access the qnx |
| 24 | * filesystem when whe are using linux. |
| 25 | */ |
| 26 | static int qnx4_match(int len, const char *name, |
| 27 | struct buffer_head *bh, unsigned long *offset) |
| 28 | { |
| 29 | struct qnx4_inode_entry *de; |
| 30 | int namelen, thislen; |
| 31 | |
| 32 | if (bh == NULL) { |
Anders Larsen | 891ddb9 | 2009-09-26 20:15:09 +0200 | [diff] [blame] | 33 | printk(KERN_WARNING "qnx4: matching unassigned buffer !\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | return 0; |
| 35 | } |
| 36 | de = (struct qnx4_inode_entry *) (bh->b_data + *offset); |
| 37 | *offset += QNX4_DIR_ENTRY_SIZE; |
| 38 | if ((de->di_status & QNX4_FILE_LINK) != 0) { |
| 39 | namelen = QNX4_NAME_MAX; |
| 40 | } else { |
| 41 | namelen = QNX4_SHORT_NAME_MAX; |
| 42 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | thislen = strlen( de->di_fname ); |
| 44 | if ( thislen > namelen ) |
| 45 | thislen = namelen; |
| 46 | if (len != thislen) { |
| 47 | return 0; |
| 48 | } |
| 49 | if (strncmp(name, de->di_fname, len) == 0) { |
| 50 | if ((de->di_status & (QNX4_FILE_USED|QNX4_FILE_LINK)) != 0) { |
| 51 | return 1; |
| 52 | } |
| 53 | } |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | static struct buffer_head *qnx4_find_entry(int len, struct inode *dir, |
| 58 | const char *name, struct qnx4_inode_entry **res_dir, int *ino) |
| 59 | { |
| 60 | unsigned long block, offset, blkofs; |
| 61 | struct buffer_head *bh; |
| 62 | |
| 63 | *res_dir = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | bh = NULL; |
| 65 | block = offset = blkofs = 0; |
| 66 | while (blkofs * QNX4_BLOCK_SIZE + offset < dir->i_size) { |
| 67 | if (!bh) { |
Al Viro | 8f82eca | 2012-02-13 20:57:12 -0500 | [diff] [blame] | 68 | block = qnx4_block_map(dir, blkofs); |
| 69 | if (block) |
| 70 | bh = sb_bread(dir->i_sb, block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | if (!bh) { |
| 72 | blkofs++; |
| 73 | continue; |
| 74 | } |
| 75 | } |
| 76 | *res_dir = (struct qnx4_inode_entry *) (bh->b_data + offset); |
| 77 | if (qnx4_match(len, name, bh, &offset)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | *ino = block * QNX4_INODES_PER_BLOCK + |
| 79 | (offset / QNX4_DIR_ENTRY_SIZE) - 1; |
| 80 | return bh; |
| 81 | } |
| 82 | if (offset < bh->b_size) { |
| 83 | continue; |
| 84 | } |
| 85 | brelse(bh); |
| 86 | bh = NULL; |
| 87 | offset = 0; |
| 88 | blkofs++; |
| 89 | } |
| 90 | brelse(bh); |
| 91 | *res_dir = NULL; |
| 92 | return NULL; |
| 93 | } |
| 94 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 95 | struct dentry * qnx4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | { |
| 97 | int ino; |
| 98 | struct qnx4_inode_entry *de; |
| 99 | struct qnx4_link_info *lnk; |
| 100 | struct buffer_head *bh; |
| 101 | const char *name = dentry->d_name.name; |
| 102 | int len = dentry->d_name.len; |
| 103 | struct inode *foundinode = NULL; |
| 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | if (!(bh = qnx4_find_entry(len, dir, name, &de, &ino))) |
| 106 | goto out; |
| 107 | /* The entry is linked, let's get the real info */ |
| 108 | if ((de->di_status & QNX4_FILE_LINK) == QNX4_FILE_LINK) { |
| 109 | lnk = (struct qnx4_link_info *) de; |
| 110 | ino = (le32_to_cpu(lnk->dl_inode_blk) - 1) * |
| 111 | QNX4_INODES_PER_BLOCK + |
| 112 | lnk->dl_inode_ndx; |
| 113 | } |
| 114 | brelse(bh); |
| 115 | |
David Howells | 2b7e5bc | 2008-02-07 00:15:45 -0800 | [diff] [blame] | 116 | foundinode = qnx4_iget(dir->i_sb, ino); |
Al Viro | b135dce | 2018-04-30 20:02:31 -0400 | [diff] [blame] | 117 | if (IS_ERR(foundinode)) |
Anders Larsen | 891ddb9 | 2009-09-26 20:15:09 +0200 | [diff] [blame] | 118 | QNX4DEBUG((KERN_ERR "qnx4: lookup->iget -> error %ld\n", |
David Howells | 2b7e5bc | 2008-02-07 00:15:45 -0800 | [diff] [blame] | 119 | PTR_ERR(foundinode))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | out: |
Al Viro | b135dce | 2018-04-30 20:02:31 -0400 | [diff] [blame] | 121 | return d_splice_alias(foundinode, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |