Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * linux/fs/ufs/util.c | |
4 | * | |
5 | * Copyright (C) 1998 | |
6 | * Daniel Pirkl <daniel.pirkl@email.cz> | |
7 | * Charles University, Faculty of Mathematics and Physics | |
8 | */ | |
9 | ||
10 | #include <linux/string.h> | |
11 | #include <linux/slab.h> | |
1da177e4 LT |
12 | #include <linux/buffer_head.h> |
13 | ||
e5420598 | 14 | #include "ufs_fs.h" |
bcd6d4ec | 15 | #include "ufs.h" |
1da177e4 LT |
16 | #include "swab.h" |
17 | #include "util.h" | |
18 | ||
1da177e4 LT |
19 | struct ufs_buffer_head * _ubh_bread_ (struct ufs_sb_private_info * uspi, |
20 | struct super_block *sb, u64 fragment, u64 size) | |
21 | { | |
22 | struct ufs_buffer_head * ubh; | |
23 | unsigned i, j ; | |
24 | u64 count = 0; | |
25 | if (size & ~uspi->s_fmask) | |
26 | return NULL; | |
27 | count = size >> uspi->s_fshift; | |
28 | if (count > UFS_MAXFRAG) | |
29 | return NULL; | |
194c8767 | 30 | ubh = kmalloc (sizeof (struct ufs_buffer_head), GFP_NOFS); |
1da177e4 LT |
31 | if (!ubh) |
32 | return NULL; | |
33 | ubh->fragment = fragment; | |
34 | ubh->count = count; | |
35 | for (i = 0; i < count; i++) | |
36 | if (!(ubh->bh[i] = sb_bread(sb, fragment + i))) | |
37 | goto failed; | |
38 | for (; i < UFS_MAXFRAG; i++) | |
39 | ubh->bh[i] = NULL; | |
40 | return ubh; | |
41 | failed: | |
42 | for (j = 0; j < i; j++) | |
43 | brelse (ubh->bh[j]); | |
44 | kfree(ubh); | |
45 | return NULL; | |
46 | } | |
47 | ||
48 | struct ufs_buffer_head * ubh_bread_uspi (struct ufs_sb_private_info * uspi, | |
49 | struct super_block *sb, u64 fragment, u64 size) | |
50 | { | |
51 | unsigned i, j; | |
52 | u64 count = 0; | |
53 | if (size & ~uspi->s_fmask) | |
54 | return NULL; | |
55 | count = size >> uspi->s_fshift; | |
56 | if (count <= 0 || count > UFS_MAXFRAG) | |
57 | return NULL; | |
9695ef16 ED |
58 | USPI_UBH(uspi)->fragment = fragment; |
59 | USPI_UBH(uspi)->count = count; | |
1da177e4 | 60 | for (i = 0; i < count; i++) |
9695ef16 | 61 | if (!(USPI_UBH(uspi)->bh[i] = sb_bread(sb, fragment + i))) |
1da177e4 LT |
62 | goto failed; |
63 | for (; i < UFS_MAXFRAG; i++) | |
9695ef16 ED |
64 | USPI_UBH(uspi)->bh[i] = NULL; |
65 | return USPI_UBH(uspi); | |
1da177e4 LT |
66 | failed: |
67 | for (j = 0; j < i; j++) | |
9695ef16 | 68 | brelse (USPI_UBH(uspi)->bh[j]); |
1da177e4 LT |
69 | return NULL; |
70 | } | |
71 | ||
72 | void ubh_brelse (struct ufs_buffer_head * ubh) | |
73 | { | |
74 | unsigned i; | |
75 | if (!ubh) | |
76 | return; | |
77 | for (i = 0; i < ubh->count; i++) | |
78 | brelse (ubh->bh[i]); | |
79 | kfree (ubh); | |
80 | } | |
81 | ||
82 | void ubh_brelse_uspi (struct ufs_sb_private_info * uspi) | |
83 | { | |
84 | unsigned i; | |
9695ef16 | 85 | if (!USPI_UBH(uspi)) |
1da177e4 | 86 | return; |
9695ef16 ED |
87 | for ( i = 0; i < USPI_UBH(uspi)->count; i++ ) { |
88 | brelse (USPI_UBH(uspi)->bh[i]); | |
89 | USPI_UBH(uspi)->bh[i] = NULL; | |
1da177e4 LT |
90 | } |
91 | } | |
92 | ||
93 | void ubh_mark_buffer_dirty (struct ufs_buffer_head * ubh) | |
94 | { | |
95 | unsigned i; | |
96 | if (!ubh) | |
97 | return; | |
98 | for ( i = 0; i < ubh->count; i++ ) | |
99 | mark_buffer_dirty (ubh->bh[i]); | |
100 | } | |
101 | ||
9cb569d6 | 102 | void ubh_sync_block(struct ufs_buffer_head *ubh) |
1da177e4 | 103 | { |
9cb569d6 CH |
104 | if (ubh) { |
105 | unsigned i; | |
098d5af7 | 106 | |
9cb569d6 | 107 | for (i = 0; i < ubh->count; i++) |
2a222ca9 | 108 | write_dirty_buffer(ubh->bh[i], 0); |
1da177e4 | 109 | |
9cb569d6 CH |
110 | for (i = 0; i < ubh->count; i++) |
111 | wait_on_buffer(ubh->bh[i]); | |
112 | } | |
1da177e4 LT |
113 | } |
114 | ||
1da177e4 LT |
115 | void ubh_bforget (struct ufs_buffer_head * ubh) |
116 | { | |
117 | unsigned i; | |
118 | if (!ubh) | |
119 | return; | |
120 | for ( i = 0; i < ubh->count; i++ ) if ( ubh->bh[i] ) | |
121 | bforget (ubh->bh[i]); | |
122 | } | |
123 | ||
124 | int ubh_buffer_dirty (struct ufs_buffer_head * ubh) | |
125 | { | |
126 | unsigned i; | |
127 | unsigned result = 0; | |
128 | if (!ubh) | |
129 | return 0; | |
130 | for ( i = 0; i < ubh->count; i++ ) | |
131 | result |= buffer_dirty(ubh->bh[i]); | |
132 | return result; | |
133 | } | |
134 | ||
1da177e4 LT |
135 | dev_t |
136 | ufs_get_inode_dev(struct super_block *sb, struct ufs_inode_info *ufsi) | |
137 | { | |
44aa5359 | 138 | __u32 fs32; |
1da177e4 LT |
139 | dev_t dev; |
140 | ||
141 | if ((UFS_SB(sb)->s_flags & UFS_ST_MASK) == UFS_ST_SUNx86) | |
44aa5359 | 142 | fs32 = fs32_to_cpu(sb, ufsi->i_u1.i_data[1]); |
1da177e4 | 143 | else |
44aa5359 | 144 | fs32 = fs32_to_cpu(sb, ufsi->i_u1.i_data[0]); |
1da177e4 LT |
145 | switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) { |
146 | case UFS_ST_SUNx86: | |
147 | case UFS_ST_SUN: | |
148 | if ((fs32 & 0xffff0000) == 0 || | |
149 | (fs32 & 0xffff0000) == 0xffff0000) | |
150 | dev = old_decode_dev(fs32 & 0x7fff); | |
151 | else | |
152 | dev = MKDEV(sysv_major(fs32), sysv_minor(fs32)); | |
153 | break; | |
154 | ||
155 | default: | |
156 | dev = old_decode_dev(fs32); | |
157 | break; | |
158 | } | |
159 | return dev; | |
160 | } | |
161 | ||
162 | void | |
163 | ufs_set_inode_dev(struct super_block *sb, struct ufs_inode_info *ufsi, dev_t dev) | |
164 | { | |
44aa5359 | 165 | __u32 fs32; |
1da177e4 LT |
166 | |
167 | switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) { | |
168 | case UFS_ST_SUNx86: | |
169 | case UFS_ST_SUN: | |
170 | fs32 = sysv_encode_dev(dev); | |
171 | if ((fs32 & 0xffff8000) == 0) { | |
172 | fs32 = old_encode_dev(dev); | |
173 | } | |
174 | break; | |
175 | ||
176 | default: | |
177 | fs32 = old_encode_dev(dev); | |
178 | break; | |
179 | } | |
1da177e4 | 180 | if ((UFS_SB(sb)->s_flags & UFS_ST_MASK) == UFS_ST_SUNx86) |
44aa5359 | 181 | ufsi->i_u1.i_data[1] = cpu_to_fs32(sb, fs32); |
1da177e4 | 182 | else |
44aa5359 | 183 | ufsi->i_u1.i_data[0] = cpu_to_fs32(sb, fs32); |
1da177e4 | 184 | } |
10e5dce0 ED |
185 | |
186 | /** | |
5fb7bd50 | 187 | * ufs_get_locked_folio() - locate, pin and lock a pagecache folio, if not exist |
10e5dce0 ED |
188 | * read it from disk. |
189 | * @mapping: the address_space to search | |
190 | * @index: the page index | |
191 | * | |
5fb7bd50 | 192 | * Locates the desired pagecache folio, if not exist we'll read it, |
10e5dce0 ED |
193 | * locks it, increments its reference |
194 | * count and returns its address. | |
195 | * | |
196 | */ | |
5fb7bd50 | 197 | struct folio *ufs_get_locked_folio(struct address_space *mapping, |
10e5dce0 ED |
198 | pgoff_t index) |
199 | { | |
267309f3 | 200 | struct inode *inode = mapping->host; |
5fb7bd50 | 201 | struct folio *folio = filemap_lock_folio(mapping, index); |
485053bb | 202 | if (IS_ERR(folio)) { |
5fb7bd50 | 203 | folio = read_mapping_folio(mapping, index, NULL); |
1fb32b7b | 204 | |
5fb7bd50 MWO |
205 | if (IS_ERR(folio)) { |
206 | printk(KERN_ERR "ufs_change_blocknr: read_mapping_folio error: ino %lu, index: %lu\n", | |
10e5dce0 | 207 | mapping->host->i_ino, index); |
5fb7bd50 | 208 | return folio; |
10e5dce0 ED |
209 | } |
210 | ||
5fb7bd50 | 211 | folio_lock(folio); |
10e5dce0 | 212 | |
5fb7bd50 | 213 | if (unlikely(folio->mapping == NULL)) { |
1fb32b7b | 214 | /* Truncate got there first */ |
5fb7bd50 MWO |
215 | folio_unlock(folio); |
216 | folio_put(folio); | |
267309f3 | 217 | return NULL; |
1fb32b7b | 218 | } |
10e5dce0 | 219 | } |
5fb7bd50 | 220 | if (!folio_buffers(folio)) |
0a88810d | 221 | create_empty_buffers(folio, 1 << inode->i_blkbits, 0); |
5fb7bd50 | 222 | return folio; |
10e5dce0 | 223 | } |