Loading...
1/*
2 * truncate.c
3 *
4 * PURPOSE
5 * Truncate handling routines for the OSTA-UDF(tm) filesystem.
6 *
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/buffer_head.h>
26
27#include "udf_i.h"
28#include "udf_sb.h"
29
30static void extent_trunc(struct inode *inode, struct extent_position *epos,
31 struct kernel_lb_addr *eloc, int8_t etype, uint32_t elen,
32 uint32_t nelen)
33{
34 struct kernel_lb_addr neloc = {};
35 int last_block = (elen + inode->i_sb->s_blocksize - 1) >>
36 inode->i_sb->s_blocksize_bits;
37 int first_block = (nelen + inode->i_sb->s_blocksize - 1) >>
38 inode->i_sb->s_blocksize_bits;
39
40 if (nelen) {
41 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
42 udf_free_blocks(inode->i_sb, inode, eloc, 0,
43 last_block);
44 etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30);
45 } else
46 neloc = *eloc;
47 nelen = (etype << 30) | nelen;
48 }
49
50 if (elen != nelen) {
51 udf_write_aext(inode, epos, &neloc, nelen, 0);
52 if (last_block - first_block > 0) {
53 if (etype == (EXT_RECORDED_ALLOCATED >> 30))
54 mark_inode_dirty(inode);
55
56 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
57 udf_free_blocks(inode->i_sb, inode, eloc,
58 first_block,
59 last_block - first_block);
60 }
61 }
62}
63
64/*
65 * Truncate the last extent to match i_size. This function assumes
66 * that preallocation extent is already truncated.
67 */
68void udf_truncate_tail_extent(struct inode *inode)
69{
70 struct extent_position epos = {};
71 struct kernel_lb_addr eloc;
72 uint32_t elen, nelen;
73 uint64_t lbcount = 0;
74 int8_t etype = -1, netype;
75 int adsize;
76 struct udf_inode_info *iinfo = UDF_I(inode);
77
78 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
79 inode->i_size == iinfo->i_lenExtents)
80 return;
81 /* Are we going to delete the file anyway? */
82 if (inode->i_nlink == 0)
83 return;
84
85 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
86 adsize = sizeof(struct short_ad);
87 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
88 adsize = sizeof(struct long_ad);
89 else
90 BUG();
91
92 /* Find the last extent in the file */
93 while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
94 etype = netype;
95 lbcount += elen;
96 if (lbcount > inode->i_size) {
97 if (lbcount - inode->i_size >= inode->i_sb->s_blocksize)
98 udf_warn(inode->i_sb,
99 "Too long extent after EOF in inode %u: i_size: %lld lbcount: %lld extent %u+%u\n",
100 (unsigned)inode->i_ino,
101 (long long)inode->i_size,
102 (long long)lbcount,
103 (unsigned)eloc.logicalBlockNum,
104 (unsigned)elen);
105 nelen = elen - (lbcount - inode->i_size);
106 epos.offset -= adsize;
107 extent_trunc(inode, &epos, &eloc, etype, elen, nelen);
108 epos.offset += adsize;
109 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) != -1)
110 udf_err(inode->i_sb,
111 "Extent after EOF in inode %u\n",
112 (unsigned)inode->i_ino);
113 break;
114 }
115 }
116 /* This inode entry is in-memory only and thus we don't have to mark
117 * the inode dirty */
118 iinfo->i_lenExtents = inode->i_size;
119 brelse(epos.bh);
120}
121
122void udf_discard_prealloc(struct inode *inode)
123{
124 struct extent_position epos = { NULL, 0, {0, 0} };
125 struct kernel_lb_addr eloc;
126 uint32_t elen;
127 uint64_t lbcount = 0;
128 int8_t etype = -1, netype;
129 int adsize;
130 struct udf_inode_info *iinfo = UDF_I(inode);
131
132 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
133 inode->i_size == iinfo->i_lenExtents)
134 return;
135
136 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
137 adsize = sizeof(struct short_ad);
138 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
139 adsize = sizeof(struct long_ad);
140 else
141 adsize = 0;
142
143 epos.block = iinfo->i_location;
144
145 /* Find the last extent in the file */
146 while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
147 etype = netype;
148 lbcount += elen;
149 }
150 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
151 epos.offset -= adsize;
152 lbcount -= elen;
153 extent_trunc(inode, &epos, &eloc, etype, elen, 0);
154 if (!epos.bh) {
155 iinfo->i_lenAlloc =
156 epos.offset -
157 udf_file_entry_alloc_offset(inode);
158 mark_inode_dirty(inode);
159 } else {
160 struct allocExtDesc *aed =
161 (struct allocExtDesc *)(epos.bh->b_data);
162 aed->lengthAllocDescs =
163 cpu_to_le32(epos.offset -
164 sizeof(struct allocExtDesc));
165 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
166 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
167 udf_update_tag(epos.bh->b_data, epos.offset);
168 else
169 udf_update_tag(epos.bh->b_data,
170 sizeof(struct allocExtDesc));
171 mark_buffer_dirty_inode(epos.bh, inode);
172 }
173 }
174 /* This inode entry is in-memory only and thus we don't have to mark
175 * the inode dirty */
176 iinfo->i_lenExtents = lbcount;
177 brelse(epos.bh);
178}
179
180static void udf_update_alloc_ext_desc(struct inode *inode,
181 struct extent_position *epos,
182 u32 lenalloc)
183{
184 struct super_block *sb = inode->i_sb;
185 struct udf_sb_info *sbi = UDF_SB(sb);
186
187 struct allocExtDesc *aed = (struct allocExtDesc *) (epos->bh->b_data);
188 int len = sizeof(struct allocExtDesc);
189
190 aed->lengthAllocDescs = cpu_to_le32(lenalloc);
191 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) || sbi->s_udfrev >= 0x0201)
192 len += lenalloc;
193
194 udf_update_tag(epos->bh->b_data, len);
195 mark_buffer_dirty_inode(epos->bh, inode);
196}
197
198/*
199 * Truncate extents of inode to inode->i_size. This function can be used only
200 * for making file shorter. For making file longer, udf_extend_file() has to
201 * be used.
202 */
203void udf_truncate_extents(struct inode *inode)
204{
205 struct extent_position epos;
206 struct kernel_lb_addr eloc, neloc = {};
207 uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
208 int8_t etype;
209 struct super_block *sb = inode->i_sb;
210 sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset;
211 loff_t byte_offset;
212 int adsize;
213 struct udf_inode_info *iinfo = UDF_I(inode);
214
215 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
216 adsize = sizeof(struct short_ad);
217 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
218 adsize = sizeof(struct long_ad);
219 else
220 BUG();
221
222 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
223 byte_offset = (offset << sb->s_blocksize_bits) +
224 (inode->i_size & (sb->s_blocksize - 1));
225 if (etype == -1) {
226 /* We should extend the file? */
227 WARN_ON(byte_offset);
228 return;
229 }
230 epos.offset -= adsize;
231 extent_trunc(inode, &epos, &eloc, etype, elen, byte_offset);
232 epos.offset += adsize;
233 if (byte_offset)
234 lenalloc = epos.offset;
235 else
236 lenalloc = epos.offset - adsize;
237
238 if (!epos.bh)
239 lenalloc -= udf_file_entry_alloc_offset(inode);
240 else
241 lenalloc -= sizeof(struct allocExtDesc);
242
243 while ((etype = udf_current_aext(inode, &epos, &eloc,
244 &elen, 0)) != -1) {
245 if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
246 udf_write_aext(inode, &epos, &neloc, nelen, 0);
247 if (indirect_ext_len) {
248 /* We managed to free all extents in the
249 * indirect extent - free it too */
250 BUG_ON(!epos.bh);
251 udf_free_blocks(sb, NULL, &epos.block,
252 0, indirect_ext_len);
253 } else if (!epos.bh) {
254 iinfo->i_lenAlloc = lenalloc;
255 mark_inode_dirty(inode);
256 } else
257 udf_update_alloc_ext_desc(inode,
258 &epos, lenalloc);
259 brelse(epos.bh);
260 epos.offset = sizeof(struct allocExtDesc);
261 epos.block = eloc;
262 epos.bh = udf_tread(sb,
263 udf_get_lb_pblock(sb, &eloc, 0));
264 if (elen)
265 indirect_ext_len =
266 (elen + sb->s_blocksize - 1) >>
267 sb->s_blocksize_bits;
268 else
269 indirect_ext_len = 1;
270 } else {
271 extent_trunc(inode, &epos, &eloc, etype, elen, 0);
272 epos.offset += adsize;
273 }
274 }
275
276 if (indirect_ext_len) {
277 BUG_ON(!epos.bh);
278 udf_free_blocks(sb, NULL, &epos.block, 0, indirect_ext_len);
279 } else if (!epos.bh) {
280 iinfo->i_lenAlloc = lenalloc;
281 mark_inode_dirty(inode);
282 } else
283 udf_update_alloc_ext_desc(inode, &epos, lenalloc);
284 iinfo->i_lenExtents = inode->i_size;
285
286 brelse(epos.bh);
287}
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * truncate.c
4 *
5 * PURPOSE
6 * Truncate handling routines for the OSTA-UDF(tm) filesystem.
7 *
8 * COPYRIGHT
9 * (C) 1999-2004 Ben Fennema
10 * (C) 1999 Stelias Computing Inc
11 *
12 * HISTORY
13 *
14 * 02/24/99 blf Created.
15 *
16 */
17
18#include "udfdecl.h"
19#include <linux/fs.h>
20#include <linux/mm.h>
21
22#include "udf_i.h"
23#include "udf_sb.h"
24
25static void extent_trunc(struct inode *inode, struct extent_position *epos,
26 struct kernel_lb_addr *eloc, int8_t etype, uint32_t elen,
27 uint32_t nelen)
28{
29 struct kernel_lb_addr neloc = {};
30 int last_block = (elen + inode->i_sb->s_blocksize - 1) >>
31 inode->i_sb->s_blocksize_bits;
32 int first_block = (nelen + inode->i_sb->s_blocksize - 1) >>
33 inode->i_sb->s_blocksize_bits;
34
35 if (nelen) {
36 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
37 udf_free_blocks(inode->i_sb, inode, eloc, 0,
38 last_block);
39 etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30);
40 } else
41 neloc = *eloc;
42 nelen = (etype << 30) | nelen;
43 }
44
45 if (elen != nelen) {
46 udf_write_aext(inode, epos, &neloc, nelen, 0);
47 if (last_block > first_block) {
48 if (etype == (EXT_RECORDED_ALLOCATED >> 30))
49 mark_inode_dirty(inode);
50
51 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
52 udf_free_blocks(inode->i_sb, inode, eloc,
53 first_block,
54 last_block - first_block);
55 }
56 }
57}
58
59/*
60 * Truncate the last extent to match i_size. This function assumes
61 * that preallocation extent is already truncated.
62 */
63void udf_truncate_tail_extent(struct inode *inode)
64{
65 struct extent_position epos = {};
66 struct kernel_lb_addr eloc;
67 uint32_t elen, nelen;
68 uint64_t lbcount = 0;
69 int8_t etype = -1, netype;
70 int adsize;
71 struct udf_inode_info *iinfo = UDF_I(inode);
72
73 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
74 inode->i_size == iinfo->i_lenExtents)
75 return;
76 /* Are we going to delete the file anyway? */
77 if (inode->i_nlink == 0)
78 return;
79
80 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
81 adsize = sizeof(struct short_ad);
82 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
83 adsize = sizeof(struct long_ad);
84 else
85 BUG();
86
87 /* Find the last extent in the file */
88 while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
89 etype = netype;
90 lbcount += elen;
91 if (lbcount > inode->i_size) {
92 if (lbcount - inode->i_size >= inode->i_sb->s_blocksize)
93 udf_warn(inode->i_sb,
94 "Too long extent after EOF in inode %u: i_size: %lld lbcount: %lld extent %u+%u\n",
95 (unsigned)inode->i_ino,
96 (long long)inode->i_size,
97 (long long)lbcount,
98 (unsigned)eloc.logicalBlockNum,
99 (unsigned)elen);
100 nelen = elen - (lbcount - inode->i_size);
101 epos.offset -= adsize;
102 extent_trunc(inode, &epos, &eloc, etype, elen, nelen);
103 epos.offset += adsize;
104 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) != -1)
105 udf_err(inode->i_sb,
106 "Extent after EOF in inode %u\n",
107 (unsigned)inode->i_ino);
108 break;
109 }
110 }
111 /* This inode entry is in-memory only and thus we don't have to mark
112 * the inode dirty */
113 iinfo->i_lenExtents = inode->i_size;
114 brelse(epos.bh);
115}
116
117void udf_discard_prealloc(struct inode *inode)
118{
119 struct extent_position epos = {};
120 struct extent_position prev_epos = {};
121 struct kernel_lb_addr eloc;
122 uint32_t elen;
123 uint64_t lbcount = 0;
124 int8_t etype = -1;
125 struct udf_inode_info *iinfo = UDF_I(inode);
126 int bsize = i_blocksize(inode);
127
128 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
129 ALIGN(inode->i_size, bsize) == ALIGN(iinfo->i_lenExtents, bsize))
130 return;
131
132 epos.block = iinfo->i_location;
133
134 /* Find the last extent in the file */
135 while (udf_next_aext(inode, &epos, &eloc, &elen, 0) != -1) {
136 brelse(prev_epos.bh);
137 prev_epos = epos;
138 if (prev_epos.bh)
139 get_bh(prev_epos.bh);
140
141 etype = udf_next_aext(inode, &epos, &eloc, &elen, 1);
142 lbcount += elen;
143 }
144 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
145 lbcount -= elen;
146 udf_delete_aext(inode, prev_epos);
147 udf_free_blocks(inode->i_sb, inode, &eloc, 0,
148 DIV_ROUND_UP(elen, bsize));
149 }
150 /* This inode entry is in-memory only and thus we don't have to mark
151 * the inode dirty */
152 iinfo->i_lenExtents = lbcount;
153 brelse(epos.bh);
154 brelse(prev_epos.bh);
155}
156
157static void udf_update_alloc_ext_desc(struct inode *inode,
158 struct extent_position *epos,
159 u32 lenalloc)
160{
161 struct super_block *sb = inode->i_sb;
162 struct udf_sb_info *sbi = UDF_SB(sb);
163
164 struct allocExtDesc *aed = (struct allocExtDesc *) (epos->bh->b_data);
165 int len = sizeof(struct allocExtDesc);
166
167 aed->lengthAllocDescs = cpu_to_le32(lenalloc);
168 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) || sbi->s_udfrev >= 0x0201)
169 len += lenalloc;
170
171 udf_update_tag(epos->bh->b_data, len);
172 mark_buffer_dirty_inode(epos->bh, inode);
173}
174
175/*
176 * Truncate extents of inode to inode->i_size. This function can be used only
177 * for making file shorter. For making file longer, udf_extend_file() has to
178 * be used.
179 */
180int udf_truncate_extents(struct inode *inode)
181{
182 struct extent_position epos;
183 struct kernel_lb_addr eloc, neloc = {};
184 uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
185 int8_t etype;
186 struct super_block *sb = inode->i_sb;
187 sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset;
188 loff_t byte_offset;
189 int adsize;
190 struct udf_inode_info *iinfo = UDF_I(inode);
191
192 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
193 adsize = sizeof(struct short_ad);
194 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
195 adsize = sizeof(struct long_ad);
196 else
197 BUG();
198
199 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
200 byte_offset = (offset << sb->s_blocksize_bits) +
201 (inode->i_size & (sb->s_blocksize - 1));
202 if (etype == -1) {
203 /* We should extend the file? */
204 WARN_ON(byte_offset);
205 return 0;
206 }
207 epos.offset -= adsize;
208 extent_trunc(inode, &epos, &eloc, etype, elen, byte_offset);
209 epos.offset += adsize;
210 if (byte_offset)
211 lenalloc = epos.offset;
212 else
213 lenalloc = epos.offset - adsize;
214
215 if (!epos.bh)
216 lenalloc -= udf_file_entry_alloc_offset(inode);
217 else
218 lenalloc -= sizeof(struct allocExtDesc);
219
220 while ((etype = udf_current_aext(inode, &epos, &eloc,
221 &elen, 0)) != -1) {
222 if (etype == (EXT_NEXT_EXTENT_ALLOCDESCS >> 30)) {
223 udf_write_aext(inode, &epos, &neloc, nelen, 0);
224 if (indirect_ext_len) {
225 /* We managed to free all extents in the
226 * indirect extent - free it too */
227 BUG_ON(!epos.bh);
228 udf_free_blocks(sb, NULL, &epos.block,
229 0, indirect_ext_len);
230 } else if (!epos.bh) {
231 iinfo->i_lenAlloc = lenalloc;
232 mark_inode_dirty(inode);
233 } else
234 udf_update_alloc_ext_desc(inode,
235 &epos, lenalloc);
236 brelse(epos.bh);
237 epos.offset = sizeof(struct allocExtDesc);
238 epos.block = eloc;
239 epos.bh = sb_bread(sb,
240 udf_get_lb_pblock(sb, &eloc, 0));
241 /* Error reading indirect block? */
242 if (!epos.bh)
243 return -EIO;
244 if (elen)
245 indirect_ext_len =
246 (elen + sb->s_blocksize - 1) >>
247 sb->s_blocksize_bits;
248 else
249 indirect_ext_len = 1;
250 } else {
251 extent_trunc(inode, &epos, &eloc, etype, elen, 0);
252 epos.offset += adsize;
253 }
254 }
255
256 if (indirect_ext_len) {
257 BUG_ON(!epos.bh);
258 udf_free_blocks(sb, NULL, &epos.block, 0, indirect_ext_len);
259 } else if (!epos.bh) {
260 iinfo->i_lenAlloc = lenalloc;
261 mark_inode_dirty(inode);
262 } else
263 udf_update_alloc_ext_desc(inode, &epos, lenalloc);
264 iinfo->i_lenExtents = inode->i_size;
265
266 brelse(epos.bh);
267 return 0;
268}