-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfat32_file.c
More file actions
350 lines (308 loc) · 11 KB
/
fat32_file.c
File metadata and controls
350 lines (308 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include "fat32fs.h"
/*
* fat32_truncate - Truncate or extend a file/directory
*
* Adjusts the cluster chain to match the requested size.
* For truncation: frees clusters beyond the new size.
* For extension: allocates additional clusters as needed.
*
* Parameters:
* fv - FAT32 vnode to truncate/extend
* size - New size in bytes
*
* Returns:
* 0 on success, error code on failure
*
* Notes:
* - For directories, fv_size must remain 0 (not updated)
* - For directories, new clusters are cleared (NC_CLEAR)
* - Size 0 means delete all clusters
*/
int
fat32_truncate(fat32fs_vnode_t *fv, off_t size)
{
fat32fs_info_t *fsi = fv->fv_fsi;
uint32_t needed_clusters;
uint32_t cluster;
uint32_t next_cluster;
unsigned int flags;
int error;
#ifdef FAT32_DBG_CLUSTERS
cmn_err(CE_NOTE, "fat32_truncate: cluster=%u old_size=%lld new_size=%lld type=%d",
fv->fv_cluster, fv->fv_size, size, fv->fv_type);
#endif
/* Calculate how many clusters we need */
if (size == 0) {
needed_clusters = 0;
} else {
needed_clusters = (uint32_t)((size + fsi->fsi_bytes_per_cluster - 1) >> fsi->fsi_cluster_shift);
}
/* Special case: truncate to zero */
if (needed_clusters == 0) {
if (fv->fv_cluster >= FAT32_CLUSTER_MIN) {
/* Free entire cluster chain */
error = fat32_free_clusters(fsi, fv->fv_cluster, 1);
if (error) {
return error;
}
fv->fv_cluster = 0;
}
/* Invalidate entire cluster cache */
mutex_lock(&fv->fv_cache_lock, PZERO);
bzero(fv->fv_cls_cache, sizeof(fv->fv_cls_cache));
mutex_unlock(&fv->fv_cache_lock);
/* Update file size to 0 (also correct for directories) and mark dirty */
fv->fv_size = 0;
fv->fv_flags |= FV_SIZE_DIRTY | FV_CLUSTER_DIRTY | FV_MODIFIED;
return 0;
}
/* Determine flags for cluster allocation */
flags = NC_ALLOC;
if (fv->fv_type == VDIR) {
flags |= NC_CLEAR; /* Clear new clusters for directories */
}
/* If file has no clusters yet, allocate the first one */
if (fv->fv_cluster == 0) {
#ifdef FAT32_DBG_CLUSTERS
cmn_err(CE_NOTE, "fat32_truncate: allocating first cluster");
#endif
error = fat32_next_cluster(fsi, 0, &cluster, flags);
if (error) {
cmn_err(CE_WARN, "fat32_truncate: failed to allocate first cluster: %d", error);
return error;
}
fv->fv_cluster = cluster;
fv->fv_flags |= FV_CLUSTER_DIRTY | FV_MODIFIED;
#ifdef FAT32_DBG_CLUSTERS
cmn_err(CE_NOTE, "fat32_truncate: first cluster allocated: %u", cluster);
#endif
}
/* Seek to the last cluster we need, allocating as needed */
#ifdef FAT32_DBG_CLUSTERS
cmn_err(CE_NOTE, "fat32_truncate: seeking to cluster %u (need %u clusters)",
needed_clusters - 1, needed_clusters);
#endif
error = fat32_vnode_seek_cluster(fv, needed_clusters - 1, &cluster, flags);
if (error) {
cmn_err(CE_WARN, "fat32_truncate: failed to seek to cluster %u: %d", needed_clusters - 1, error);
return error;
}
#ifdef FAT32_DBG_CLUSTERS
cmn_err(CE_NOTE, "fat32_truncate: last needed cluster is %u, checking for extras", cluster);
#endif
/* Now 'cluster' points to the last cluster we need */
/* Check if there are more clusters beyond this - if so, free them */
error = fat32_next_cluster(fsi, cluster, &next_cluster, 0);
if (error && error != ENOSPC) {
cmn_err(CE_WARN, "fat32_truncate: failed to read next cluster after %u: %d", cluster, error);
return error;
}
#ifdef FAT32_DBG_CLUSTERS
cmn_err(CE_NOTE, "fat32_truncate: after last cluster: error=%d next=%u", error, next_cluster);
#endif
if (error == 0 && next_cluster >= FAT32_CLUSTER_MIN && next_cluster < FAT32_CLUSTER_EOC_MIN) {
/* There are clusters beyond what we need - free them */
#ifdef FAT32_DBG_CLUSTERS
cmn_err(CE_NOTE, "fat32_truncate: freeing extra clusters starting from %u", cluster);
#endif
error = fat32_free_clusters(fsi, cluster, 0);
if (error) {
cmn_err(CE_WARN, "fat32_truncate: failed to free extra clusters: %d", error);
return error;
}
/* Invalidate cache entries beyond truncation point */
mutex_lock(&fv->fv_cache_lock, PZERO);
{
int i;
for (i = 0; i < FAT32_CLS_CACHE_SIZE; i++) {
if (fv->fv_cls_cache[i].cluster != 0 &&
fv->fv_cls_cache[i].index >= needed_clusters) {
fv->fv_cls_cache[i].cluster = 0;
}
}
}
mutex_unlock(&fv->fv_cache_lock);
}
/* Update file size (not for directories) */
fv->fv_size = (fv->fv_type == VDIR) ? 0 : size;
fv->fv_flags |= FV_SIZE_DIRTY | FV_MODIFIED;
#ifdef FAT32_DBG_CLUSTERS
cmn_err(CE_NOTE, "fat32_truncate: done cluster=%u size=%lld",
fv->fv_cluster, fv->fv_type == VDIR ? 0LL : fv->fv_size);
#endif
return 0;
}
/*
* fat32_make_vnode - Create and initialize a FAT32 vnode
*
* Creates a new vnode for the given cluster number.
* Cluster number is used directly as the inode number.
*
* Parameters:
* fsi - Filesystem info
* cluster - Starting cluster number
* type - Vnode type (VREG, VDIR, etc.)
* vpp - Output vnode pointer
* fv - Pre-allocated fat32fs_vnode_t structure (required, must not be NULL)
* parent_vp - Parent vnode pointer (NULL for root)
* If non-NULL, a reference will be held on parent
*/
int
fat32_make_vnode(uint32_t cluster, vtype_t type, vnode_t **vpp, fat32fs_vnode_t *fv, vnode_t *parent_vp)
{
vnode_t *vp;
fat32fs_info_t *fsi;
#ifdef FAT32_DBG_OTHER
cmn_err(CE_NOTE, "fat32_make_vnode: enter cluster=%u type=%d fv=%p parent_vp=%p",
cluster, type, fv, parent_vp);
#endif
/* Validate that fv is provided */
if (!fv) {
cmn_err(CE_WARN, "fat32_make_vnode: fv is NULL, caller must provide fv structure");
return EINVAL;
}
fsi = fv->fv_fsi;
/* Validate provided fv */
fat32_vnode_validate(fv, __func__, __LINE__);
#ifdef FAT32_DBG_OTHER
cmn_err(CE_NOTE, "fat32_make_vnode: using provided fv=%p", fv);
#endif
/* Allocate system vnode */
vp = vn_alloc(fsi->fsi_vfsp, type, 0);
if (!vp) {
cmn_err(CE_WARN, "fat32_make_vnode: vn_alloc failed");
cmn_err(CE_NOTE, "fat32_make_vnode: exit ENOMEM (vn_alloc)");
return ENOMEM;
}
#ifdef FAT32_DBG_OTHER
cmn_err(CE_NOTE, "fat32_make_vnode: vn_alloc returned vp=%p", vp);
#endif
/* Initialize FAT32 vnode */
/* Note: magic, size, and fv_fsi already initialized by fat32fs_vnode_new */
fv->fv_vnode = vp;
fv->fv_cluster = cluster;
fv->fv_type = type;
mrinit(&fv->fv_lock, "fat32vnode");
/* Store parent vnode and hold reference */
if (parent_vp) {
fat32fs_vnode_t *parent_fv = VNODE_TO_FV(parent_vp);
VN_HOLD(parent_vp);
fv->fv_parent_vp = parent_vp;
fv->fv_parent_cluster = parent_fv->fv_cluster;
#ifdef FAT32_DBG_OTHER
cmn_err(CE_NOTE, "fat32_make_vnode: held reference to parent_vp=%p cluster=%u",
parent_vp, parent_fv->fv_cluster);
#endif
} else {
fv->fv_parent_vp = NULL;
fv->fv_parent_cluster = 0;
}
/* Mark root directory by comparing cluster number */
if (cluster == fsi->fsi_root_cluster && type == VDIR) {
fv->fv_flags |= FV_ROOT;
/* Root directory gets special permissions */
fv->fv_mode = S_IFDIR | 0755;
} else {
fv->fv_flags = 0;
if (fv->fv_mode == 0) {
fv->fv_mode = (type == VDIR) ? (S_IFDIR | 0755) : (S_IFREG | 0644);
}
}
/* Set ownership from mount options */
fv->fv_uid = fv->fv_fsi->fsi_uid;
fv->fv_gid = fv->fv_fsi->fsi_gid;
/* Initialize behavior descriptor and attach to vnode */
bhv_desc_init(&fv->fv_bhv, fv, vp, &fat32vnodeops);
vn_bhv_insert_initial(VN_BHV_HEAD(vp), &fv->fv_bhv);
/* Set VINACTIVE_TEARDOWN flag - required when returning VN_INACTIVE_NOCACHE */
VN_FLAGSET(vp, VINACTIVE_TEARDOWN);
*vpp = vp;
#ifdef FAT32_DBG_OTHER
cmn_err(CE_NOTE, "fat32_make_vnode: exit success, vp=%p", vp);
#endif
return 0;
}
/*
* fat32_touch - Update vnode timestamps
*
* Updates one or more timestamps on a vnode based on the provided flags.
* Uses the FV_*_DIRTY flags to determine which timestamps to update.
*
* Parameters:
* fv - FAT32 vnode to update
* flags - Bitmask of FV_ATIME_DIRTY, FV_MTIME_DIRTY, or a special
* value 0 to update ctime/mtime/atime (for creation)
*
* Notes:
* - If flags is 0, sets all three times (ctime, mtime, atime) to current time
* - Otherwise, updates only the times specified by the flags
* - Always marks the vnode as modified
*/
void
fat32_touch(fat32fs_vnode_t *fv, uint32_t flags)
{
timespec_t now;
nanotime(&now);
/* Special case: flags == 0 means creation time (set all three) */
if (flags == 0) {
fv->fv_ctime = now;
fv->fv_mtime = now;
fv->fv_atime = now;
fv->fv_flags |= FV_MTIME_DIRTY | FV_ATIME_DIRTY | FV_MODIFIED;
return;
}
/* Update specified timestamps */
if (flags & FV_ATIME_DIRTY) {
fv->fv_atime = now;
}
if (flags & FV_MTIME_DIRTY) {
fv->fv_mtime = now;
}
/* Always mark as modified with the specified flags */
fv->fv_flags |= (flags & (FV_ATIME_DIRTY | FV_MTIME_DIRTY)) | FV_MODIFIED;
}
/*
* fat32_faccess - Check access permissions
*/
int
fat32_faccess(fat32fs_vnode_t *fv, int mode, cred_t *cr)
{
mode_t fmode = fv->fv_mode;
uid_t uid = fv->fv_uid;
gid_t gid = fv->fv_gid;
int denied_mode = 0;
/* Check for read-only filesystem */
if ((mode & VWRITE) && (fv->fv_fsi->fsi_vfsp->vfs_flag & VFS_RDONLY)) {
return EROFS;
}
/* Check for read-only attribute on file */
if ((mode & VWRITE) && (fv->fv_attr & FAT32_ATTR_READ_ONLY)) {
return EACCES;
}
/* If root, grant access */
if (cr->cr_uid == 0) {
return 0;
}
/* Check owner permissions */
if (cr->cr_uid == uid) {
if ((mode & VREAD) && !(fmode & S_IRUSR)) denied_mode |= VREAD;
if ((mode & VWRITE) && !(fmode & S_IWUSR)) denied_mode |= VWRITE;
if ((mode & VEXEC) && !(fmode & S_IXUSR)) denied_mode |= VEXEC;
}
/* Check group permissions */
else if (groupmember(gid, cr)) {
if ((mode & VREAD) && !(fmode & S_IRGRP)) denied_mode |= VREAD;
if ((mode & VWRITE) && !(fmode & S_IWGRP)) denied_mode |= VWRITE;
if ((mode & VEXEC) && !(fmode & S_IXGRP)) denied_mode |= VEXEC;
}
/* Check other permissions */
else {
if ((mode & VREAD) && !(fmode & S_IROTH)) denied_mode |= VREAD;
if ((mode & VWRITE) && !(fmode & S_IWOTH)) denied_mode |= VWRITE;
if ((mode & VEXEC) && !(fmode & S_IXOTH)) denied_mode |= VEXEC;
}
if (denied_mode) {
return EACCES;
}
return 0;
}