udf: fix sparse warnings (shadowing & mismatch between declaration and definition)
[deliverable/linux.git] / fs / udf / truncate.c
CommitLineData
1da177e4
LT
1/*
2 * truncate.c
3 *
4 * PURPOSE
5 * Truncate handling routines for the OSTA-UDF(tm) filesystem.
6 *
1da177e4
LT
7 * COPYRIGHT
8 * This file is distributed under the terms of the GNU General Public
9 * License (GPL). Copies of the GPL can be obtained from:
10 * ftp://prep.ai.mit.edu/pub/gnu/GPL
11 * Each contributing author retains all rights to their own work.
12 *
13 * (C) 1999-2004 Ben Fennema
14 * (C) 1999 Stelias Computing Inc
15 *
16 * HISTORY
17 *
18 * 02/24/99 blf Created.
19 *
20 */
21
22#include "udfdecl.h"
23#include <linux/fs.h>
24#include <linux/mm.h>
25#include <linux/udf_fs.h>
26#include <linux/buffer_head.h>
27
28#include "udf_i.h"
29#include "udf_sb.h"
30
cb00ea35
CG
31static void extent_trunc(struct inode *inode, struct extent_position *epos,
32 kernel_lb_addr eloc, int8_t etype, uint32_t elen,
33 uint32_t nelen)
1da177e4 34{
28de7948
CG
35 kernel_lb_addr neloc = {};
36 int last_block = (elen + inode->i_sb->s_blocksize - 1) >>
37 inode->i_sb->s_blocksize_bits;
38 int first_block = (nelen + inode->i_sb->s_blocksize - 1) >>
39 inode->i_sb->s_blocksize_bits;
1da177e4 40
cb00ea35
CG
41 if (nelen) {
42 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
43 udf_free_blocks(inode->i_sb, inode, eloc, 0,
44 last_block);
1da177e4 45 etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30);
cb00ea35 46 } else
1da177e4
LT
47 neloc = eloc;
48 nelen = (etype << 30) | nelen;
49 }
50
cb00ea35 51 if (elen != nelen) {
ff116fc8 52 udf_write_aext(inode, epos, neloc, nelen, 0);
cb00ea35 53 if (last_block - first_block > 0) {
1da177e4
LT
54 if (etype == (EXT_RECORDED_ALLOCATED >> 30))
55 mark_inode_dirty(inode);
56
57 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
cb00ea35
CG
58 udf_free_blocks(inode->i_sb, inode, eloc,
59 first_block,
60 last_block - first_block);
1da177e4
LT
61 }
62 }
63}
64
74584ae5
JK
65/*
66 * Truncate the last extent to match i_size. This function assumes
67 * that preallocation extent is already truncated.
68 */
69void udf_truncate_tail_extent(struct inode *inode)
1da177e4 70{
28de7948 71 struct extent_position epos = {};
ff116fc8
JK
72 kernel_lb_addr eloc;
73 uint32_t elen, nelen;
1da177e4
LT
74 uint64_t lbcount = 0;
75 int8_t etype = -1, netype;
1da177e4
LT
76 int adsize;
77
78 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB ||
74584ae5
JK
79 inode->i_size == UDF_I_LENEXTENTS(inode))
80 return;
81 /* Are we going to delete the file anyway? */
82 if (inode->i_nlink == 0)
1da177e4 83 return;
1da177e4
LT
84
85 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
86 adsize = sizeof(short_ad);
87 else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
88 adsize = sizeof(long_ad);
89 else
74584ae5 90 BUG();
1da177e4 91
ff116fc8 92 /* Find the last extent in the file */
cb00ea35 93 while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
1da177e4
LT
94 etype = netype;
95 lbcount += elen;
74584ae5
JK
96 if (lbcount > inode->i_size) {
97 if (lbcount - inode->i_size >= inode->i_sb->s_blocksize)
98 printk(KERN_WARNING
99 "udf_truncate_tail_extent(): Too long "
100 "extent after EOF in inode %u: i_size: "
101 "%Ld lbcount: %Ld extent %u+%u\n",
102 (unsigned)inode->i_ino,
103 (long long)inode->i_size,
104 (long long)lbcount,
105 (unsigned)eloc.logicalBlockNum,
106 (unsigned)elen);
1da177e4 107 nelen = elen - (lbcount - inode->i_size);
ff116fc8
JK
108 epos.offset -= adsize;
109 extent_trunc(inode, &epos, eloc, etype, elen, nelen);
110 epos.offset += adsize;
74584ae5
JK
111 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) != -1)
112 printk(KERN_ERR "udf_truncate_tail_extent(): "
113 "Extent after EOF in inode %u.\n",
114 (unsigned)inode->i_ino);
115 break;
1da177e4
LT
116 }
117 }
74584ae5
JK
118 /* This inode entry is in-memory only and thus we don't have to mark
119 * the inode dirty */
120 UDF_I_LENEXTENTS(inode) = inode->i_size;
121 brelse(epos.bh);
122}
123
124void udf_discard_prealloc(struct inode *inode)
125{
cb00ea35 126 struct extent_position epos = { NULL, 0, {0, 0} };
74584ae5
JK
127 kernel_lb_addr eloc;
128 uint32_t elen;
129 uint64_t lbcount = 0;
130 int8_t etype = -1, netype;
131 int adsize;
132
133 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB ||
cb00ea35 134 inode->i_size == UDF_I_LENEXTENTS(inode))
74584ae5
JK
135 return;
136
137 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
138 adsize = sizeof(short_ad);
139 else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
140 adsize = sizeof(long_ad);
141 else
142 adsize = 0;
143
144 epos.block = UDF_I_LOCATION(inode);
145
146 /* Find the last extent in the file */
147 while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
148 etype = netype;
149 lbcount += elen;
150 }
ff116fc8
JK
151 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
152 epos.offset -= adsize;
1da177e4 153 lbcount -= elen;
ff116fc8 154 extent_trunc(inode, &epos, eloc, etype, elen, 0);
74584ae5 155 if (!epos.bh) {
cb00ea35 156 UDF_I_LENALLOC(inode) =
28de7948 157 epos.offset - udf_file_entry_alloc_offset(inode);
1da177e4 158 mark_inode_dirty(inode);
74584ae5 159 } else {
cb00ea35 160 struct allocExtDesc *aed =
28de7948 161 (struct allocExtDesc *)(epos.bh->b_data);
cb00ea35 162 aed->lengthAllocDescs =
28de7948
CG
163 cpu_to_le32(epos.offset -
164 sizeof(struct allocExtDesc));
165 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
6c79e987 166 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
ff116fc8 167 udf_update_tag(epos.bh->b_data, epos.offset);
1da177e4 168 else
cb00ea35
CG
169 udf_update_tag(epos.bh->b_data,
170 sizeof(struct allocExtDesc));
ff116fc8 171 mark_buffer_dirty_inode(epos.bh, inode);
1da177e4
LT
172 }
173 }
74584ae5
JK
174 /* This inode entry is in-memory only and thus we don't have to mark
175 * the inode dirty */
1da177e4 176 UDF_I_LENEXTENTS(inode) = lbcount;
3bf25cb4 177 brelse(epos.bh);
1da177e4
LT
178}
179
cb00ea35 180void udf_truncate_extents(struct inode *inode)
1da177e4 181{
ff116fc8 182 struct extent_position epos;
28de7948 183 kernel_lb_addr eloc, neloc = {};
ff116fc8 184 uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
1da177e4 185 int8_t etype;
31170b6a 186 struct super_block *sb = inode->i_sb;
6c79e987 187 struct udf_sb_info *sbi = UDF_SB(sb);
31170b6a 188 sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset;
60448b1d 189 loff_t byte_offset;
1da177e4
LT
190 int adsize;
191
192 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
193 adsize = sizeof(short_ad);
194 else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
195 adsize = sizeof(long_ad);
196 else
ff116fc8 197 BUG();
1da177e4 198
ff116fc8 199 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
28de7948
CG
200 byte_offset = (offset << sb->s_blocksize_bits) +
201 (inode->i_size & (sb->s_blocksize - 1));
cb00ea35 202 if (etype != -1) {
ff116fc8
JK
203 epos.offset -= adsize;
204 extent_trunc(inode, &epos, eloc, etype, elen, byte_offset);
205 epos.offset += adsize;
60448b1d 206 if (byte_offset)
ff116fc8 207 lenalloc = epos.offset;
1da177e4 208 else
ff116fc8 209 lenalloc = epos.offset - adsize;
1da177e4 210
ff116fc8 211 if (!epos.bh)
1da177e4
LT
212 lenalloc -= udf_file_entry_alloc_offset(inode);
213 else
214 lenalloc -= sizeof(struct allocExtDesc);
215
28de7948 216 while ((etype = udf_current_aext(inode, &epos, &eloc, &elen, 0)) != -1) {
cb00ea35 217 if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
ff116fc8 218 udf_write_aext(inode, &epos, neloc, nelen, 0);
cb00ea35 219 if (indirect_ext_len) {
ff116fc8
JK
220 /* We managed to free all extents in the
221 * indirect extent - free it too */
222 if (!epos.bh)
1da177e4 223 BUG();
cb00ea35
CG
224 udf_free_blocks(sb, inode, epos.block,
225 0, indirect_ext_len);
226 } else {
227 if (!epos.bh) {
28de7948 228 UDF_I_LENALLOC(inode) = lenalloc;
1da177e4 229 mark_inode_dirty(inode);
cb00ea35
CG
230 } else {
231 struct allocExtDesc *aed =
28de7948 232 (struct allocExtDesc *)(epos.bh->b_data);
cb00ea35
CG
233 aed->lengthAllocDescs =
234 cpu_to_le32(lenalloc);
28de7948 235 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) ||
6c79e987 236 sbi->s_udfrev >= 0x0201)
28de7948
CG
237 udf_update_tag(epos.bh->b_data,
238 lenalloc +
239 sizeof(struct allocExtDesc));
1da177e4 240 else
28de7948
CG
241 udf_update_tag(epos.bh->b_data,
242 sizeof(struct allocExtDesc));
243 mark_buffer_dirty_inode(epos.bh, inode);
1da177e4
LT
244 }
245 }
ff116fc8
JK
246 brelse(epos.bh);
247 epos.offset = sizeof(struct allocExtDesc);
248 epos.block = eloc;
28de7948 249 epos.bh = udf_tread(sb, udf_get_lb_pblock(sb, eloc, 0));
1da177e4 250 if (elen)
28de7948
CG
251 indirect_ext_len = (elen + sb->s_blocksize -1) >>
252 sb->s_blocksize_bits;
1da177e4 253 else
ff116fc8 254 indirect_ext_len = 1;
cb00ea35 255 } else {
28de7948 256 extent_trunc(inode, &epos, eloc, etype, elen, 0);
ff116fc8 257 epos.offset += adsize;
1da177e4
LT
258 }
259 }
260
cb00ea35 261 if (indirect_ext_len) {
ff116fc8 262 if (!epos.bh)
1da177e4 263 BUG();
cb00ea35
CG
264 udf_free_blocks(sb, inode, epos.block, 0,
265 indirect_ext_len);
266 } else {
267 if (!epos.bh) {
1da177e4
LT
268 UDF_I_LENALLOC(inode) = lenalloc;
269 mark_inode_dirty(inode);
cb00ea35
CG
270 } else {
271 struct allocExtDesc *aed =
272 (struct allocExtDesc *)(epos.bh->b_data);
1da177e4 273 aed->lengthAllocDescs = cpu_to_le32(lenalloc);
28de7948 274 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) ||
6c79e987 275 sbi->s_udfrev >= 0x0201)
cb00ea35 276 udf_update_tag(epos.bh->b_data,
28de7948 277 lenalloc + sizeof(struct allocExtDesc));
1da177e4 278 else
cb00ea35 279 udf_update_tag(epos.bh->b_data,
28de7948 280 sizeof(struct allocExtDesc));
ff116fc8 281 mark_buffer_dirty_inode(epos.bh, inode);
1da177e4
LT
282 }
283 }
cb00ea35
CG
284 } else if (inode->i_size) {
285 if (byte_offset) {
31170b6a
JK
286 kernel_long_ad extent;
287
00a2b0f6
JK
288 /*
289 * OK, there is not extent covering inode->i_size and
290 * no extent above inode->i_size => truncate is
31170b6a 291 * extending the file by 'offset' blocks.
00a2b0f6 292 */
28de7948
CG
293 if ((!epos.bh &&
294 epos.offset == udf_file_entry_alloc_offset(inode)) ||
295 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
31170b6a
JK
296 /* File has no extents at all or has empty last
297 * indirect extent! Create a fake extent... */
298 extent.extLocation.logicalBlockNum = 0;
299 extent.extLocation.partitionReferenceNum = 0;
28de7948 300 extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
cb00ea35 301 } else {
ff116fc8 302 epos.offset -= adsize;
31170b6a 303 etype = udf_next_aext(inode, &epos,
cb00ea35
CG
304 &extent.extLocation,
305 &extent.extLength, 0);
31170b6a 306 extent.extLength |= etype << 30;
1da177e4 307 }
cb00ea35 308 udf_extend_file(inode, &epos, &extent,
28de7948 309 offset + ((inode->i_size & (sb->s_blocksize - 1)) != 0));
1da177e4
LT
310 }
311 }
312 UDF_I_LENEXTENTS(inode) = inode->i_size;
313
3bf25cb4 314 brelse(epos.bh);
1da177e4 315}
This page took 0.492049 seconds and 5 git commands to generate.