]>
Commit | Line | Data |
---|---|---|
328970de | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
fa60ce2c | 2 | /* |
ccd979bd MF |
3 | * inode.c |
4 | * | |
5 | * vfs' aops, fops, dops and iops | |
6 | * | |
7 | * Copyright (C) 2002, 2004 Oracle. All rights reserved. | |
ccd979bd MF |
8 | */ |
9 | ||
10 | #include <linux/fs.h> | |
11 | #include <linux/types.h> | |
ccd979bd MF |
12 | #include <linux/highmem.h> |
13 | #include <linux/pagemap.h> | |
a90714c1 | 14 | #include <linux/quotaops.h> |
cc56c33e | 15 | #include <linux/iversion.h> |
ccd979bd MF |
16 | |
17 | #include <asm/byteorder.h> | |
18 | ||
ccd979bd MF |
19 | #include <cluster/masklog.h> |
20 | ||
21 | #include "ocfs2.h" | |
22 | ||
23 | #include "alloc.h" | |
9b7895ef | 24 | #include "dir.h" |
d6b32bbb | 25 | #include "blockcheck.h" |
ccd979bd MF |
26 | #include "dlmglue.h" |
27 | #include "extent_map.h" | |
28 | #include "file.h" | |
b4df6ed8 | 29 | #include "heartbeat.h" |
ccd979bd MF |
30 | #include "inode.h" |
31 | #include "journal.h" | |
32 | #include "namei.h" | |
33 | #include "suballoc.h" | |
34 | #include "super.h" | |
35 | #include "symlink.h" | |
36 | #include "sysfile.h" | |
37 | #include "uptodate.h" | |
cf1d6c76 | 38 | #include "xattr.h" |
8b2c0dba | 39 | #include "refcounttree.h" |
64f3b269 | 40 | #include "ocfs2_trace.h" |
d56a8f32 | 41 | #include "filecheck.h" |
ccd979bd MF |
42 | |
43 | #include "buffer_head_io.h" | |
44 | ||
ccd979bd MF |
45 | struct ocfs2_find_inode_args |
46 | { | |
47 | u64 fi_blkno; | |
48 | unsigned long fi_ino; | |
49 | unsigned int fi_flags; | |
5fa0613e | 50 | unsigned int fi_sysfile_type; |
ccd979bd MF |
51 | }; |
52 | ||
5fa0613e JK |
53 | static struct lock_class_key ocfs2_sysfile_lock_key[NUM_SYSTEM_INODES]; |
54 | ||
ccd979bd MF |
55 | static int ocfs2_read_locked_inode(struct inode *inode, |
56 | struct ocfs2_find_inode_args *args); | |
57 | static int ocfs2_init_locked_inode(struct inode *inode, void *opaque); | |
58 | static int ocfs2_find_actor(struct inode *inode, void *opaque); | |
59 | static int ocfs2_truncate_for_delete(struct ocfs2_super *osb, | |
60 | struct inode *inode, | |
61 | struct buffer_head *fe_bh); | |
62 | ||
d56a8f32 GH |
63 | static int ocfs2_filecheck_read_inode_block_full(struct inode *inode, |
64 | struct buffer_head **bh, | |
65 | int flags, int type); | |
66 | static int ocfs2_filecheck_validate_inode_block(struct super_block *sb, | |
67 | struct buffer_head *bh); | |
68 | static int ocfs2_filecheck_repair_inode_block(struct super_block *sb, | |
69 | struct buffer_head *bh); | |
70 | ||
ca4d147e HP |
71 | void ocfs2_set_inode_flags(struct inode *inode) |
72 | { | |
73 | unsigned int flags = OCFS2_I(inode)->ip_attr; | |
74 | ||
75 | inode->i_flags &= ~(S_IMMUTABLE | | |
76 | S_SYNC | S_APPEND | S_NOATIME | S_DIRSYNC); | |
77 | ||
78 | if (flags & OCFS2_IMMUTABLE_FL) | |
79 | inode->i_flags |= S_IMMUTABLE; | |
80 | ||
81 | if (flags & OCFS2_SYNC_FL) | |
82 | inode->i_flags |= S_SYNC; | |
83 | if (flags & OCFS2_APPEND_FL) | |
84 | inode->i_flags |= S_APPEND; | |
85 | if (flags & OCFS2_NOATIME_FL) | |
86 | inode->i_flags |= S_NOATIME; | |
87 | if (flags & OCFS2_DIRSYNC_FL) | |
88 | inode->i_flags |= S_DIRSYNC; | |
89 | } | |
90 | ||
6e4b0d56 JK |
91 | /* Propagate flags from i_flags to OCFS2_I(inode)->ip_attr */ |
92 | void ocfs2_get_inode_flags(struct ocfs2_inode_info *oi) | |
93 | { | |
94 | unsigned int flags = oi->vfs_inode.i_flags; | |
95 | ||
96 | oi->ip_attr &= ~(OCFS2_SYNC_FL|OCFS2_APPEND_FL| | |
97 | OCFS2_IMMUTABLE_FL|OCFS2_NOATIME_FL|OCFS2_DIRSYNC_FL); | |
98 | if (flags & S_SYNC) | |
99 | oi->ip_attr |= OCFS2_SYNC_FL; | |
100 | if (flags & S_APPEND) | |
101 | oi->ip_attr |= OCFS2_APPEND_FL; | |
102 | if (flags & S_IMMUTABLE) | |
103 | oi->ip_attr |= OCFS2_IMMUTABLE_FL; | |
104 | if (flags & S_NOATIME) | |
105 | oi->ip_attr |= OCFS2_NOATIME_FL; | |
106 | if (flags & S_DIRSYNC) | |
107 | oi->ip_attr |= OCFS2_DIRSYNC_FL; | |
108 | } | |
109 | ||
6ca497a8 | 110 | struct inode *ocfs2_ilookup(struct super_block *sb, u64 blkno) |
111 | { | |
112 | struct ocfs2_find_inode_args args; | |
113 | ||
114 | args.fi_blkno = blkno; | |
115 | args.fi_flags = 0; | |
116 | args.fi_ino = ino_from_blkno(sb, blkno); | |
117 | args.fi_sysfile_type = 0; | |
118 | ||
119 | return ilookup5(sb, blkno, ocfs2_find_actor, &args); | |
120 | } | |
5fa0613e JK |
121 | struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags, |
122 | int sysfile_type) | |
ccd979bd | 123 | { |
8c4d5a43 | 124 | int rc = -ESTALE; |
ccd979bd MF |
125 | struct inode *inode = NULL; |
126 | struct super_block *sb = osb->sb; | |
127 | struct ocfs2_find_inode_args args; | |
bb20b31d | 128 | journal_t *journal = osb->journal->j_journal; |
ccd979bd | 129 | |
64f3b269 TM |
130 | trace_ocfs2_iget_begin((unsigned long long)blkno, flags, |
131 | sysfile_type); | |
ccd979bd MF |
132 | |
133 | /* Ok. By now we've either got the offsets passed to us by the | |
134 | * caller, or we just pulled them off the bh. Lets do some | |
135 | * sanity checks to make sure they're OK. */ | |
136 | if (blkno == 0) { | |
137 | inode = ERR_PTR(-EINVAL); | |
138 | mlog_errno(PTR_ERR(inode)); | |
139 | goto bail; | |
140 | } | |
141 | ||
142 | args.fi_blkno = blkno; | |
24c19ef4 | 143 | args.fi_flags = flags; |
ccd979bd | 144 | args.fi_ino = ino_from_blkno(sb, blkno); |
5fa0613e | 145 | args.fi_sysfile_type = sysfile_type; |
ccd979bd MF |
146 | |
147 | inode = iget5_locked(sb, args.fi_ino, ocfs2_find_actor, | |
148 | ocfs2_init_locked_inode, &args); | |
149 | /* inode was *not* in the inode cache. 2.6.x requires | |
150 | * us to do our own read_inode call and unlock it | |
151 | * afterwards. */ | |
ccd979bd MF |
152 | if (inode == NULL) { |
153 | inode = ERR_PTR(-ENOMEM); | |
154 | mlog_errno(PTR_ERR(inode)); | |
155 | goto bail; | |
156 | } | |
64f3b269 | 157 | trace_ocfs2_iget5_locked(inode->i_state); |
6218b90e | 158 | if (inode->i_state & I_NEW) { |
d56a8f32 | 159 | rc = ocfs2_read_locked_inode(inode, &args); |
6218b90e TM |
160 | unlock_new_inode(inode); |
161 | } | |
ccd979bd MF |
162 | if (is_bad_inode(inode)) { |
163 | iput(inode); | |
1f3a437f | 164 | inode = ERR_PTR(rc); |
ccd979bd MF |
165 | goto bail; |
166 | } | |
167 | ||
2931cdcb DW |
168 | /* |
169 | * Set transaction id's of transactions that have to be committed | |
170 | * to finish f[data]sync. We set them to currently running transaction | |
171 | * as we cannot be sure that the inode or some of its metadata isn't | |
172 | * part of the transaction - the inode could have been reclaimed and | |
173 | * now it is reread from disk. | |
174 | */ | |
bb20b31d | 175 | if (journal) { |
2931cdcb DW |
176 | transaction_t *transaction; |
177 | tid_t tid; | |
178 | struct ocfs2_inode_info *oi = OCFS2_I(inode); | |
179 | ||
180 | read_lock(&journal->j_state_lock); | |
181 | if (journal->j_running_transaction) | |
182 | transaction = journal->j_running_transaction; | |
183 | else | |
184 | transaction = journal->j_committing_transaction; | |
185 | if (transaction) | |
186 | tid = transaction->t_tid; | |
187 | else | |
188 | tid = journal->j_commit_sequence; | |
189 | read_unlock(&journal->j_state_lock); | |
190 | oi->i_sync_tid = tid; | |
191 | oi->i_datasync_tid = tid; | |
192 | } | |
193 | ||
ccd979bd MF |
194 | bail: |
195 | if (!IS_ERR(inode)) { | |
64f3b269 TM |
196 | trace_ocfs2_iget_end(inode, |
197 | (unsigned long long)OCFS2_I(inode)->ip_blkno); | |
6a1bd4a5 | 198 | } |
ccd979bd MF |
199 | |
200 | return inode; | |
201 | } | |
202 | ||
fdbb6cd9 JS |
203 | static int ocfs2_dinode_has_extents(struct ocfs2_dinode *di) |
204 | { | |
205 | /* inodes flagged with other stuff in id2 */ | |
206 | if (di->i_flags & (OCFS2_SUPER_BLOCK_FL | OCFS2_LOCAL_ALLOC_FL | | |
207 | OCFS2_CHAIN_FL | OCFS2_DEALLOC_FL)) | |
208 | return 0; | |
209 | /* i_flags doesn't indicate when id2 is a fast symlink */ | |
210 | if (S_ISLNK(di->i_mode) && di->i_size && di->i_clusters == 0) | |
211 | return 0; | |
212 | if (di->i_dyn_features & OCFS2_INLINE_DATA_FL) | |
213 | return 0; | |
214 | ||
215 | return 1; | |
216 | } | |
ccd979bd MF |
217 | |
218 | /* | |
219 | * here's how inodes get read from disk: | |
220 | * iget5_locked -> find_actor -> OCFS2_FIND_ACTOR | |
221 | * found? : return the in-memory inode | |
222 | * not found? : get_new_inode -> OCFS2_INIT_LOCKED_INODE | |
223 | */ | |
224 | ||
225 | static int ocfs2_find_actor(struct inode *inode, void *opaque) | |
226 | { | |
227 | struct ocfs2_find_inode_args *args = NULL; | |
228 | struct ocfs2_inode_info *oi = OCFS2_I(inode); | |
229 | int ret = 0; | |
230 | ||
ccd979bd MF |
231 | args = opaque; |
232 | ||
233 | mlog_bug_on_msg(!inode, "No inode in find actor!\n"); | |
234 | ||
64f3b269 TM |
235 | trace_ocfs2_find_actor(inode, inode->i_ino, opaque, args->fi_blkno); |
236 | ||
ccd979bd MF |
237 | if (oi->ip_blkno != args->fi_blkno) |
238 | goto bail; | |
239 | ||
ccd979bd MF |
240 | ret = 1; |
241 | bail: | |
ccd979bd MF |
242 | return ret; |
243 | } | |
244 | ||
245 | /* | |
246 | * initialize the new inode, but don't do anything that would cause | |
247 | * us to sleep. | |
248 | * return 0 on success, 1 on failure | |
249 | */ | |
250 | static int ocfs2_init_locked_inode(struct inode *inode, void *opaque) | |
251 | { | |
252 | struct ocfs2_find_inode_args *args = opaque; | |
cb25797d JK |
253 | static struct lock_class_key ocfs2_quota_ip_alloc_sem_key, |
254 | ocfs2_file_ip_alloc_sem_key; | |
ccd979bd | 255 | |
ccd979bd MF |
256 | inode->i_ino = args->fi_ino; |
257 | OCFS2_I(inode)->ip_blkno = args->fi_blkno; | |
5fa0613e | 258 | if (args->fi_sysfile_type != 0) |
9902af79 | 259 | lockdep_set_class(&inode->i_rwsem, |
5fa0613e | 260 | &ocfs2_sysfile_lock_key[args->fi_sysfile_type]); |
cb25797d JK |
261 | if (args->fi_sysfile_type == USER_QUOTA_SYSTEM_INODE || |
262 | args->fi_sysfile_type == GROUP_QUOTA_SYSTEM_INODE || | |
263 | args->fi_sysfile_type == LOCAL_USER_QUOTA_SYSTEM_INODE || | |
264 | args->fi_sysfile_type == LOCAL_GROUP_QUOTA_SYSTEM_INODE) | |
265 | lockdep_set_class(&OCFS2_I(inode)->ip_alloc_sem, | |
266 | &ocfs2_quota_ip_alloc_sem_key); | |
267 | else | |
268 | lockdep_set_class(&OCFS2_I(inode)->ip_alloc_sem, | |
269 | &ocfs2_file_ip_alloc_sem_key); | |
ccd979bd | 270 | |
ccd979bd MF |
271 | return 0; |
272 | } | |
273 | ||
b657c95c JB |
274 | void ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe, |
275 | int create_ino) | |
ccd979bd MF |
276 | { |
277 | struct super_block *sb; | |
278 | struct ocfs2_super *osb; | |
53da4939 | 279 | int use_plocks = 1; |
ccd979bd | 280 | |
ccd979bd MF |
281 | sb = inode->i_sb; |
282 | osb = OCFS2_SB(sb); | |
283 | ||
53da4939 MF |
284 | if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) || |
285 | ocfs2_mount_local(osb) || !ocfs2_stack_supports_plocks()) | |
286 | use_plocks = 0; | |
287 | ||
b657c95c JB |
288 | /* |
289 | * These have all been checked by ocfs2_read_inode_block() or set | |
290 | * by ocfs2_mknod_locked(), so a failure is a code bug. | |
291 | */ | |
292 | BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); /* This means that read_inode | |
293 | cannot create a superblock | |
294 | inode today. change if | |
295 | that is needed. */ | |
296 | BUG_ON(!(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL))); | |
297 | BUG_ON(le32_to_cpu(fe->i_fs_generation) != osb->fs_generation); | |
ccd979bd | 298 | |
ccd979bd | 299 | |
8110b073 MF |
300 | OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters); |
301 | OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr); | |
15b1e36b | 302 | OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features); |
8110b073 | 303 | |
cc56c33e | 304 | inode_set_iversion(inode, 1); |
ccd979bd MF |
305 | inode->i_generation = le32_to_cpu(fe->i_generation); |
306 | inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev)); | |
307 | inode->i_mode = le16_to_cpu(fe->i_mode); | |
2c034176 EB |
308 | i_uid_write(inode, le32_to_cpu(fe->i_uid)); |
309 | i_gid_write(inode, le32_to_cpu(fe->i_gid)); | |
ccd979bd MF |
310 | |
311 | /* Fast symlinks will have i_size but no allocated clusters. */ | |
ea022dfb | 312 | if (S_ISLNK(inode->i_mode) && !fe->i_clusters) { |
ccd979bd | 313 | inode->i_blocks = 0; |
ea022dfb AV |
314 | inode->i_mapping->a_ops = &ocfs2_fast_symlink_aops; |
315 | } else { | |
8110b073 | 316 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
ea022dfb AV |
317 | inode->i_mapping->a_ops = &ocfs2_aops; |
318 | } | |
fd6acbbc JL |
319 | inode_set_atime(inode, le64_to_cpu(fe->i_atime), |
320 | le32_to_cpu(fe->i_atime_nsec)); | |
321 | inode_set_mtime(inode, le64_to_cpu(fe->i_mtime), | |
322 | le32_to_cpu(fe->i_mtime_nsec)); | |
6861de97 JL |
323 | inode_set_ctime(inode, le64_to_cpu(fe->i_ctime), |
324 | le32_to_cpu(fe->i_ctime_nsec)); | |
ccd979bd MF |
325 | |
326 | if (OCFS2_I(inode)->ip_blkno != le64_to_cpu(fe->i_blkno)) | |
327 | mlog(ML_ERROR, | |
b0697053 MF |
328 | "ip_blkno %llu != i_blkno %llu!\n", |
329 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
1ca1a111 | 330 | (unsigned long long)le64_to_cpu(fe->i_blkno)); |
ccd979bd | 331 | |
bfe86848 | 332 | set_nlink(inode, ocfs2_read_links_count(fe)); |
ccd979bd | 333 | |
64f3b269 TM |
334 | trace_ocfs2_populate_inode(OCFS2_I(inode)->ip_blkno, |
335 | le32_to_cpu(fe->i_flags)); | |
bbbd0eb3 | 336 | if (fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) { |
24c19ef4 | 337 | OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SYSTEM_FILE; |
bbbd0eb3 JK |
338 | inode->i_flags |= S_NOQUOTA; |
339 | } | |
64f3b269 | 340 | |
ccd979bd MF |
341 | if (fe->i_flags & cpu_to_le32(OCFS2_LOCAL_ALLOC_FL)) { |
342 | OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP; | |
ccd979bd MF |
343 | } else if (fe->i_flags & cpu_to_le32(OCFS2_BITMAP_FL)) { |
344 | OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP; | |
1a224ad1 JK |
345 | } else if (fe->i_flags & cpu_to_le32(OCFS2_QUOTA_FL)) { |
346 | inode->i_flags |= S_NOQUOTA; | |
ccd979bd | 347 | } else if (fe->i_flags & cpu_to_le32(OCFS2_SUPER_BLOCK_FL)) { |
ccd979bd MF |
348 | /* we can't actually hit this as read_inode can't |
349 | * handle superblocks today ;-) */ | |
350 | BUG(); | |
351 | } | |
352 | ||
353 | switch (inode->i_mode & S_IFMT) { | |
354 | case S_IFREG: | |
53da4939 MF |
355 | if (use_plocks) |
356 | inode->i_fop = &ocfs2_fops; | |
357 | else | |
358 | inode->i_fop = &ocfs2_fops_no_plocks; | |
ccd979bd MF |
359 | inode->i_op = &ocfs2_file_iops; |
360 | i_size_write(inode, le64_to_cpu(fe->i_size)); | |
361 | break; | |
362 | case S_IFDIR: | |
363 | inode->i_op = &ocfs2_dir_iops; | |
53da4939 MF |
364 | if (use_plocks) |
365 | inode->i_fop = &ocfs2_dops; | |
366 | else | |
367 | inode->i_fop = &ocfs2_dops_no_plocks; | |
ccd979bd | 368 | i_size_write(inode, le64_to_cpu(fe->i_size)); |
5e98d492 | 369 | OCFS2_I(inode)->ip_dir_lock_gen = 1; |
ccd979bd MF |
370 | break; |
371 | case S_IFLNK: | |
ea022dfb | 372 | inode->i_op = &ocfs2_symlink_inode_operations; |
21fc61c7 | 373 | inode_nohighmem(inode); |
ccd979bd MF |
374 | i_size_write(inode, le64_to_cpu(fe->i_size)); |
375 | break; | |
376 | default: | |
377 | inode->i_op = &ocfs2_special_file_iops; | |
378 | init_special_inode(inode, inode->i_mode, | |
379 | inode->i_rdev); | |
380 | break; | |
381 | } | |
382 | ||
24c19ef4 MF |
383 | if (create_ino) { |
384 | inode->i_ino = ino_from_blkno(inode->i_sb, | |
385 | le64_to_cpu(fe->i_blkno)); | |
386 | ||
387 | /* | |
388 | * If we ever want to create system files from kernel, | |
389 | * the generation argument to | |
390 | * ocfs2_inode_lock_res_init() will have to change. | |
391 | */ | |
1ca1a111 | 392 | BUG_ON(le32_to_cpu(fe->i_flags) & OCFS2_SYSTEM_FL); |
24c19ef4 | 393 | |
e63aecb6 | 394 | ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres, |
24c19ef4 | 395 | OCFS2_LOCK_TYPE_META, 0, inode); |
50008630 TY |
396 | |
397 | ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres, | |
398 | OCFS2_LOCK_TYPE_OPEN, 0, inode); | |
24c19ef4 MF |
399 | } |
400 | ||
ccd979bd | 401 | ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_rw_lockres, |
24c19ef4 MF |
402 | OCFS2_LOCK_TYPE_RW, inode->i_generation, |
403 | inode); | |
404 | ||
ca4d147e | 405 | ocfs2_set_inode_flags(inode); |
ca4d147e | 406 | |
13821151 TM |
407 | OCFS2_I(inode)->ip_last_used_slot = 0; |
408 | OCFS2_I(inode)->ip_last_used_group = 0; | |
e3b4a97d MF |
409 | |
410 | if (S_ISDIR(inode->i_mode)) | |
411 | ocfs2_resv_set_type(&OCFS2_I(inode)->ip_la_data_resv, | |
412 | OCFS2_RESV_FLAG_DIR); | |
ccd979bd MF |
413 | } |
414 | ||
415 | static int ocfs2_read_locked_inode(struct inode *inode, | |
416 | struct ocfs2_find_inode_args *args) | |
417 | { | |
418 | struct super_block *sb; | |
419 | struct ocfs2_super *osb; | |
420 | struct ocfs2_dinode *fe; | |
421 | struct buffer_head *bh = NULL; | |
d56a8f32 | 422 | int status, can_lock, lock_level = 0; |
24c19ef4 | 423 | u32 generation = 0; |
ccd979bd | 424 | |
ccd979bd | 425 | status = -EINVAL; |
ccd979bd MF |
426 | sb = inode->i_sb; |
427 | osb = OCFS2_SB(sb); | |
428 | ||
24c19ef4 MF |
429 | /* |
430 | * To improve performance of cold-cache inode stats, we take | |
431 | * the cluster lock here if possible. | |
432 | * | |
433 | * Generally, OCFS2 never trusts the contents of an inode | |
434 | * unless it's holding a cluster lock, so taking it here isn't | |
435 | * a correctness issue as much as it is a performance | |
436 | * improvement. | |
437 | * | |
438 | * There are three times when taking the lock is not a good idea: | |
439 | * | |
440 | * 1) During startup, before we have initialized the DLM. | |
441 | * | |
442 | * 2) If we are reading certain system files which never get | |
443 | * cluster locks (local alloc, truncate log). | |
444 | * | |
445 | * 3) If the process doing the iget() is responsible for | |
446 | * orphan dir recovery. We're holding the orphan dir lock and | |
447 | * can get into a deadlock with another process on another | |
448 | * node in ->delete_inode(). | |
449 | * | |
450 | * #1 and #2 can be simply solved by never taking the lock | |
451 | * here for system files (which are the only type we read | |
452 | * during mount). It's a heavier approach, but our main | |
b595076a | 453 | * concern is user-accessible files anyway. |
24c19ef4 MF |
454 | * |
455 | * #3 works itself out because we'll eventually take the | |
456 | * cluster lock before trusting anything anyway. | |
457 | */ | |
458 | can_lock = !(args->fi_flags & OCFS2_FI_FLAG_SYSFILE) | |
50008630 | 459 | && !(args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY) |
c271c5c2 | 460 | && !ocfs2_mount_local(osb); |
24c19ef4 | 461 | |
64f3b269 TM |
462 | trace_ocfs2_read_locked_inode( |
463 | (unsigned long long)OCFS2_I(inode)->ip_blkno, can_lock); | |
464 | ||
24c19ef4 MF |
465 | /* |
466 | * To maintain backwards compatibility with older versions of | |
467 | * ocfs2-tools, we still store the generation value for system | |
468 | * files. The only ones that actually matter to userspace are | |
469 | * the journals, but it's easier and inexpensive to just flag | |
470 | * all system files similarly. | |
471 | */ | |
472 | if (args->fi_flags & OCFS2_FI_FLAG_SYSFILE) | |
473 | generation = osb->fs_generation; | |
474 | ||
e63aecb6 | 475 | ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres, |
24c19ef4 MF |
476 | OCFS2_LOCK_TYPE_META, |
477 | generation, inode); | |
478 | ||
50008630 TY |
479 | ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres, |
480 | OCFS2_LOCK_TYPE_OPEN, | |
481 | 0, inode); | |
482 | ||
24c19ef4 | 483 | if (can_lock) { |
50008630 TY |
484 | status = ocfs2_open_lock(inode); |
485 | if (status) { | |
486 | make_bad_inode(inode); | |
487 | mlog_errno(status); | |
488 | return status; | |
489 | } | |
d56a8f32 | 490 | status = ocfs2_inode_lock(inode, NULL, lock_level); |
24c19ef4 MF |
491 | if (status) { |
492 | make_bad_inode(inode); | |
493 | mlog_errno(status); | |
494 | return status; | |
495 | } | |
ccd979bd MF |
496 | } |
497 | ||
50008630 TY |
498 | if (args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY) { |
499 | status = ocfs2_try_open_lock(inode, 0); | |
500 | if (status) { | |
2bd63216 | 501 | make_bad_inode(inode); |
50008630 TY |
502 | return status; |
503 | } | |
504 | } | |
505 | ||
b657c95c | 506 | if (can_lock) { |
d56a8f32 GH |
507 | if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_CHK) |
508 | status = ocfs2_filecheck_read_inode_block_full(inode, | |
509 | &bh, OCFS2_BH_IGNORE_CACHE, 0); | |
510 | else if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_FIX) | |
511 | status = ocfs2_filecheck_read_inode_block_full(inode, | |
512 | &bh, OCFS2_BH_IGNORE_CACHE, 1); | |
513 | else | |
514 | status = ocfs2_read_inode_block_full(inode, | |
515 | &bh, OCFS2_BH_IGNORE_CACHE); | |
b657c95c | 516 | } else { |
da1e9098 | 517 | status = ocfs2_read_blocks_sync(osb, args->fi_blkno, 1, &bh); |
f5ce5a08 SM |
518 | /* |
519 | * If buffer is in jbd, then its checksum may not have been | |
520 | * computed as yet. | |
521 | */ | |
d56a8f32 GH |
522 | if (!status && !buffer_jbd(bh)) { |
523 | if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_CHK) | |
524 | status = ocfs2_filecheck_validate_inode_block( | |
525 | osb->sb, bh); | |
526 | else if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_FIX) | |
527 | status = ocfs2_filecheck_repair_inode_block( | |
528 | osb->sb, bh); | |
529 | else | |
530 | status = ocfs2_validate_inode_block( | |
531 | osb->sb, bh); | |
532 | } | |
b657c95c | 533 | } |
ccd979bd MF |
534 | if (status < 0) { |
535 | mlog_errno(status); | |
ccd979bd MF |
536 | goto bail; |
537 | } | |
538 | ||
24c19ef4 | 539 | status = -EINVAL; |
ccd979bd | 540 | fe = (struct ocfs2_dinode *) bh->b_data; |
ccd979bd | 541 | |
24c19ef4 MF |
542 | /* |
543 | * This is a code bug. Right now the caller needs to | |
544 | * understand whether it is asking for a system file inode or | |
545 | * not so the proper lock names can be built. | |
546 | */ | |
547 | mlog_bug_on_msg(!!(fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) != | |
548 | !!(args->fi_flags & OCFS2_FI_FLAG_SYSFILE), | |
1c3ce541 | 549 | "Inode %llu: system file state is ambiguous\n", |
24c19ef4 | 550 | (unsigned long long)args->fi_blkno); |
ccd979bd MF |
551 | |
552 | if (S_ISCHR(le16_to_cpu(fe->i_mode)) || | |
553 | S_ISBLK(le16_to_cpu(fe->i_mode))) | |
b657c95c | 554 | inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev)); |
ccd979bd | 555 | |
b657c95c | 556 | ocfs2_populate_inode(inode, fe, 0); |
ccd979bd MF |
557 | |
558 | BUG_ON(args->fi_blkno != le64_to_cpu(fe->i_blkno)); | |
559 | ||
d56a8f32 GH |
560 | if (buffer_dirty(bh) && !buffer_jbd(bh)) { |
561 | if (can_lock) { | |
562 | ocfs2_inode_unlock(inode, lock_level); | |
563 | lock_level = 1; | |
564 | ocfs2_inode_lock(inode, NULL, lock_level); | |
565 | } | |
566 | status = ocfs2_write_block(osb, bh, INODE_CACHE(inode)); | |
567 | if (status < 0) { | |
568 | mlog_errno(status); | |
569 | goto bail; | |
570 | } | |
571 | } | |
572 | ||
ccd979bd MF |
573 | status = 0; |
574 | ||
575 | bail: | |
24c19ef4 | 576 | if (can_lock) |
d56a8f32 | 577 | ocfs2_inode_unlock(inode, lock_level); |
24c19ef4 MF |
578 | |
579 | if (status < 0) | |
580 | make_bad_inode(inode); | |
581 | ||
4a635a11 | 582 | brelse(bh); |
ccd979bd | 583 | |
ccd979bd MF |
584 | return status; |
585 | } | |
586 | ||
587 | void ocfs2_sync_blockdev(struct super_block *sb) | |
588 | { | |
589 | sync_blockdev(sb->s_bdev); | |
590 | } | |
591 | ||
592 | static int ocfs2_truncate_for_delete(struct ocfs2_super *osb, | |
593 | struct inode *inode, | |
594 | struct buffer_head *fe_bh) | |
595 | { | |
596 | int status = 0; | |
ccd979bd | 597 | struct ocfs2_dinode *fe; |
60b11392 | 598 | handle_t *handle = NULL; |
ccd979bd | 599 | |
ccd979bd MF |
600 | fe = (struct ocfs2_dinode *) fe_bh->b_data; |
601 | ||
1afc32b9 MF |
602 | /* |
603 | * This check will also skip truncate of inodes with inline | |
604 | * data and fast symlinks. | |
605 | */ | |
3a0782d0 | 606 | if (fe->i_clusters) { |
2b4e30fb JB |
607 | if (ocfs2_should_order_data(inode)) |
608 | ocfs2_begin_ordered_truncate(inode, 0); | |
609 | ||
60b11392 MF |
610 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
611 | if (IS_ERR(handle)) { | |
612 | status = PTR_ERR(handle); | |
0350cb07 | 613 | handle = NULL; |
60b11392 MF |
614 | mlog_errno(status); |
615 | goto out; | |
616 | } | |
617 | ||
0cf2f763 JB |
618 | status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), |
619 | fe_bh, | |
13723d00 | 620 | OCFS2_JOURNAL_ACCESS_WRITE); |
60b11392 MF |
621 | if (status < 0) { |
622 | mlog_errno(status); | |
623 | goto out; | |
624 | } | |
625 | ||
626 | i_size_write(inode, 0); | |
627 | ||
628 | status = ocfs2_mark_inode_dirty(handle, inode, fe_bh); | |
629 | if (status < 0) { | |
630 | mlog_errno(status); | |
631 | goto out; | |
632 | } | |
633 | ||
634 | ocfs2_commit_trans(osb, handle); | |
635 | handle = NULL; | |
636 | ||
78f94673 | 637 | status = ocfs2_commit_truncate(osb, inode, fe_bh); |
229ba1f8 | 638 | if (status < 0) |
3a0782d0 | 639 | mlog_errno(status); |
ccd979bd | 640 | } |
ccd979bd | 641 | |
60b11392 MF |
642 | out: |
643 | if (handle) | |
644 | ocfs2_commit_trans(osb, handle); | |
ccd979bd MF |
645 | return status; |
646 | } | |
647 | ||
648 | static int ocfs2_remove_inode(struct inode *inode, | |
649 | struct buffer_head *di_bh, | |
650 | struct inode *orphan_dir_inode, | |
651 | struct buffer_head *orphan_dir_bh) | |
652 | { | |
653 | int status; | |
654 | struct inode *inode_alloc_inode = NULL; | |
655 | struct buffer_head *inode_alloc_bh = NULL; | |
1fabe148 | 656 | handle_t *handle; |
ccd979bd MF |
657 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
658 | struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data; | |
659 | ||
660 | inode_alloc_inode = | |
661 | ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE, | |
662 | le16_to_cpu(di->i_suballoc_slot)); | |
663 | if (!inode_alloc_inode) { | |
62f8b1f0 | 664 | status = -ENOENT; |
ccd979bd MF |
665 | mlog_errno(status); |
666 | goto bail; | |
667 | } | |
668 | ||
5955102c | 669 | inode_lock(inode_alloc_inode); |
e63aecb6 | 670 | status = ocfs2_inode_lock(inode_alloc_inode, &inode_alloc_bh, 1); |
ccd979bd | 671 | if (status < 0) { |
5955102c | 672 | inode_unlock(inode_alloc_inode); |
ccd979bd MF |
673 | |
674 | mlog_errno(status); | |
675 | goto bail; | |
676 | } | |
677 | ||
a90714c1 | 678 | handle = ocfs2_start_trans(osb, OCFS2_DELETE_INODE_CREDITS + |
9b7895ef | 679 | ocfs2_quota_trans_credits(inode->i_sb)); |
ccd979bd MF |
680 | if (IS_ERR(handle)) { |
681 | status = PTR_ERR(handle); | |
682 | mlog_errno(status); | |
683 | goto bail_unlock; | |
684 | } | |
685 | ||
d4cd1871 LD |
686 | if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) { |
687 | status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode, | |
06ee5c75 | 688 | orphan_dir_bh, false); |
d4cd1871 LD |
689 | if (status < 0) { |
690 | mlog_errno(status); | |
691 | goto bail_commit; | |
692 | } | |
ccd979bd MF |
693 | } |
694 | ||
695 | /* set the inodes dtime */ | |
0cf2f763 | 696 | status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, |
13723d00 | 697 | OCFS2_JOURNAL_ACCESS_WRITE); |
ccd979bd MF |
698 | if (status < 0) { |
699 | mlog_errno(status); | |
700 | goto bail_commit; | |
701 | } | |
702 | ||
c62c38f6 | 703 | di->i_dtime = cpu_to_le64(ktime_get_real_seconds()); |
4092d49f | 704 | di->i_flags &= cpu_to_le32(~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL)); |
ec20cec7 | 705 | ocfs2_journal_dirty(handle, di_bh); |
ccd979bd | 706 | |
8cb471e8 | 707 | ocfs2_remove_from_cache(INODE_CACHE(inode), di_bh); |
63936dda | 708 | dquot_free_inode(inode); |
ccd979bd MF |
709 | |
710 | status = ocfs2_free_dinode(handle, inode_alloc_inode, | |
711 | inode_alloc_bh, di); | |
712 | if (status < 0) | |
713 | mlog_errno(status); | |
714 | ||
715 | bail_commit: | |
02dc1af4 | 716 | ocfs2_commit_trans(osb, handle); |
ccd979bd | 717 | bail_unlock: |
e63aecb6 | 718 | ocfs2_inode_unlock(inode_alloc_inode, 1); |
5955102c | 719 | inode_unlock(inode_alloc_inode); |
ccd979bd MF |
720 | brelse(inode_alloc_bh); |
721 | bail: | |
722 | iput(inode_alloc_inode); | |
723 | ||
724 | return status; | |
725 | } | |
726 | ||
2bd63216 | 727 | /* |
b4df6ed8 MF |
728 | * Serialize with orphan dir recovery. If the process doing |
729 | * recovery on this orphan dir does an iget() with the dir | |
137cebf9 | 730 | * i_rwsem held, we'll deadlock here. Instead we detect this |
b4df6ed8 MF |
731 | * and exit early - recovery will wipe this inode for us. |
732 | */ | |
733 | static int ocfs2_check_orphan_recovery_state(struct ocfs2_super *osb, | |
734 | int slot) | |
735 | { | |
736 | int ret = 0; | |
737 | ||
738 | spin_lock(&osb->osb_lock); | |
739 | if (ocfs2_node_map_test_bit(osb, &osb->osb_recovering_orphan_dirs, slot)) { | |
b4df6ed8 MF |
740 | ret = -EDEADLK; |
741 | goto out; | |
742 | } | |
743 | /* This signals to the orphan recovery process that it should | |
744 | * wait for us to handle the wipe. */ | |
745 | osb->osb_orphan_wipes[slot]++; | |
746 | out: | |
747 | spin_unlock(&osb->osb_lock); | |
64f3b269 | 748 | trace_ocfs2_check_orphan_recovery_state(slot, ret); |
b4df6ed8 MF |
749 | return ret; |
750 | } | |
751 | ||
752 | static void ocfs2_signal_wipe_completion(struct ocfs2_super *osb, | |
753 | int slot) | |
754 | { | |
755 | spin_lock(&osb->osb_lock); | |
756 | osb->osb_orphan_wipes[slot]--; | |
757 | spin_unlock(&osb->osb_lock); | |
758 | ||
759 | wake_up(&osb->osb_wipe_event); | |
760 | } | |
761 | ||
ccd979bd MF |
762 | static int ocfs2_wipe_inode(struct inode *inode, |
763 | struct buffer_head *di_bh) | |
764 | { | |
d577632e | 765 | int status, orphaned_slot = -1; |
ccd979bd MF |
766 | struct inode *orphan_dir_inode = NULL; |
767 | struct buffer_head *orphan_dir_bh = NULL; | |
768 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
d4cd1871 | 769 | struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data; |
ccd979bd | 770 | |
d4cd1871 LD |
771 | if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) { |
772 | orphaned_slot = le16_to_cpu(di->i_orphaned_slot); | |
b4df6ed8 | 773 | |
d4cd1871 LD |
774 | status = ocfs2_check_orphan_recovery_state(osb, orphaned_slot); |
775 | if (status) | |
776 | return status; | |
b4df6ed8 | 777 | |
d4cd1871 LD |
778 | orphan_dir_inode = ocfs2_get_system_file_inode(osb, |
779 | ORPHAN_DIR_SYSTEM_INODE, | |
780 | orphaned_slot); | |
781 | if (!orphan_dir_inode) { | |
62f8b1f0 | 782 | status = -ENOENT; |
d4cd1871 LD |
783 | mlog_errno(status); |
784 | goto bail; | |
785 | } | |
ccd979bd | 786 | |
d4cd1871 LD |
787 | /* Lock the orphan dir. The lock will be held for the entire |
788 | * delete_inode operation. We do this now to avoid races with | |
789 | * recovery completion on other nodes. */ | |
5955102c | 790 | inode_lock(orphan_dir_inode); |
d4cd1871 LD |
791 | status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1); |
792 | if (status < 0) { | |
5955102c | 793 | inode_unlock(orphan_dir_inode); |
ccd979bd | 794 | |
d4cd1871 LD |
795 | mlog_errno(status); |
796 | goto bail; | |
797 | } | |
ccd979bd MF |
798 | } |
799 | ||
800 | /* we do this while holding the orphan dir lock because we | |
34d024f8 MF |
801 | * don't want recovery being run from another node to try an |
802 | * inode delete underneath us -- this will result in two nodes | |
ccd979bd MF |
803 | * truncating the same file! */ |
804 | status = ocfs2_truncate_for_delete(osb, inode, di_bh); | |
805 | if (status < 0) { | |
806 | mlog_errno(status); | |
807 | goto bail_unlock_dir; | |
808 | } | |
809 | ||
9b7895ef MF |
810 | /* Remove any dir index tree */ |
811 | if (S_ISDIR(inode->i_mode)) { | |
812 | status = ocfs2_dx_dir_truncate(inode, di_bh); | |
813 | if (status) { | |
814 | mlog_errno(status); | |
815 | goto bail_unlock_dir; | |
816 | } | |
817 | } | |
818 | ||
cf1d6c76 TY |
819 | /*Free extended attribute resources associated with this inode.*/ |
820 | status = ocfs2_xattr_remove(inode, di_bh); | |
821 | if (status < 0) { | |
822 | mlog_errno(status); | |
823 | goto bail_unlock_dir; | |
824 | } | |
825 | ||
8b2c0dba TM |
826 | status = ocfs2_remove_refcount_tree(inode, di_bh); |
827 | if (status < 0) { | |
828 | mlog_errno(status); | |
829 | goto bail_unlock_dir; | |
830 | } | |
831 | ||
ccd979bd MF |
832 | status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode, |
833 | orphan_dir_bh); | |
834 | if (status < 0) | |
835 | mlog_errno(status); | |
836 | ||
837 | bail_unlock_dir: | |
d4cd1871 LD |
838 | if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR) |
839 | return status; | |
840 | ||
e63aecb6 | 841 | ocfs2_inode_unlock(orphan_dir_inode, 1); |
5955102c | 842 | inode_unlock(orphan_dir_inode); |
ccd979bd MF |
843 | brelse(orphan_dir_bh); |
844 | bail: | |
845 | iput(orphan_dir_inode); | |
b4df6ed8 | 846 | ocfs2_signal_wipe_completion(osb, orphaned_slot); |
ccd979bd MF |
847 | |
848 | return status; | |
849 | } | |
850 | ||
851 | /* There is a series of simple checks that should be done before a | |
34d024f8 | 852 | * trylock is even considered. Encapsulate those in this function. */ |
ccd979bd MF |
853 | static int ocfs2_inode_is_valid_to_delete(struct inode *inode) |
854 | { | |
855 | int ret = 0; | |
856 | struct ocfs2_inode_info *oi = OCFS2_I(inode); | |
857 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
858 | ||
64f3b269 TM |
859 | trace_ocfs2_inode_is_valid_to_delete(current, osb->dc_task, |
860 | (unsigned long long)oi->ip_blkno, | |
861 | oi->ip_flags); | |
862 | ||
ccd979bd MF |
863 | /* We shouldn't be getting here for the root directory |
864 | * inode.. */ | |
865 | if (inode == osb->root_inode) { | |
866 | mlog(ML_ERROR, "Skipping delete of root inode.\n"); | |
867 | goto bail; | |
868 | } | |
869 | ||
bd62ad7a JK |
870 | /* |
871 | * If we're coming from downconvert_thread we can't go into our own | |
872 | * voting [hello, deadlock city!] so we cannot delete the inode. But | |
873 | * since we dropped last inode ref when downconverting dentry lock, | |
874 | * we cannot have the file open and thus the node doing unlink will | |
875 | * take care of deleting the inode. | |
876 | */ | |
64f3b269 | 877 | if (current == osb->dc_task) |
ccd979bd | 878 | goto bail; |
ccd979bd MF |
879 | |
880 | spin_lock(&oi->ip_lock); | |
881 | /* OCFS2 *never* deletes system files. This should technically | |
882 | * never get here as system file inodes should always have a | |
883 | * positive link count. */ | |
884 | if (oi->ip_flags & OCFS2_INODE_SYSTEM_FILE) { | |
b0697053 MF |
885 | mlog(ML_ERROR, "Skipping delete of system file %llu\n", |
886 | (unsigned long long)oi->ip_blkno); | |
ccd979bd MF |
887 | goto bail_unlock; |
888 | } | |
889 | ||
ccd979bd MF |
890 | ret = 1; |
891 | bail_unlock: | |
892 | spin_unlock(&oi->ip_lock); | |
893 | bail: | |
894 | return ret; | |
895 | } | |
896 | ||
897 | /* Query the cluster to determine whether we should wipe an inode from | |
898 | * disk or not. | |
899 | * | |
900 | * Requires the inode to have the cluster lock. */ | |
901 | static int ocfs2_query_inode_wipe(struct inode *inode, | |
902 | struct buffer_head *di_bh, | |
903 | int *wipe) | |
904 | { | |
64f3b269 | 905 | int status = 0, reason = 0; |
ccd979bd MF |
906 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
907 | struct ocfs2_dinode *di; | |
908 | ||
909 | *wipe = 0; | |
910 | ||
64f3b269 TM |
911 | trace_ocfs2_query_inode_wipe_begin((unsigned long long)oi->ip_blkno, |
912 | inode->i_nlink); | |
913 | ||
ccd979bd MF |
914 | /* While we were waiting for the cluster lock in |
915 | * ocfs2_delete_inode, another node might have asked to delete | |
916 | * the inode. Recheck our flags to catch this. */ | |
917 | if (!ocfs2_inode_is_valid_to_delete(inode)) { | |
64f3b269 | 918 | reason = 1; |
ccd979bd MF |
919 | goto bail; |
920 | } | |
921 | ||
922 | /* Now that we have an up to date inode, we can double check | |
923 | * the link count. */ | |
64f3b269 | 924 | if (inode->i_nlink) |
ccd979bd | 925 | goto bail; |
ccd979bd MF |
926 | |
927 | /* Do some basic inode verification... */ | |
928 | di = (struct ocfs2_dinode *) di_bh->b_data; | |
d4cd1871 LD |
929 | if (!(di->i_flags & cpu_to_le32(OCFS2_ORPHANED_FL)) && |
930 | !(oi->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) { | |
b54c2ca4 TY |
931 | /* |
932 | * Inodes in the orphan dir must have ORPHANED_FL. The only | |
933 | * inodes that come back out of the orphan dir are reflink | |
934 | * targets. A reflink target may be moved out of the orphan | |
935 | * dir between the time we scan the directory and the time we | |
936 | * process it. This would lead to HAS_REFCOUNT_FL being set but | |
937 | * ORPHANED_FL not. | |
938 | */ | |
939 | if (di->i_dyn_features & cpu_to_le16(OCFS2_HAS_REFCOUNT_FL)) { | |
64f3b269 | 940 | reason = 2; |
b54c2ca4 TY |
941 | goto bail; |
942 | } | |
943 | ||
ccd979bd MF |
944 | /* for lack of a better error? */ |
945 | status = -EEXIST; | |
946 | mlog(ML_ERROR, | |
b0697053 | 947 | "Inode %llu (on-disk %llu) not orphaned! " |
ccd979bd | 948 | "Disk flags 0x%x, inode flags 0x%x\n", |
b0697053 | 949 | (unsigned long long)oi->ip_blkno, |
1ca1a111 MF |
950 | (unsigned long long)le64_to_cpu(di->i_blkno), |
951 | le32_to_cpu(di->i_flags), oi->ip_flags); | |
ccd979bd MF |
952 | goto bail; |
953 | } | |
954 | ||
955 | /* has someone already deleted us?! baaad... */ | |
956 | if (di->i_dtime) { | |
957 | status = -EEXIST; | |
958 | mlog_errno(status); | |
959 | goto bail; | |
960 | } | |
961 | ||
6f16bf65 MF |
962 | /* |
963 | * This is how ocfs2 determines whether an inode is still live | |
964 | * within the cluster. Every node takes a shared read lock on | |
965 | * the inode open lock in ocfs2_read_locked_inode(). When we | |
966 | * get to ->delete_inode(), each node tries to convert it's | |
967 | * lock to an exclusive. Trylocks are serialized by the inode | |
25985edc | 968 | * meta data lock. If the upconvert succeeds, we know the inode |
6f16bf65 MF |
969 | * is no longer live and can be deleted. |
970 | * | |
971 | * Though we call this with the meta data lock held, the | |
972 | * trylock keeps us from ABBA deadlock. | |
973 | */ | |
974 | status = ocfs2_try_open_lock(inode, 1); | |
50008630 | 975 | if (status == -EAGAIN) { |
ccd979bd | 976 | status = 0; |
64f3b269 | 977 | reason = 3; |
ccd979bd MF |
978 | goto bail; |
979 | } | |
980 | if (status < 0) { | |
981 | mlog_errno(status); | |
982 | goto bail; | |
983 | } | |
984 | ||
50008630 | 985 | *wipe = 1; |
64f3b269 | 986 | trace_ocfs2_query_inode_wipe_succ(le16_to_cpu(di->i_orphaned_slot)); |
ccd979bd MF |
987 | |
988 | bail: | |
64f3b269 | 989 | trace_ocfs2_query_inode_wipe_end(status, reason); |
ccd979bd MF |
990 | return status; |
991 | } | |
992 | ||
993 | /* Support function for ocfs2_delete_inode. Will help us keep the | |
994 | * inode data in a consistent state for clear_inode. Always truncates | |
995 | * pages, optionally sync's them first. */ | |
996 | static void ocfs2_cleanup_delete_inode(struct inode *inode, | |
997 | int sync_data) | |
998 | { | |
64f3b269 TM |
999 | trace_ocfs2_cleanup_delete_inode( |
1000 | (unsigned long long)OCFS2_I(inode)->ip_blkno, sync_data); | |
ccd979bd | 1001 | if (sync_data) |
249ec93c | 1002 | filemap_write_and_wait(inode->i_mapping); |
91b0abe3 | 1003 | truncate_inode_pages_final(&inode->i_data); |
ccd979bd MF |
1004 | } |
1005 | ||
066d92dc | 1006 | static void ocfs2_delete_inode(struct inode *inode) |
ccd979bd MF |
1007 | { |
1008 | int wipe, status; | |
e4b963f1 | 1009 | sigset_t oldset; |
ccd979bd | 1010 | struct buffer_head *di_bh = NULL; |
ad694821 | 1011 | struct ocfs2_dinode *di = NULL; |
ccd979bd | 1012 | |
64f3b269 TM |
1013 | trace_ocfs2_delete_inode(inode->i_ino, |
1014 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
1015 | is_bad_inode(inode)); | |
ccd979bd | 1016 | |
a90714c1 JK |
1017 | /* When we fail in read_inode() we mark inode as bad. The second test |
1018 | * catches the case when inode allocation fails before allocating | |
1019 | * a block for inode. */ | |
64f3b269 | 1020 | if (is_bad_inode(inode) || !OCFS2_I(inode)->ip_blkno) |
ccd979bd | 1021 | goto bail; |
ccd979bd MF |
1022 | |
1023 | if (!ocfs2_inode_is_valid_to_delete(inode)) { | |
1024 | /* It's probably not necessary to truncate_inode_pages | |
1025 | * here but we do it for safety anyway (it will most | |
1026 | * likely be a no-op anyway) */ | |
1027 | ocfs2_cleanup_delete_inode(inode, 0); | |
1028 | goto bail; | |
1029 | } | |
1030 | ||
bd62ad7a JK |
1031 | dquot_initialize(inode); |
1032 | ||
ccd979bd MF |
1033 | /* We want to block signals in delete_inode as the lock and |
1034 | * messaging paths may return us -ERESTARTSYS. Which would | |
1035 | * cause us to exit early, resulting in inodes being orphaned | |
1036 | * forever. */ | |
e4b963f1 | 1037 | ocfs2_block_signals(&oldset); |
ccd979bd | 1038 | |
6ca497a8 | 1039 | /* |
1040 | * Synchronize us against ocfs2_get_dentry. We take this in | |
1041 | * shared mode so that all nodes can still concurrently | |
1042 | * process deletes. | |
1043 | */ | |
1044 | status = ocfs2_nfs_sync_lock(OCFS2_SB(inode->i_sb), 0); | |
1045 | if (status < 0) { | |
1046 | mlog(ML_ERROR, "getting nfs sync lock(PR) failed %d\n", status); | |
1047 | ocfs2_cleanup_delete_inode(inode, 0); | |
1048 | goto bail_unblock; | |
1049 | } | |
ccd979bd MF |
1050 | /* Lock down the inode. This gives us an up to date view of |
1051 | * it's metadata (for verification), and allows us to | |
34d024f8 | 1052 | * serialize delete_inode on multiple nodes. |
ccd979bd MF |
1053 | * |
1054 | * Even though we might be doing a truncate, we don't take the | |
1055 | * allocation lock here as it won't be needed - nobody will | |
1056 | * have the file open. | |
1057 | */ | |
e63aecb6 | 1058 | status = ocfs2_inode_lock(inode, &di_bh, 1); |
ccd979bd MF |
1059 | if (status < 0) { |
1060 | if (status != -ENOENT) | |
1061 | mlog_errno(status); | |
1062 | ocfs2_cleanup_delete_inode(inode, 0); | |
6ca497a8 | 1063 | goto bail_unlock_nfs_sync; |
ccd979bd MF |
1064 | } |
1065 | ||
ad694821 JQ |
1066 | di = (struct ocfs2_dinode *)di_bh->b_data; |
1067 | /* Skip inode deletion and wait for dio orphan entry recovered | |
1068 | * first */ | |
1069 | if (unlikely(di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL))) { | |
1070 | ocfs2_cleanup_delete_inode(inode, 0); | |
1071 | goto bail_unlock_inode; | |
1072 | } | |
1073 | ||
ccd979bd MF |
1074 | /* Query the cluster. This will be the final decision made |
1075 | * before we go ahead and wipe the inode. */ | |
1076 | status = ocfs2_query_inode_wipe(inode, di_bh, &wipe); | |
1077 | if (!wipe || status < 0) { | |
34d024f8 | 1078 | /* Error and remote inode busy both mean we won't be |
ccd979bd MF |
1079 | * removing the inode, so they take almost the same |
1080 | * path. */ | |
1081 | if (status < 0) | |
1082 | mlog_errno(status); | |
1083 | ||
34d024f8 MF |
1084 | /* Someone in the cluster has disallowed a wipe of |
1085 | * this inode, or it was never completely | |
1086 | * orphaned. Write out the pages and exit now. */ | |
ccd979bd MF |
1087 | ocfs2_cleanup_delete_inode(inode, 1); |
1088 | goto bail_unlock_inode; | |
1089 | } | |
1090 | ||
1091 | ocfs2_cleanup_delete_inode(inode, 0); | |
1092 | ||
1093 | status = ocfs2_wipe_inode(inode, di_bh); | |
1094 | if (status < 0) { | |
b4df6ed8 MF |
1095 | if (status != -EDEADLK) |
1096 | mlog_errno(status); | |
ccd979bd MF |
1097 | goto bail_unlock_inode; |
1098 | } | |
1099 | ||
24c19ef4 MF |
1100 | /* |
1101 | * Mark the inode as successfully deleted. | |
1102 | * | |
1103 | * This is important for ocfs2_clear_inode() as it will check | |
1104 | * this flag and skip any checkpointing work | |
1105 | * | |
1106 | * ocfs2_stuff_meta_lvb() also uses this flag to invalidate | |
1107 | * the LVB for other nodes. | |
1108 | */ | |
ccd979bd MF |
1109 | OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED; |
1110 | ||
1111 | bail_unlock_inode: | |
e63aecb6 | 1112 | ocfs2_inode_unlock(inode, 1); |
ccd979bd | 1113 | brelse(di_bh); |
6ca497a8 | 1114 | |
1115 | bail_unlock_nfs_sync: | |
1116 | ocfs2_nfs_sync_unlock(OCFS2_SB(inode->i_sb), 0); | |
1117 | ||
ccd979bd | 1118 | bail_unblock: |
e4b963f1 | 1119 | ocfs2_unblock_signals(&oldset); |
ccd979bd | 1120 | bail: |
c1e8d35e | 1121 | return; |
ccd979bd MF |
1122 | } |
1123 | ||
066d92dc | 1124 | static void ocfs2_clear_inode(struct inode *inode) |
ccd979bd MF |
1125 | { |
1126 | int status; | |
1127 | struct ocfs2_inode_info *oi = OCFS2_I(inode); | |
84d86f83 | 1128 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
ccd979bd | 1129 | |
dbd5768f | 1130 | clear_inode(inode); |
64f3b269 TM |
1131 | trace_ocfs2_clear_inode((unsigned long long)oi->ip_blkno, |
1132 | inode->i_nlink); | |
ccd979bd | 1133 | |
1119d3c0 | 1134 | mlog_bug_on_msg(osb == NULL, |
ccd979bd MF |
1135 | "Inode=%lu\n", inode->i_ino); |
1136 | ||
9f754758 | 1137 | dquot_drop(inode); |
257ba15c | 1138 | |
a0f8a9a9 | 1139 | /* To prevent remote deletes we hold open lock before, now it |
34d024f8 | 1140 | * is time to unlock PR and EX open locks. */ |
50008630 TY |
1141 | ocfs2_open_unlock(inode); |
1142 | ||
ccd979bd | 1143 | /* Do these before all the other work so that we don't bounce |
34d024f8 | 1144 | * the downconvert thread while waiting to destroy the locks. */ |
84d86f83 JK |
1145 | ocfs2_mark_lockres_freeing(osb, &oi->ip_rw_lockres); |
1146 | ocfs2_mark_lockres_freeing(osb, &oi->ip_inode_lockres); | |
1147 | ocfs2_mark_lockres_freeing(osb, &oi->ip_open_lockres); | |
ccd979bd | 1148 | |
1119d3c0 | 1149 | ocfs2_resv_discard(&osb->osb_la_resmap, |
4fe370af MF |
1150 | &oi->ip_la_data_resv); |
1151 | ocfs2_resv_init_once(&oi->ip_la_data_resv); | |
1152 | ||
ccd979bd MF |
1153 | /* We very well may get a clear_inode before all an inodes |
1154 | * metadata has hit disk. Of course, we can't drop any cluster | |
1155 | * locks until the journal has finished with it. The only | |
1156 | * exception here are successfully wiped inodes - their | |
1157 | * metadata can now be considered to be part of the system | |
1158 | * inodes from which it came. */ | |
d324cd4c | 1159 | if (!(oi->ip_flags & OCFS2_INODE_DELETED)) |
ccd979bd MF |
1160 | ocfs2_checkpoint_inode(inode); |
1161 | ||
1162 | mlog_bug_on_msg(!list_empty(&oi->ip_io_markers), | |
b0697053 MF |
1163 | "Clear inode of %llu, inode has io markers\n", |
1164 | (unsigned long long)oi->ip_blkno); | |
4506cfb6 RD |
1165 | mlog_bug_on_msg(!list_empty(&oi->ip_unwritten_list), |
1166 | "Clear inode of %llu, inode has unwritten extents\n", | |
1167 | (unsigned long long)oi->ip_blkno); | |
ccd979bd | 1168 | |
83418978 MF |
1169 | ocfs2_extent_map_trunc(inode, 0); |
1170 | ||
ccd979bd MF |
1171 | status = ocfs2_drop_inode_locks(inode); |
1172 | if (status < 0) | |
1173 | mlog_errno(status); | |
1174 | ||
1175 | ocfs2_lock_res_free(&oi->ip_rw_lockres); | |
e63aecb6 | 1176 | ocfs2_lock_res_free(&oi->ip_inode_lockres); |
50008630 | 1177 | ocfs2_lock_res_free(&oi->ip_open_lockres); |
ccd979bd | 1178 | |
66fb345d | 1179 | ocfs2_metadata_cache_exit(INODE_CACHE(inode)); |
ccd979bd | 1180 | |
8cb471e8 | 1181 | mlog_bug_on_msg(INODE_CACHE(inode)->ci_num_cached, |
b0697053 | 1182 | "Clear inode of %llu, inode has %u cache items\n", |
8cb471e8 JB |
1183 | (unsigned long long)oi->ip_blkno, |
1184 | INODE_CACHE(inode)->ci_num_cached); | |
ccd979bd | 1185 | |
8cb471e8 | 1186 | mlog_bug_on_msg(!(INODE_CACHE(inode)->ci_flags & OCFS2_CACHE_FL_INLINE), |
b0697053 MF |
1187 | "Clear inode of %llu, inode has a bad flag\n", |
1188 | (unsigned long long)oi->ip_blkno); | |
ccd979bd MF |
1189 | |
1190 | mlog_bug_on_msg(spin_is_locked(&oi->ip_lock), | |
b0697053 MF |
1191 | "Clear inode of %llu, inode is locked\n", |
1192 | (unsigned long long)oi->ip_blkno); | |
ccd979bd | 1193 | |
251b6ecc | 1194 | mlog_bug_on_msg(!mutex_trylock(&oi->ip_io_mutex), |
b0697053 MF |
1195 | "Clear inode of %llu, io_mutex is locked\n", |
1196 | (unsigned long long)oi->ip_blkno); | |
251b6ecc | 1197 | mutex_unlock(&oi->ip_io_mutex); |
ccd979bd MF |
1198 | |
1199 | /* | |
1200 | * down_trylock() returns 0, down_write_trylock() returns 1 | |
1201 | * kernel 1, world 0 | |
1202 | */ | |
1203 | mlog_bug_on_msg(!down_write_trylock(&oi->ip_alloc_sem), | |
b0697053 MF |
1204 | "Clear inode of %llu, alloc_sem is locked\n", |
1205 | (unsigned long long)oi->ip_blkno); | |
ccd979bd MF |
1206 | up_write(&oi->ip_alloc_sem); |
1207 | ||
1208 | mlog_bug_on_msg(oi->ip_open_count, | |
b0697053 MF |
1209 | "Clear inode of %llu has open count %d\n", |
1210 | (unsigned long long)oi->ip_blkno, oi->ip_open_count); | |
ccd979bd MF |
1211 | |
1212 | /* Clear all other flags. */ | |
47460d65 | 1213 | oi->ip_flags = 0; |
ccd979bd MF |
1214 | oi->ip_dir_start_lookup = 0; |
1215 | oi->ip_blkno = 0ULL; | |
ae0dff68 SM |
1216 | |
1217 | /* | |
1218 | * ip_jinode is used to track txns against this inode. We ensure that | |
1219 | * the journal is flushed before journal shutdown. Thus it is safe to | |
1220 | * have inodes get cleaned up after journal shutdown. | |
1221 | */ | |
1119d3c0 | 1222 | jbd2_journal_release_jbd_inode(osb->journal->j_journal, |
2b4e30fb | 1223 | &oi->ip_jinode); |
ccd979bd MF |
1224 | } |
1225 | ||
066d92dc AV |
1226 | void ocfs2_evict_inode(struct inode *inode) |
1227 | { | |
1228 | if (!inode->i_nlink || | |
1229 | (OCFS2_I(inode)->ip_flags & OCFS2_INODE_MAYBE_ORPHANED)) { | |
1230 | ocfs2_delete_inode(inode); | |
1231 | } else { | |
91b0abe3 | 1232 | truncate_inode_pages_final(&inode->i_data); |
066d92dc AV |
1233 | } |
1234 | ocfs2_clear_inode(inode); | |
1235 | } | |
1236 | ||
ccd979bd MF |
1237 | /* Called under inode_lock, with no more references on the |
1238 | * struct inode, so it's safe here to check the flags field | |
1239 | * and to manipulate i_nlink without any other locks. */ | |
45321ac5 | 1240 | int ocfs2_drop_inode(struct inode *inode) |
ccd979bd MF |
1241 | { |
1242 | struct ocfs2_inode_info *oi = OCFS2_I(inode); | |
1243 | ||
64f3b269 TM |
1244 | trace_ocfs2_drop_inode((unsigned long long)oi->ip_blkno, |
1245 | inode->i_nlink, oi->ip_flags); | |
ccd979bd | 1246 | |
513e2dae X |
1247 | assert_spin_locked(&inode->i_lock); |
1248 | inode->i_state |= I_WILL_FREE; | |
1249 | spin_unlock(&inode->i_lock); | |
1250 | write_inode_now(inode, 1); | |
1251 | spin_lock(&inode->i_lock); | |
1252 | WARN_ON(inode->i_state & I_NEW); | |
1253 | inode->i_state &= ~I_WILL_FREE; | |
ccd979bd | 1254 | |
513e2dae | 1255 | return 1; |
ccd979bd MF |
1256 | } |
1257 | ||
ccd979bd MF |
1258 | /* |
1259 | * This is called from our getattr. | |
1260 | */ | |
1261 | int ocfs2_inode_revalidate(struct dentry *dentry) | |
1262 | { | |
2b0143b5 | 1263 | struct inode *inode = d_inode(dentry); |
ccd979bd MF |
1264 | int status = 0; |
1265 | ||
64f3b269 TM |
1266 | trace_ocfs2_inode_revalidate(inode, |
1267 | inode ? (unsigned long long)OCFS2_I(inode)->ip_blkno : 0ULL, | |
1268 | inode ? (unsigned long long)OCFS2_I(inode)->ip_flags : 0); | |
ccd979bd MF |
1269 | |
1270 | if (!inode) { | |
ccd979bd MF |
1271 | status = -ENOENT; |
1272 | goto bail; | |
1273 | } | |
1274 | ||
1275 | spin_lock(&OCFS2_I(inode)->ip_lock); | |
1276 | if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) { | |
1277 | spin_unlock(&OCFS2_I(inode)->ip_lock); | |
ccd979bd MF |
1278 | status = -ENOENT; |
1279 | goto bail; | |
1280 | } | |
1281 | spin_unlock(&OCFS2_I(inode)->ip_lock); | |
1282 | ||
e63aecb6 | 1283 | /* Let ocfs2_inode_lock do the work of updating our struct |
ccd979bd | 1284 | * inode for us. */ |
e63aecb6 | 1285 | status = ocfs2_inode_lock(inode, NULL, 0); |
ccd979bd MF |
1286 | if (status < 0) { |
1287 | if (status != -ENOENT) | |
1288 | mlog_errno(status); | |
1289 | goto bail; | |
1290 | } | |
e63aecb6 | 1291 | ocfs2_inode_unlock(inode, 0); |
ccd979bd | 1292 | bail: |
ccd979bd MF |
1293 | return status; |
1294 | } | |
1295 | ||
1296 | /* | |
1297 | * Updates a disk inode from a | |
1298 | * struct inode. | |
1299 | * Only takes ip_lock. | |
1300 | */ | |
1fabe148 | 1301 | int ocfs2_mark_inode_dirty(handle_t *handle, |
ccd979bd MF |
1302 | struct inode *inode, |
1303 | struct buffer_head *bh) | |
1304 | { | |
1305 | int status; | |
1306 | struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data; | |
1307 | ||
64f3b269 | 1308 | trace_ocfs2_mark_inode_dirty((unsigned long long)OCFS2_I(inode)->ip_blkno); |
ccd979bd | 1309 | |
0cf2f763 | 1310 | status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh, |
13723d00 | 1311 | OCFS2_JOURNAL_ACCESS_WRITE); |
ccd979bd MF |
1312 | if (status < 0) { |
1313 | mlog_errno(status); | |
1314 | goto leave; | |
1315 | } | |
1316 | ||
1317 | spin_lock(&OCFS2_I(inode)->ip_lock); | |
1318 | fe->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters); | |
6e4b0d56 | 1319 | ocfs2_get_inode_flags(OCFS2_I(inode)); |
ca4d147e | 1320 | fe->i_attr = cpu_to_le32(OCFS2_I(inode)->ip_attr); |
15b1e36b | 1321 | fe->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features); |
ccd979bd MF |
1322 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
1323 | ||
1324 | fe->i_size = cpu_to_le64(i_size_read(inode)); | |
198a1ca3 | 1325 | ocfs2_set_links_count(fe, inode->i_nlink); |
2c034176 EB |
1326 | fe->i_uid = cpu_to_le32(i_uid_read(inode)); |
1327 | fe->i_gid = cpu_to_le32(i_gid_read(inode)); | |
ccd979bd | 1328 | fe->i_mode = cpu_to_le16(inode->i_mode); |
fd6acbbc JL |
1329 | fe->i_atime = cpu_to_le64(inode_get_atime_sec(inode)); |
1330 | fe->i_atime_nsec = cpu_to_le32(inode_get_atime_nsec(inode)); | |
1331 | fe->i_ctime = cpu_to_le64(inode_get_ctime_sec(inode)); | |
1332 | fe->i_ctime_nsec = cpu_to_le32(inode_get_ctime_nsec(inode)); | |
1333 | fe->i_mtime = cpu_to_le64(inode_get_mtime_sec(inode)); | |
1334 | fe->i_mtime_nsec = cpu_to_le32(inode_get_mtime_nsec(inode)); | |
ccd979bd | 1335 | |
ec20cec7 | 1336 | ocfs2_journal_dirty(handle, bh); |
2931cdcb | 1337 | ocfs2_update_inode_fsync_trans(handle, inode, 1); |
ccd979bd | 1338 | leave: |
ccd979bd MF |
1339 | return status; |
1340 | } | |
1341 | ||
1342 | /* | |
1343 | * | |
1344 | * Updates a struct inode from a disk inode. | |
1345 | * does no i/o, only takes ip_lock. | |
1346 | */ | |
1347 | void ocfs2_refresh_inode(struct inode *inode, | |
1348 | struct ocfs2_dinode *fe) | |
1349 | { | |
ccd979bd MF |
1350 | spin_lock(&OCFS2_I(inode)->ip_lock); |
1351 | ||
1352 | OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters); | |
ca4d147e | 1353 | OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr); |
15b1e36b | 1354 | OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features); |
ca4d147e | 1355 | ocfs2_set_inode_flags(inode); |
ccd979bd | 1356 | i_size_write(inode, le64_to_cpu(fe->i_size)); |
bfe86848 | 1357 | set_nlink(inode, ocfs2_read_links_count(fe)); |
2c034176 EB |
1358 | i_uid_write(inode, le32_to_cpu(fe->i_uid)); |
1359 | i_gid_write(inode, le32_to_cpu(fe->i_gid)); | |
ccd979bd | 1360 | inode->i_mode = le16_to_cpu(fe->i_mode); |
ccd979bd MF |
1361 | if (S_ISLNK(inode->i_mode) && le32_to_cpu(fe->i_clusters) == 0) |
1362 | inode->i_blocks = 0; | |
1363 | else | |
8110b073 | 1364 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
fd6acbbc JL |
1365 | inode_set_atime(inode, le64_to_cpu(fe->i_atime), |
1366 | le32_to_cpu(fe->i_atime_nsec)); | |
1367 | inode_set_mtime(inode, le64_to_cpu(fe->i_mtime), | |
1368 | le32_to_cpu(fe->i_mtime_nsec)); | |
6861de97 JL |
1369 | inode_set_ctime(inode, le64_to_cpu(fe->i_ctime), |
1370 | le32_to_cpu(fe->i_ctime_nsec)); | |
ccd979bd MF |
1371 | |
1372 | spin_unlock(&OCFS2_I(inode)->ip_lock); | |
1373 | } | |
b657c95c JB |
1374 | |
1375 | int ocfs2_validate_inode_block(struct super_block *sb, | |
1376 | struct buffer_head *bh) | |
1377 | { | |
d6b32bbb | 1378 | int rc; |
b657c95c JB |
1379 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data; |
1380 | ||
64f3b269 | 1381 | trace_ocfs2_validate_inode_block((unsigned long long)bh->b_blocknr); |
970e4936 | 1382 | |
b657c95c JB |
1383 | BUG_ON(!buffer_uptodate(bh)); |
1384 | ||
d6b32bbb JB |
1385 | /* |
1386 | * If the ecc fails, we return the error but otherwise | |
1387 | * leave the filesystem running. We know any error is | |
1388 | * local to this block. | |
1389 | */ | |
1390 | rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check); | |
13723d00 JB |
1391 | if (rc) { |
1392 | mlog(ML_ERROR, "Checksum failed for dinode %llu\n", | |
1393 | (unsigned long long)bh->b_blocknr); | |
d6b32bbb | 1394 | goto bail; |
13723d00 | 1395 | } |
d6b32bbb JB |
1396 | |
1397 | /* | |
1398 | * Errors after here are fatal. | |
1399 | */ | |
1400 | ||
1401 | rc = -EINVAL; | |
1402 | ||
b657c95c | 1403 | if (!OCFS2_IS_VALID_DINODE(di)) { |
17a5b9ab | 1404 | rc = ocfs2_error(sb, "Invalid dinode #%llu: signature = %.*s\n", |
7ecef14a JP |
1405 | (unsigned long long)bh->b_blocknr, 7, |
1406 | di->i_signature); | |
b657c95c JB |
1407 | goto bail; |
1408 | } | |
1409 | ||
1410 | if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) { | |
17a5b9ab | 1411 | rc = ocfs2_error(sb, "Invalid dinode #%llu: i_blkno is %llu\n", |
7ecef14a JP |
1412 | (unsigned long long)bh->b_blocknr, |
1413 | (unsigned long long)le64_to_cpu(di->i_blkno)); | |
b657c95c JB |
1414 | goto bail; |
1415 | } | |
1416 | ||
1417 | if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) { | |
17a5b9ab | 1418 | rc = ocfs2_error(sb, |
7ecef14a JP |
1419 | "Invalid dinode #%llu: OCFS2_VALID_FL not set\n", |
1420 | (unsigned long long)bh->b_blocknr); | |
b657c95c JB |
1421 | goto bail; |
1422 | } | |
1423 | ||
1424 | if (le32_to_cpu(di->i_fs_generation) != | |
1425 | OCFS2_SB(sb)->fs_generation) { | |
17a5b9ab | 1426 | rc = ocfs2_error(sb, |
7ecef14a JP |
1427 | "Invalid dinode #%llu: fs_generation is %u\n", |
1428 | (unsigned long long)bh->b_blocknr, | |
1429 | le32_to_cpu(di->i_fs_generation)); | |
b657c95c JB |
1430 | goto bail; |
1431 | } | |
1432 | ||
1433 | rc = 0; | |
1434 | ||
1435 | bail: | |
1436 | return rc; | |
1437 | } | |
1438 | ||
d56a8f32 GH |
1439 | static int ocfs2_filecheck_validate_inode_block(struct super_block *sb, |
1440 | struct buffer_head *bh) | |
1441 | { | |
1442 | int rc = 0; | |
1443 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data; | |
1444 | ||
1445 | trace_ocfs2_filecheck_validate_inode_block( | |
1446 | (unsigned long long)bh->b_blocknr); | |
1447 | ||
1448 | BUG_ON(!buffer_uptodate(bh)); | |
1449 | ||
1450 | /* | |
1451 | * Call ocfs2_validate_meta_ecc() first since it has ecc repair | |
1452 | * function, but we should not return error immediately when ecc | |
1453 | * validation fails, because the reason is quite likely the invalid | |
a0f8a9a9 | 1454 | * inode number inputted. |
d56a8f32 GH |
1455 | */ |
1456 | rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check); | |
1457 | if (rc) { | |
1458 | mlog(ML_ERROR, | |
1459 | "Filecheck: checksum failed for dinode %llu\n", | |
1460 | (unsigned long long)bh->b_blocknr); | |
1461 | rc = -OCFS2_FILECHECK_ERR_BLOCKECC; | |
1462 | } | |
1463 | ||
1464 | if (!OCFS2_IS_VALID_DINODE(di)) { | |
1465 | mlog(ML_ERROR, | |
1466 | "Filecheck: invalid dinode #%llu: signature = %.*s\n", | |
1467 | (unsigned long long)bh->b_blocknr, 7, di->i_signature); | |
1468 | rc = -OCFS2_FILECHECK_ERR_INVALIDINO; | |
1469 | goto bail; | |
1470 | } else if (rc) | |
1471 | goto bail; | |
1472 | ||
1473 | if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) { | |
1474 | mlog(ML_ERROR, | |
1475 | "Filecheck: invalid dinode #%llu: i_blkno is %llu\n", | |
1476 | (unsigned long long)bh->b_blocknr, | |
1477 | (unsigned long long)le64_to_cpu(di->i_blkno)); | |
1478 | rc = -OCFS2_FILECHECK_ERR_BLOCKNO; | |
1479 | goto bail; | |
1480 | } | |
1481 | ||
1482 | if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) { | |
1483 | mlog(ML_ERROR, | |
1484 | "Filecheck: invalid dinode #%llu: OCFS2_VALID_FL " | |
1485 | "not set\n", | |
1486 | (unsigned long long)bh->b_blocknr); | |
1487 | rc = -OCFS2_FILECHECK_ERR_VALIDFLAG; | |
1488 | goto bail; | |
1489 | } | |
1490 | ||
1491 | if (le32_to_cpu(di->i_fs_generation) != | |
1492 | OCFS2_SB(sb)->fs_generation) { | |
1493 | mlog(ML_ERROR, | |
1494 | "Filecheck: invalid dinode #%llu: fs_generation is %u\n", | |
1495 | (unsigned long long)bh->b_blocknr, | |
1496 | le32_to_cpu(di->i_fs_generation)); | |
1497 | rc = -OCFS2_FILECHECK_ERR_GENERATION; | |
d56a8f32 GH |
1498 | } |
1499 | ||
1500 | bail: | |
1501 | return rc; | |
1502 | } | |
1503 | ||
1504 | static int ocfs2_filecheck_repair_inode_block(struct super_block *sb, | |
1505 | struct buffer_head *bh) | |
1506 | { | |
1507 | int changed = 0; | |
1508 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data; | |
1509 | ||
1510 | if (!ocfs2_filecheck_validate_inode_block(sb, bh)) | |
1511 | return 0; | |
1512 | ||
1513 | trace_ocfs2_filecheck_repair_inode_block( | |
1514 | (unsigned long long)bh->b_blocknr); | |
1515 | ||
1516 | if (ocfs2_is_hard_readonly(OCFS2_SB(sb)) || | |
1517 | ocfs2_is_soft_readonly(OCFS2_SB(sb))) { | |
1518 | mlog(ML_ERROR, | |
1519 | "Filecheck: cannot repair dinode #%llu " | |
1520 | "on readonly filesystem\n", | |
1521 | (unsigned long long)bh->b_blocknr); | |
1522 | return -OCFS2_FILECHECK_ERR_READONLY; | |
1523 | } | |
1524 | ||
1525 | if (buffer_jbd(bh)) { | |
1526 | mlog(ML_ERROR, | |
1527 | "Filecheck: cannot repair dinode #%llu, " | |
1528 | "its buffer is in jbd\n", | |
1529 | (unsigned long long)bh->b_blocknr); | |
1530 | return -OCFS2_FILECHECK_ERR_INJBD; | |
1531 | } | |
1532 | ||
1533 | if (!OCFS2_IS_VALID_DINODE(di)) { | |
1534 | /* Cannot fix invalid inode block */ | |
1535 | return -OCFS2_FILECHECK_ERR_INVALIDINO; | |
1536 | } | |
1537 | ||
1538 | if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) { | |
1539 | /* Cannot just add VALID_FL flag back as a fix, | |
1540 | * need more things to check here. | |
1541 | */ | |
1542 | return -OCFS2_FILECHECK_ERR_VALIDFLAG; | |
1543 | } | |
1544 | ||
1545 | if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) { | |
1546 | di->i_blkno = cpu_to_le64(bh->b_blocknr); | |
1547 | changed = 1; | |
1548 | mlog(ML_ERROR, | |
1549 | "Filecheck: reset dinode #%llu: i_blkno to %llu\n", | |
1550 | (unsigned long long)bh->b_blocknr, | |
1551 | (unsigned long long)le64_to_cpu(di->i_blkno)); | |
1552 | } | |
1553 | ||
1554 | if (le32_to_cpu(di->i_fs_generation) != | |
1555 | OCFS2_SB(sb)->fs_generation) { | |
1556 | di->i_fs_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation); | |
1557 | changed = 1; | |
1558 | mlog(ML_ERROR, | |
1559 | "Filecheck: reset dinode #%llu: fs_generation to %u\n", | |
1560 | (unsigned long long)bh->b_blocknr, | |
1561 | le32_to_cpu(di->i_fs_generation)); | |
1562 | } | |
1563 | ||
fdbb6cd9 JS |
1564 | if (ocfs2_dinode_has_extents(di) && |
1565 | le16_to_cpu(di->id2.i_list.l_next_free_rec) > le16_to_cpu(di->id2.i_list.l_count)) { | |
1566 | di->id2.i_list.l_next_free_rec = di->id2.i_list.l_count; | |
1567 | changed = 1; | |
1568 | mlog(ML_ERROR, | |
1569 | "Filecheck: reset dinode #%llu: l_next_free_rec to %u\n", | |
1570 | (unsigned long long)bh->b_blocknr, | |
1571 | le16_to_cpu(di->id2.i_list.l_next_free_rec)); | |
1572 | } | |
1573 | ||
d56a8f32 GH |
1574 | if (changed || ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check)) { |
1575 | ocfs2_compute_meta_ecc(sb, bh->b_data, &di->i_check); | |
1576 | mark_buffer_dirty(bh); | |
1577 | mlog(ML_ERROR, | |
1578 | "Filecheck: reset dinode #%llu: compute meta ecc\n", | |
1579 | (unsigned long long)bh->b_blocknr); | |
1580 | } | |
1581 | ||
1582 | return 0; | |
1583 | } | |
1584 | ||
1585 | static int | |
1586 | ocfs2_filecheck_read_inode_block_full(struct inode *inode, | |
1587 | struct buffer_head **bh, | |
1588 | int flags, int type) | |
1589 | { | |
1590 | int rc; | |
1591 | struct buffer_head *tmp = *bh; | |
1592 | ||
1593 | if (!type) /* Check inode block */ | |
1594 | rc = ocfs2_read_blocks(INODE_CACHE(inode), | |
1595 | OCFS2_I(inode)->ip_blkno, | |
1596 | 1, &tmp, flags, | |
1597 | ocfs2_filecheck_validate_inode_block); | |
1598 | else /* Repair inode block */ | |
1599 | rc = ocfs2_read_blocks(INODE_CACHE(inode), | |
1600 | OCFS2_I(inode)->ip_blkno, | |
1601 | 1, &tmp, flags, | |
1602 | ocfs2_filecheck_repair_inode_block); | |
1603 | ||
1604 | /* If ocfs2_read_blocks() got us a new bh, pass it up. */ | |
1605 | if (!rc && !*bh) | |
1606 | *bh = tmp; | |
1607 | ||
1608 | return rc; | |
1609 | } | |
1610 | ||
b657c95c JB |
1611 | int ocfs2_read_inode_block_full(struct inode *inode, struct buffer_head **bh, |
1612 | int flags) | |
1613 | { | |
1614 | int rc; | |
1615 | struct buffer_head *tmp = *bh; | |
1616 | ||
8cb471e8 JB |
1617 | rc = ocfs2_read_blocks(INODE_CACHE(inode), OCFS2_I(inode)->ip_blkno, |
1618 | 1, &tmp, flags, ocfs2_validate_inode_block); | |
b657c95c JB |
1619 | |
1620 | /* If ocfs2_read_blocks() got us a new bh, pass it up. */ | |
970e4936 | 1621 | if (!rc && !*bh) |
b657c95c JB |
1622 | *bh = tmp; |
1623 | ||
b657c95c JB |
1624 | return rc; |
1625 | } | |
1626 | ||
1627 | int ocfs2_read_inode_block(struct inode *inode, struct buffer_head **bh) | |
1628 | { | |
1629 | return ocfs2_read_inode_block_full(inode, bh, 0); | |
1630 | } | |
6e5a3d75 | 1631 | |
6e5a3d75 JB |
1632 | |
1633 | static u64 ocfs2_inode_cache_owner(struct ocfs2_caching_info *ci) | |
1634 | { | |
1635 | struct ocfs2_inode_info *oi = cache_info_to_inode(ci); | |
1636 | ||
1637 | return oi->ip_blkno; | |
1638 | } | |
1639 | ||
8cb471e8 JB |
1640 | static struct super_block *ocfs2_inode_cache_get_super(struct ocfs2_caching_info *ci) |
1641 | { | |
1642 | struct ocfs2_inode_info *oi = cache_info_to_inode(ci); | |
1643 | ||
1644 | return oi->vfs_inode.i_sb; | |
1645 | } | |
1646 | ||
6e5a3d75 | 1647 | static void ocfs2_inode_cache_lock(struct ocfs2_caching_info *ci) |
fc07d2a2 | 1648 | __acquires(&oi->ip_lock) |
6e5a3d75 JB |
1649 | { |
1650 | struct ocfs2_inode_info *oi = cache_info_to_inode(ci); | |
1651 | ||
1652 | spin_lock(&oi->ip_lock); | |
1653 | } | |
1654 | ||
1655 | static void ocfs2_inode_cache_unlock(struct ocfs2_caching_info *ci) | |
fc07d2a2 | 1656 | __releases(&oi->ip_lock) |
6e5a3d75 JB |
1657 | { |
1658 | struct ocfs2_inode_info *oi = cache_info_to_inode(ci); | |
1659 | ||
1660 | spin_unlock(&oi->ip_lock); | |
1661 | } | |
1662 | ||
1663 | static void ocfs2_inode_cache_io_lock(struct ocfs2_caching_info *ci) | |
1664 | { | |
1665 | struct ocfs2_inode_info *oi = cache_info_to_inode(ci); | |
1666 | ||
1667 | mutex_lock(&oi->ip_io_mutex); | |
1668 | } | |
1669 | ||
1670 | static void ocfs2_inode_cache_io_unlock(struct ocfs2_caching_info *ci) | |
1671 | { | |
1672 | struct ocfs2_inode_info *oi = cache_info_to_inode(ci); | |
1673 | ||
1674 | mutex_unlock(&oi->ip_io_mutex); | |
1675 | } | |
1676 | ||
1677 | const struct ocfs2_caching_operations ocfs2_inode_caching_ops = { | |
1678 | .co_owner = ocfs2_inode_cache_owner, | |
8cb471e8 | 1679 | .co_get_super = ocfs2_inode_cache_get_super, |
6e5a3d75 JB |
1680 | .co_cache_lock = ocfs2_inode_cache_lock, |
1681 | .co_cache_unlock = ocfs2_inode_cache_unlock, | |
1682 | .co_io_lock = ocfs2_inode_cache_io_lock, | |
1683 | .co_io_unlock = ocfs2_inode_cache_io_unlock, | |
1684 | }; | |
1685 |