Commit | Line | Data |
---|---|---|
ccd979bd MF |
1 | /* -*- mode: c; c-basic-offset: 8; -*- |
2 | * vim: noexpandtab sw=8 ts=8 sts=0: | |
3 | * | |
4 | * inode.c | |
5 | * | |
6 | * vfs' aops, fops, dops and iops | |
7 | * | |
8 | * Copyright (C) 2002, 2004 Oracle. All rights reserved. | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or | |
11 | * modify it under the terms of the GNU General Public | |
12 | * License as published by the Free Software Foundation; either | |
13 | * version 2 of the License, or (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 | * General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public | |
21 | * License along with this program; if not, write to the | |
22 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 | * Boston, MA 021110-1307, USA. | |
24 | */ | |
25 | ||
26 | #include <linux/fs.h> | |
27 | #include <linux/types.h> | |
28 | #include <linux/slab.h> | |
29 | #include <linux/highmem.h> | |
30 | #include <linux/pagemap.h> | |
a90714c1 | 31 | #include <linux/quotaops.h> |
ccd979bd MF |
32 | |
33 | #include <asm/byteorder.h> | |
34 | ||
35 | #define MLOG_MASK_PREFIX ML_INODE | |
36 | #include <cluster/masklog.h> | |
37 | ||
38 | #include "ocfs2.h" | |
39 | ||
40 | #include "alloc.h" | |
41 | #include "dlmglue.h" | |
42 | #include "extent_map.h" | |
43 | #include "file.h" | |
b4df6ed8 | 44 | #include "heartbeat.h" |
ccd979bd MF |
45 | #include "inode.h" |
46 | #include "journal.h" | |
47 | #include "namei.h" | |
48 | #include "suballoc.h" | |
49 | #include "super.h" | |
50 | #include "symlink.h" | |
51 | #include "sysfile.h" | |
52 | #include "uptodate.h" | |
cf1d6c76 | 53 | #include "xattr.h" |
ccd979bd MF |
54 | |
55 | #include "buffer_head_io.h" | |
56 | ||
ccd979bd MF |
57 | struct ocfs2_find_inode_args |
58 | { | |
59 | u64 fi_blkno; | |
60 | unsigned long fi_ino; | |
61 | unsigned int fi_flags; | |
5fa0613e | 62 | unsigned int fi_sysfile_type; |
ccd979bd MF |
63 | }; |
64 | ||
5fa0613e JK |
65 | static struct lock_class_key ocfs2_sysfile_lock_key[NUM_SYSTEM_INODES]; |
66 | ||
ccd979bd MF |
67 | static int ocfs2_read_locked_inode(struct inode *inode, |
68 | struct ocfs2_find_inode_args *args); | |
69 | static int ocfs2_init_locked_inode(struct inode *inode, void *opaque); | |
70 | static int ocfs2_find_actor(struct inode *inode, void *opaque); | |
71 | static int ocfs2_truncate_for_delete(struct ocfs2_super *osb, | |
72 | struct inode *inode, | |
73 | struct buffer_head *fe_bh); | |
74 | ||
ca4d147e HP |
75 | void ocfs2_set_inode_flags(struct inode *inode) |
76 | { | |
77 | unsigned int flags = OCFS2_I(inode)->ip_attr; | |
78 | ||
79 | inode->i_flags &= ~(S_IMMUTABLE | | |
80 | S_SYNC | S_APPEND | S_NOATIME | S_DIRSYNC); | |
81 | ||
82 | if (flags & OCFS2_IMMUTABLE_FL) | |
83 | inode->i_flags |= S_IMMUTABLE; | |
84 | ||
85 | if (flags & OCFS2_SYNC_FL) | |
86 | inode->i_flags |= S_SYNC; | |
87 | if (flags & OCFS2_APPEND_FL) | |
88 | inode->i_flags |= S_APPEND; | |
89 | if (flags & OCFS2_NOATIME_FL) | |
90 | inode->i_flags |= S_NOATIME; | |
91 | if (flags & OCFS2_DIRSYNC_FL) | |
92 | inode->i_flags |= S_DIRSYNC; | |
93 | } | |
94 | ||
6e4b0d56 JK |
95 | /* Propagate flags from i_flags to OCFS2_I(inode)->ip_attr */ |
96 | void ocfs2_get_inode_flags(struct ocfs2_inode_info *oi) | |
97 | { | |
98 | unsigned int flags = oi->vfs_inode.i_flags; | |
99 | ||
100 | oi->ip_attr &= ~(OCFS2_SYNC_FL|OCFS2_APPEND_FL| | |
101 | OCFS2_IMMUTABLE_FL|OCFS2_NOATIME_FL|OCFS2_DIRSYNC_FL); | |
102 | if (flags & S_SYNC) | |
103 | oi->ip_attr |= OCFS2_SYNC_FL; | |
104 | if (flags & S_APPEND) | |
105 | oi->ip_attr |= OCFS2_APPEND_FL; | |
106 | if (flags & S_IMMUTABLE) | |
107 | oi->ip_attr |= OCFS2_IMMUTABLE_FL; | |
108 | if (flags & S_NOATIME) | |
109 | oi->ip_attr |= OCFS2_NOATIME_FL; | |
110 | if (flags & S_DIRSYNC) | |
111 | oi->ip_attr |= OCFS2_DIRSYNC_FL; | |
112 | } | |
113 | ||
5fa0613e JK |
114 | struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags, |
115 | int sysfile_type) | |
ccd979bd MF |
116 | { |
117 | struct inode *inode = NULL; | |
118 | struct super_block *sb = osb->sb; | |
119 | struct ocfs2_find_inode_args args; | |
120 | ||
b0697053 | 121 | mlog_entry("(blkno = %llu)\n", (unsigned long long)blkno); |
ccd979bd MF |
122 | |
123 | /* Ok. By now we've either got the offsets passed to us by the | |
124 | * caller, or we just pulled them off the bh. Lets do some | |
125 | * sanity checks to make sure they're OK. */ | |
126 | if (blkno == 0) { | |
127 | inode = ERR_PTR(-EINVAL); | |
128 | mlog_errno(PTR_ERR(inode)); | |
129 | goto bail; | |
130 | } | |
131 | ||
132 | args.fi_blkno = blkno; | |
24c19ef4 | 133 | args.fi_flags = flags; |
ccd979bd | 134 | args.fi_ino = ino_from_blkno(sb, blkno); |
5fa0613e | 135 | args.fi_sysfile_type = sysfile_type; |
ccd979bd MF |
136 | |
137 | inode = iget5_locked(sb, args.fi_ino, ocfs2_find_actor, | |
138 | ocfs2_init_locked_inode, &args); | |
139 | /* inode was *not* in the inode cache. 2.6.x requires | |
140 | * us to do our own read_inode call and unlock it | |
141 | * afterwards. */ | |
142 | if (inode && inode->i_state & I_NEW) { | |
143 | mlog(0, "Inode was not in inode cache, reading it.\n"); | |
144 | ocfs2_read_locked_inode(inode, &args); | |
145 | unlock_new_inode(inode); | |
146 | } | |
147 | if (inode == NULL) { | |
148 | inode = ERR_PTR(-ENOMEM); | |
149 | mlog_errno(PTR_ERR(inode)); | |
150 | goto bail; | |
151 | } | |
152 | if (is_bad_inode(inode)) { | |
153 | iput(inode); | |
154 | inode = ERR_PTR(-ESTALE); | |
ccd979bd MF |
155 | goto bail; |
156 | } | |
157 | ||
158 | bail: | |
159 | if (!IS_ERR(inode)) { | |
b0697053 MF |
160 | mlog(0, "returning inode with number %llu\n", |
161 | (unsigned long long)OCFS2_I(inode)->ip_blkno); | |
ccd979bd | 162 | mlog_exit_ptr(inode); |
6a1bd4a5 | 163 | } |
ccd979bd MF |
164 | |
165 | return inode; | |
166 | } | |
167 | ||
168 | ||
169 | /* | |
170 | * here's how inodes get read from disk: | |
171 | * iget5_locked -> find_actor -> OCFS2_FIND_ACTOR | |
172 | * found? : return the in-memory inode | |
173 | * not found? : get_new_inode -> OCFS2_INIT_LOCKED_INODE | |
174 | */ | |
175 | ||
176 | static int ocfs2_find_actor(struct inode *inode, void *opaque) | |
177 | { | |
178 | struct ocfs2_find_inode_args *args = NULL; | |
179 | struct ocfs2_inode_info *oi = OCFS2_I(inode); | |
180 | int ret = 0; | |
181 | ||
182 | mlog_entry("(0x%p, %lu, 0x%p)\n", inode, inode->i_ino, opaque); | |
183 | ||
184 | args = opaque; | |
185 | ||
186 | mlog_bug_on_msg(!inode, "No inode in find actor!\n"); | |
187 | ||
188 | if (oi->ip_blkno != args->fi_blkno) | |
189 | goto bail; | |
190 | ||
ccd979bd MF |
191 | ret = 1; |
192 | bail: | |
193 | mlog_exit(ret); | |
194 | return ret; | |
195 | } | |
196 | ||
197 | /* | |
198 | * initialize the new inode, but don't do anything that would cause | |
199 | * us to sleep. | |
200 | * return 0 on success, 1 on failure | |
201 | */ | |
202 | static int ocfs2_init_locked_inode(struct inode *inode, void *opaque) | |
203 | { | |
204 | struct ocfs2_find_inode_args *args = opaque; | |
205 | ||
206 | mlog_entry("inode = %p, opaque = %p\n", inode, opaque); | |
207 | ||
208 | inode->i_ino = args->fi_ino; | |
209 | OCFS2_I(inode)->ip_blkno = args->fi_blkno; | |
5fa0613e JK |
210 | if (args->fi_sysfile_type != 0) |
211 | lockdep_set_class(&inode->i_mutex, | |
212 | &ocfs2_sysfile_lock_key[args->fi_sysfile_type]); | |
ccd979bd MF |
213 | |
214 | mlog_exit(0); | |
215 | return 0; | |
216 | } | |
217 | ||
b657c95c JB |
218 | void ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe, |
219 | int create_ino) | |
ccd979bd MF |
220 | { |
221 | struct super_block *sb; | |
222 | struct ocfs2_super *osb; | |
53da4939 | 223 | int use_plocks = 1; |
ccd979bd | 224 | |
b0697053 | 225 | mlog_entry("(0x%p, size:%llu)\n", inode, |
1ca1a111 | 226 | (unsigned long long)le64_to_cpu(fe->i_size)); |
ccd979bd MF |
227 | |
228 | sb = inode->i_sb; | |
229 | osb = OCFS2_SB(sb); | |
230 | ||
53da4939 MF |
231 | if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) || |
232 | ocfs2_mount_local(osb) || !ocfs2_stack_supports_plocks()) | |
233 | use_plocks = 0; | |
234 | ||
b657c95c JB |
235 | /* |
236 | * These have all been checked by ocfs2_read_inode_block() or set | |
237 | * by ocfs2_mknod_locked(), so a failure is a code bug. | |
238 | */ | |
239 | BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); /* This means that read_inode | |
240 | cannot create a superblock | |
241 | inode today. change if | |
242 | that is needed. */ | |
243 | BUG_ON(!(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL))); | |
244 | BUG_ON(le32_to_cpu(fe->i_fs_generation) != osb->fs_generation); | |
ccd979bd | 245 | |
ccd979bd | 246 | |
8110b073 MF |
247 | OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters); |
248 | OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr); | |
15b1e36b | 249 | OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features); |
8110b073 | 250 | |
ccd979bd MF |
251 | inode->i_version = 1; |
252 | inode->i_generation = le32_to_cpu(fe->i_generation); | |
253 | inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev)); | |
254 | inode->i_mode = le16_to_cpu(fe->i_mode); | |
255 | inode->i_uid = le32_to_cpu(fe->i_uid); | |
256 | inode->i_gid = le32_to_cpu(fe->i_gid); | |
ccd979bd MF |
257 | |
258 | /* Fast symlinks will have i_size but no allocated clusters. */ | |
259 | if (S_ISLNK(inode->i_mode) && !fe->i_clusters) | |
260 | inode->i_blocks = 0; | |
261 | else | |
8110b073 | 262 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
ccd979bd | 263 | inode->i_mapping->a_ops = &ocfs2_aops; |
ccd979bd MF |
264 | inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime); |
265 | inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec); | |
266 | inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime); | |
267 | inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec); | |
268 | inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime); | |
269 | inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec); | |
270 | ||
271 | if (OCFS2_I(inode)->ip_blkno != le64_to_cpu(fe->i_blkno)) | |
272 | mlog(ML_ERROR, | |
b0697053 MF |
273 | "ip_blkno %llu != i_blkno %llu!\n", |
274 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
1ca1a111 | 275 | (unsigned long long)le64_to_cpu(fe->i_blkno)); |
ccd979bd | 276 | |
ccd979bd MF |
277 | inode->i_nlink = le16_to_cpu(fe->i_links_count); |
278 | ||
bbbd0eb3 | 279 | if (fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) { |
24c19ef4 | 280 | OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SYSTEM_FILE; |
bbbd0eb3 JK |
281 | inode->i_flags |= S_NOQUOTA; |
282 | } | |
24c19ef4 | 283 | |
ccd979bd MF |
284 | if (fe->i_flags & cpu_to_le32(OCFS2_LOCAL_ALLOC_FL)) { |
285 | OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP; | |
286 | mlog(0, "local alloc inode: i_ino=%lu\n", inode->i_ino); | |
287 | } else if (fe->i_flags & cpu_to_le32(OCFS2_BITMAP_FL)) { | |
288 | OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP; | |
1a224ad1 JK |
289 | } else if (fe->i_flags & cpu_to_le32(OCFS2_QUOTA_FL)) { |
290 | inode->i_flags |= S_NOQUOTA; | |
ccd979bd MF |
291 | } else if (fe->i_flags & cpu_to_le32(OCFS2_SUPER_BLOCK_FL)) { |
292 | mlog(0, "superblock inode: i_ino=%lu\n", inode->i_ino); | |
293 | /* we can't actually hit this as read_inode can't | |
294 | * handle superblocks today ;-) */ | |
295 | BUG(); | |
296 | } | |
297 | ||
298 | switch (inode->i_mode & S_IFMT) { | |
299 | case S_IFREG: | |
53da4939 MF |
300 | if (use_plocks) |
301 | inode->i_fop = &ocfs2_fops; | |
302 | else | |
303 | inode->i_fop = &ocfs2_fops_no_plocks; | |
ccd979bd MF |
304 | inode->i_op = &ocfs2_file_iops; |
305 | i_size_write(inode, le64_to_cpu(fe->i_size)); | |
306 | break; | |
307 | case S_IFDIR: | |
308 | inode->i_op = &ocfs2_dir_iops; | |
53da4939 MF |
309 | if (use_plocks) |
310 | inode->i_fop = &ocfs2_dops; | |
311 | else | |
312 | inode->i_fop = &ocfs2_dops_no_plocks; | |
ccd979bd MF |
313 | i_size_write(inode, le64_to_cpu(fe->i_size)); |
314 | break; | |
315 | case S_IFLNK: | |
316 | if (ocfs2_inode_is_fast_symlink(inode)) | |
317 | inode->i_op = &ocfs2_fast_symlink_inode_operations; | |
318 | else | |
319 | inode->i_op = &ocfs2_symlink_inode_operations; | |
320 | i_size_write(inode, le64_to_cpu(fe->i_size)); | |
321 | break; | |
322 | default: | |
323 | inode->i_op = &ocfs2_special_file_iops; | |
324 | init_special_inode(inode, inode->i_mode, | |
325 | inode->i_rdev); | |
326 | break; | |
327 | } | |
328 | ||
24c19ef4 MF |
329 | if (create_ino) { |
330 | inode->i_ino = ino_from_blkno(inode->i_sb, | |
331 | le64_to_cpu(fe->i_blkno)); | |
332 | ||
333 | /* | |
334 | * If we ever want to create system files from kernel, | |
335 | * the generation argument to | |
336 | * ocfs2_inode_lock_res_init() will have to change. | |
337 | */ | |
1ca1a111 | 338 | BUG_ON(le32_to_cpu(fe->i_flags) & OCFS2_SYSTEM_FL); |
24c19ef4 | 339 | |
e63aecb6 | 340 | ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres, |
24c19ef4 | 341 | OCFS2_LOCK_TYPE_META, 0, inode); |
50008630 TY |
342 | |
343 | ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres, | |
344 | OCFS2_LOCK_TYPE_OPEN, 0, inode); | |
24c19ef4 MF |
345 | } |
346 | ||
ccd979bd | 347 | ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_rw_lockres, |
24c19ef4 MF |
348 | OCFS2_LOCK_TYPE_RW, inode->i_generation, |
349 | inode); | |
350 | ||
ca4d147e | 351 | ocfs2_set_inode_flags(inode); |
ca4d147e | 352 | |
b657c95c | 353 | mlog_exit_void(); |
ccd979bd MF |
354 | } |
355 | ||
356 | static int ocfs2_read_locked_inode(struct inode *inode, | |
357 | struct ocfs2_find_inode_args *args) | |
358 | { | |
359 | struct super_block *sb; | |
360 | struct ocfs2_super *osb; | |
361 | struct ocfs2_dinode *fe; | |
362 | struct buffer_head *bh = NULL; | |
24c19ef4 MF |
363 | int status, can_lock; |
364 | u32 generation = 0; | |
ccd979bd MF |
365 | |
366 | mlog_entry("(0x%p, 0x%p)\n", inode, args); | |
367 | ||
368 | status = -EINVAL; | |
369 | if (inode == NULL || inode->i_sb == NULL) { | |
370 | mlog(ML_ERROR, "bad inode\n"); | |
24c19ef4 | 371 | return status; |
ccd979bd MF |
372 | } |
373 | sb = inode->i_sb; | |
374 | osb = OCFS2_SB(sb); | |
375 | ||
376 | if (!args) { | |
377 | mlog(ML_ERROR, "bad inode args\n"); | |
378 | make_bad_inode(inode); | |
24c19ef4 MF |
379 | return status; |
380 | } | |
381 | ||
382 | /* | |
383 | * To improve performance of cold-cache inode stats, we take | |
384 | * the cluster lock here if possible. | |
385 | * | |
386 | * Generally, OCFS2 never trusts the contents of an inode | |
387 | * unless it's holding a cluster lock, so taking it here isn't | |
388 | * a correctness issue as much as it is a performance | |
389 | * improvement. | |
390 | * | |
391 | * There are three times when taking the lock is not a good idea: | |
392 | * | |
393 | * 1) During startup, before we have initialized the DLM. | |
394 | * | |
395 | * 2) If we are reading certain system files which never get | |
396 | * cluster locks (local alloc, truncate log). | |
397 | * | |
398 | * 3) If the process doing the iget() is responsible for | |
399 | * orphan dir recovery. We're holding the orphan dir lock and | |
400 | * can get into a deadlock with another process on another | |
401 | * node in ->delete_inode(). | |
402 | * | |
403 | * #1 and #2 can be simply solved by never taking the lock | |
404 | * here for system files (which are the only type we read | |
405 | * during mount). It's a heavier approach, but our main | |
406 | * concern is user-accesible files anyway. | |
407 | * | |
408 | * #3 works itself out because we'll eventually take the | |
409 | * cluster lock before trusting anything anyway. | |
410 | */ | |
411 | can_lock = !(args->fi_flags & OCFS2_FI_FLAG_SYSFILE) | |
50008630 | 412 | && !(args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY) |
c271c5c2 | 413 | && !ocfs2_mount_local(osb); |
24c19ef4 MF |
414 | |
415 | /* | |
416 | * To maintain backwards compatibility with older versions of | |
417 | * ocfs2-tools, we still store the generation value for system | |
418 | * files. The only ones that actually matter to userspace are | |
419 | * the journals, but it's easier and inexpensive to just flag | |
420 | * all system files similarly. | |
421 | */ | |
422 | if (args->fi_flags & OCFS2_FI_FLAG_SYSFILE) | |
423 | generation = osb->fs_generation; | |
424 | ||
e63aecb6 | 425 | ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres, |
24c19ef4 MF |
426 | OCFS2_LOCK_TYPE_META, |
427 | generation, inode); | |
428 | ||
50008630 TY |
429 | ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres, |
430 | OCFS2_LOCK_TYPE_OPEN, | |
431 | 0, inode); | |
432 | ||
24c19ef4 | 433 | if (can_lock) { |
50008630 TY |
434 | status = ocfs2_open_lock(inode); |
435 | if (status) { | |
436 | make_bad_inode(inode); | |
437 | mlog_errno(status); | |
438 | return status; | |
439 | } | |
e63aecb6 | 440 | status = ocfs2_inode_lock(inode, NULL, 0); |
24c19ef4 MF |
441 | if (status) { |
442 | make_bad_inode(inode); | |
443 | mlog_errno(status); | |
444 | return status; | |
445 | } | |
ccd979bd MF |
446 | } |
447 | ||
50008630 TY |
448 | if (args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY) { |
449 | status = ocfs2_try_open_lock(inode, 0); | |
450 | if (status) { | |
451 | make_bad_inode(inode); | |
452 | return status; | |
453 | } | |
454 | } | |
455 | ||
b657c95c JB |
456 | if (can_lock) { |
457 | status = ocfs2_read_inode_block_full(inode, &bh, | |
458 | OCFS2_BH_IGNORE_CACHE); | |
459 | } else { | |
da1e9098 | 460 | status = ocfs2_read_blocks_sync(osb, args->fi_blkno, 1, &bh); |
b657c95c JB |
461 | if (!status) |
462 | status = ocfs2_validate_inode_block(osb->sb, bh); | |
463 | } | |
ccd979bd MF |
464 | if (status < 0) { |
465 | mlog_errno(status); | |
ccd979bd MF |
466 | goto bail; |
467 | } | |
468 | ||
24c19ef4 | 469 | status = -EINVAL; |
ccd979bd | 470 | fe = (struct ocfs2_dinode *) bh->b_data; |
ccd979bd | 471 | |
24c19ef4 MF |
472 | /* |
473 | * This is a code bug. Right now the caller needs to | |
474 | * understand whether it is asking for a system file inode or | |
475 | * not so the proper lock names can be built. | |
476 | */ | |
477 | mlog_bug_on_msg(!!(fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) != | |
478 | !!(args->fi_flags & OCFS2_FI_FLAG_SYSFILE), | |
479 | "Inode %llu: system file state is ambigous\n", | |
480 | (unsigned long long)args->fi_blkno); | |
ccd979bd MF |
481 | |
482 | if (S_ISCHR(le16_to_cpu(fe->i_mode)) || | |
483 | S_ISBLK(le16_to_cpu(fe->i_mode))) | |
b657c95c | 484 | inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev)); |
ccd979bd | 485 | |
b657c95c | 486 | ocfs2_populate_inode(inode, fe, 0); |
ccd979bd MF |
487 | |
488 | BUG_ON(args->fi_blkno != le64_to_cpu(fe->i_blkno)); | |
489 | ||
ccd979bd MF |
490 | status = 0; |
491 | ||
492 | bail: | |
24c19ef4 | 493 | if (can_lock) |
e63aecb6 | 494 | ocfs2_inode_unlock(inode, 0); |
24c19ef4 MF |
495 | |
496 | if (status < 0) | |
497 | make_bad_inode(inode); | |
498 | ||
ccd979bd MF |
499 | if (args && bh) |
500 | brelse(bh); | |
501 | ||
502 | mlog_exit(status); | |
503 | return status; | |
504 | } | |
505 | ||
506 | void ocfs2_sync_blockdev(struct super_block *sb) | |
507 | { | |
508 | sync_blockdev(sb->s_bdev); | |
509 | } | |
510 | ||
511 | static int ocfs2_truncate_for_delete(struct ocfs2_super *osb, | |
512 | struct inode *inode, | |
513 | struct buffer_head *fe_bh) | |
514 | { | |
515 | int status = 0; | |
ccd979bd MF |
516 | struct ocfs2_truncate_context *tc = NULL; |
517 | struct ocfs2_dinode *fe; | |
60b11392 | 518 | handle_t *handle = NULL; |
ccd979bd MF |
519 | |
520 | mlog_entry_void(); | |
521 | ||
522 | fe = (struct ocfs2_dinode *) fe_bh->b_data; | |
523 | ||
1afc32b9 MF |
524 | /* |
525 | * This check will also skip truncate of inodes with inline | |
526 | * data and fast symlinks. | |
527 | */ | |
3a0782d0 | 528 | if (fe->i_clusters) { |
2b4e30fb JB |
529 | if (ocfs2_should_order_data(inode)) |
530 | ocfs2_begin_ordered_truncate(inode, 0); | |
531 | ||
60b11392 MF |
532 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
533 | if (IS_ERR(handle)) { | |
534 | status = PTR_ERR(handle); | |
535 | mlog_errno(status); | |
536 | goto out; | |
537 | } | |
538 | ||
539 | status = ocfs2_journal_access(handle, inode, fe_bh, | |
540 | OCFS2_JOURNAL_ACCESS_WRITE); | |
541 | if (status < 0) { | |
542 | mlog_errno(status); | |
543 | goto out; | |
544 | } | |
545 | ||
546 | i_size_write(inode, 0); | |
547 | ||
548 | status = ocfs2_mark_inode_dirty(handle, inode, fe_bh); | |
549 | if (status < 0) { | |
550 | mlog_errno(status); | |
551 | goto out; | |
552 | } | |
553 | ||
554 | ocfs2_commit_trans(osb, handle); | |
555 | handle = NULL; | |
556 | ||
3a0782d0 MF |
557 | status = ocfs2_prepare_truncate(osb, inode, fe_bh, &tc); |
558 | if (status < 0) { | |
559 | mlog_errno(status); | |
560 | goto out; | |
561 | } | |
ccd979bd | 562 | |
3a0782d0 MF |
563 | status = ocfs2_commit_truncate(osb, inode, fe_bh, tc); |
564 | if (status < 0) { | |
565 | mlog_errno(status); | |
566 | goto out; | |
567 | } | |
ccd979bd | 568 | } |
ccd979bd | 569 | |
60b11392 MF |
570 | out: |
571 | if (handle) | |
572 | ocfs2_commit_trans(osb, handle); | |
ccd979bd MF |
573 | mlog_exit(status); |
574 | return status; | |
575 | } | |
576 | ||
577 | static int ocfs2_remove_inode(struct inode *inode, | |
578 | struct buffer_head *di_bh, | |
579 | struct inode *orphan_dir_inode, | |
580 | struct buffer_head *orphan_dir_bh) | |
581 | { | |
582 | int status; | |
583 | struct inode *inode_alloc_inode = NULL; | |
584 | struct buffer_head *inode_alloc_bh = NULL; | |
1fabe148 | 585 | handle_t *handle; |
ccd979bd MF |
586 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
587 | struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data; | |
588 | ||
589 | inode_alloc_inode = | |
590 | ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE, | |
591 | le16_to_cpu(di->i_suballoc_slot)); | |
592 | if (!inode_alloc_inode) { | |
593 | status = -EEXIST; | |
594 | mlog_errno(status); | |
595 | goto bail; | |
596 | } | |
597 | ||
1b1dcc1b | 598 | mutex_lock(&inode_alloc_inode->i_mutex); |
e63aecb6 | 599 | status = ocfs2_inode_lock(inode_alloc_inode, &inode_alloc_bh, 1); |
ccd979bd | 600 | if (status < 0) { |
1b1dcc1b | 601 | mutex_unlock(&inode_alloc_inode->i_mutex); |
ccd979bd MF |
602 | |
603 | mlog_errno(status); | |
604 | goto bail; | |
605 | } | |
606 | ||
a90714c1 JK |
607 | handle = ocfs2_start_trans(osb, OCFS2_DELETE_INODE_CREDITS + |
608 | ocfs2_quota_trans_credits(inode->i_sb)); | |
ccd979bd MF |
609 | if (IS_ERR(handle)) { |
610 | status = PTR_ERR(handle); | |
611 | mlog_errno(status); | |
612 | goto bail_unlock; | |
613 | } | |
614 | ||
615 | status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode, | |
616 | orphan_dir_bh); | |
617 | if (status < 0) { | |
618 | mlog_errno(status); | |
619 | goto bail_commit; | |
620 | } | |
621 | ||
622 | /* set the inodes dtime */ | |
623 | status = ocfs2_journal_access(handle, inode, di_bh, | |
624 | OCFS2_JOURNAL_ACCESS_WRITE); | |
625 | if (status < 0) { | |
626 | mlog_errno(status); | |
627 | goto bail_commit; | |
628 | } | |
629 | ||
630 | di->i_dtime = cpu_to_le64(CURRENT_TIME.tv_sec); | |
4092d49f | 631 | di->i_flags &= cpu_to_le32(~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL)); |
ccd979bd MF |
632 | |
633 | status = ocfs2_journal_dirty(handle, di_bh); | |
634 | if (status < 0) { | |
635 | mlog_errno(status); | |
636 | goto bail_commit; | |
637 | } | |
638 | ||
639 | ocfs2_remove_from_cache(inode, di_bh); | |
a90714c1 | 640 | vfs_dq_free_inode(inode); |
ccd979bd MF |
641 | |
642 | status = ocfs2_free_dinode(handle, inode_alloc_inode, | |
643 | inode_alloc_bh, di); | |
644 | if (status < 0) | |
645 | mlog_errno(status); | |
646 | ||
647 | bail_commit: | |
02dc1af4 | 648 | ocfs2_commit_trans(osb, handle); |
ccd979bd | 649 | bail_unlock: |
e63aecb6 | 650 | ocfs2_inode_unlock(inode_alloc_inode, 1); |
1b1dcc1b | 651 | mutex_unlock(&inode_alloc_inode->i_mutex); |
ccd979bd MF |
652 | brelse(inode_alloc_bh); |
653 | bail: | |
654 | iput(inode_alloc_inode); | |
655 | ||
656 | return status; | |
657 | } | |
658 | ||
b4df6ed8 MF |
659 | /* |
660 | * Serialize with orphan dir recovery. If the process doing | |
661 | * recovery on this orphan dir does an iget() with the dir | |
662 | * i_mutex held, we'll deadlock here. Instead we detect this | |
663 | * and exit early - recovery will wipe this inode for us. | |
664 | */ | |
665 | static int ocfs2_check_orphan_recovery_state(struct ocfs2_super *osb, | |
666 | int slot) | |
667 | { | |
668 | int ret = 0; | |
669 | ||
670 | spin_lock(&osb->osb_lock); | |
671 | if (ocfs2_node_map_test_bit(osb, &osb->osb_recovering_orphan_dirs, slot)) { | |
672 | mlog(0, "Recovery is happening on orphan dir %d, will skip " | |
673 | "this inode\n", slot); | |
674 | ret = -EDEADLK; | |
675 | goto out; | |
676 | } | |
677 | /* This signals to the orphan recovery process that it should | |
678 | * wait for us to handle the wipe. */ | |
679 | osb->osb_orphan_wipes[slot]++; | |
680 | out: | |
681 | spin_unlock(&osb->osb_lock); | |
682 | return ret; | |
683 | } | |
684 | ||
685 | static void ocfs2_signal_wipe_completion(struct ocfs2_super *osb, | |
686 | int slot) | |
687 | { | |
688 | spin_lock(&osb->osb_lock); | |
689 | osb->osb_orphan_wipes[slot]--; | |
690 | spin_unlock(&osb->osb_lock); | |
691 | ||
692 | wake_up(&osb->osb_wipe_event); | |
693 | } | |
694 | ||
ccd979bd MF |
695 | static int ocfs2_wipe_inode(struct inode *inode, |
696 | struct buffer_head *di_bh) | |
697 | { | |
698 | int status, orphaned_slot; | |
699 | struct inode *orphan_dir_inode = NULL; | |
700 | struct buffer_head *orphan_dir_bh = NULL; | |
701 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
50008630 | 702 | struct ocfs2_dinode *di; |
ccd979bd | 703 | |
50008630 TY |
704 | di = (struct ocfs2_dinode *) di_bh->b_data; |
705 | orphaned_slot = le16_to_cpu(di->i_orphaned_slot); | |
b4df6ed8 MF |
706 | |
707 | status = ocfs2_check_orphan_recovery_state(osb, orphaned_slot); | |
708 | if (status) | |
709 | return status; | |
710 | ||
ccd979bd MF |
711 | orphan_dir_inode = ocfs2_get_system_file_inode(osb, |
712 | ORPHAN_DIR_SYSTEM_INODE, | |
713 | orphaned_slot); | |
714 | if (!orphan_dir_inode) { | |
715 | status = -EEXIST; | |
716 | mlog_errno(status); | |
717 | goto bail; | |
718 | } | |
719 | ||
720 | /* Lock the orphan dir. The lock will be held for the entire | |
721 | * delete_inode operation. We do this now to avoid races with | |
722 | * recovery completion on other nodes. */ | |
1b1dcc1b | 723 | mutex_lock(&orphan_dir_inode->i_mutex); |
e63aecb6 | 724 | status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1); |
ccd979bd | 725 | if (status < 0) { |
1b1dcc1b | 726 | mutex_unlock(&orphan_dir_inode->i_mutex); |
ccd979bd MF |
727 | |
728 | mlog_errno(status); | |
729 | goto bail; | |
730 | } | |
731 | ||
732 | /* we do this while holding the orphan dir lock because we | |
34d024f8 MF |
733 | * don't want recovery being run from another node to try an |
734 | * inode delete underneath us -- this will result in two nodes | |
ccd979bd MF |
735 | * truncating the same file! */ |
736 | status = ocfs2_truncate_for_delete(osb, inode, di_bh); | |
737 | if (status < 0) { | |
738 | mlog_errno(status); | |
739 | goto bail_unlock_dir; | |
740 | } | |
741 | ||
cf1d6c76 TY |
742 | /*Free extended attribute resources associated with this inode.*/ |
743 | status = ocfs2_xattr_remove(inode, di_bh); | |
744 | if (status < 0) { | |
745 | mlog_errno(status); | |
746 | goto bail_unlock_dir; | |
747 | } | |
748 | ||
ccd979bd MF |
749 | status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode, |
750 | orphan_dir_bh); | |
751 | if (status < 0) | |
752 | mlog_errno(status); | |
753 | ||
754 | bail_unlock_dir: | |
e63aecb6 | 755 | ocfs2_inode_unlock(orphan_dir_inode, 1); |
1b1dcc1b | 756 | mutex_unlock(&orphan_dir_inode->i_mutex); |
ccd979bd MF |
757 | brelse(orphan_dir_bh); |
758 | bail: | |
759 | iput(orphan_dir_inode); | |
b4df6ed8 | 760 | ocfs2_signal_wipe_completion(osb, orphaned_slot); |
ccd979bd MF |
761 | |
762 | return status; | |
763 | } | |
764 | ||
765 | /* There is a series of simple checks that should be done before a | |
34d024f8 | 766 | * trylock is even considered. Encapsulate those in this function. */ |
ccd979bd MF |
767 | static int ocfs2_inode_is_valid_to_delete(struct inode *inode) |
768 | { | |
769 | int ret = 0; | |
770 | struct ocfs2_inode_info *oi = OCFS2_I(inode); | |
771 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
772 | ||
773 | /* We shouldn't be getting here for the root directory | |
774 | * inode.. */ | |
775 | if (inode == osb->root_inode) { | |
776 | mlog(ML_ERROR, "Skipping delete of root inode.\n"); | |
777 | goto bail; | |
778 | } | |
779 | ||
34d024f8 | 780 | /* If we're coming from downconvert_thread we can't go into our own |
ccd979bd MF |
781 | * voting [hello, deadlock city!], so unforuntately we just |
782 | * have to skip deleting this guy. That's OK though because | |
783 | * the node who's doing the actual deleting should handle it | |
784 | * anyway. */ | |
34d024f8 | 785 | if (current == osb->dc_task) { |
ccd979bd | 786 | mlog(0, "Skipping delete of %lu because we're currently " |
34d024f8 | 787 | "in downconvert\n", inode->i_ino); |
ccd979bd MF |
788 | goto bail; |
789 | } | |
790 | ||
791 | spin_lock(&oi->ip_lock); | |
792 | /* OCFS2 *never* deletes system files. This should technically | |
793 | * never get here as system file inodes should always have a | |
794 | * positive link count. */ | |
795 | if (oi->ip_flags & OCFS2_INODE_SYSTEM_FILE) { | |
b0697053 MF |
796 | mlog(ML_ERROR, "Skipping delete of system file %llu\n", |
797 | (unsigned long long)oi->ip_blkno); | |
ccd979bd MF |
798 | goto bail_unlock; |
799 | } | |
800 | ||
34d024f8 MF |
801 | /* If we have allowd wipe of this inode for another node, it |
802 | * will be marked here so we can safely skip it. Recovery will | |
803 | * cleanup any inodes we might inadvertantly skip here. */ | |
ccd979bd MF |
804 | if (oi->ip_flags & OCFS2_INODE_SKIP_DELETE) { |
805 | mlog(0, "Skipping delete of %lu because another node " | |
806 | "has done this for us.\n", inode->i_ino); | |
807 | goto bail_unlock; | |
808 | } | |
809 | ||
810 | ret = 1; | |
811 | bail_unlock: | |
812 | spin_unlock(&oi->ip_lock); | |
813 | bail: | |
814 | return ret; | |
815 | } | |
816 | ||
817 | /* Query the cluster to determine whether we should wipe an inode from | |
818 | * disk or not. | |
819 | * | |
820 | * Requires the inode to have the cluster lock. */ | |
821 | static int ocfs2_query_inode_wipe(struct inode *inode, | |
822 | struct buffer_head *di_bh, | |
823 | int *wipe) | |
824 | { | |
825 | int status = 0; | |
826 | struct ocfs2_inode_info *oi = OCFS2_I(inode); | |
827 | struct ocfs2_dinode *di; | |
828 | ||
829 | *wipe = 0; | |
830 | ||
831 | /* While we were waiting for the cluster lock in | |
832 | * ocfs2_delete_inode, another node might have asked to delete | |
833 | * the inode. Recheck our flags to catch this. */ | |
834 | if (!ocfs2_inode_is_valid_to_delete(inode)) { | |
b0697053 MF |
835 | mlog(0, "Skipping delete of %llu because flags changed\n", |
836 | (unsigned long long)oi->ip_blkno); | |
ccd979bd MF |
837 | goto bail; |
838 | } | |
839 | ||
840 | /* Now that we have an up to date inode, we can double check | |
841 | * the link count. */ | |
842 | if (inode->i_nlink) { | |
b0697053 MF |
843 | mlog(0, "Skipping delete of %llu because nlink = %u\n", |
844 | (unsigned long long)oi->ip_blkno, inode->i_nlink); | |
ccd979bd MF |
845 | goto bail; |
846 | } | |
847 | ||
848 | /* Do some basic inode verification... */ | |
849 | di = (struct ocfs2_dinode *) di_bh->b_data; | |
850 | if (!(di->i_flags & cpu_to_le32(OCFS2_ORPHANED_FL))) { | |
851 | /* for lack of a better error? */ | |
852 | status = -EEXIST; | |
853 | mlog(ML_ERROR, | |
b0697053 | 854 | "Inode %llu (on-disk %llu) not orphaned! " |
ccd979bd | 855 | "Disk flags 0x%x, inode flags 0x%x\n", |
b0697053 | 856 | (unsigned long long)oi->ip_blkno, |
1ca1a111 MF |
857 | (unsigned long long)le64_to_cpu(di->i_blkno), |
858 | le32_to_cpu(di->i_flags), oi->ip_flags); | |
ccd979bd MF |
859 | goto bail; |
860 | } | |
861 | ||
862 | /* has someone already deleted us?! baaad... */ | |
863 | if (di->i_dtime) { | |
864 | status = -EEXIST; | |
865 | mlog_errno(status); | |
866 | goto bail; | |
867 | } | |
868 | ||
6f16bf65 MF |
869 | /* |
870 | * This is how ocfs2 determines whether an inode is still live | |
871 | * within the cluster. Every node takes a shared read lock on | |
872 | * the inode open lock in ocfs2_read_locked_inode(). When we | |
873 | * get to ->delete_inode(), each node tries to convert it's | |
874 | * lock to an exclusive. Trylocks are serialized by the inode | |
875 | * meta data lock. If the upconvert suceeds, we know the inode | |
876 | * is no longer live and can be deleted. | |
877 | * | |
878 | * Though we call this with the meta data lock held, the | |
879 | * trylock keeps us from ABBA deadlock. | |
880 | */ | |
881 | status = ocfs2_try_open_lock(inode, 1); | |
50008630 | 882 | if (status == -EAGAIN) { |
ccd979bd | 883 | status = 0; |
2759236f | 884 | mlog(0, "Skipping delete of %llu because it is in use on " |
b0697053 | 885 | "other nodes\n", (unsigned long long)oi->ip_blkno); |
ccd979bd MF |
886 | goto bail; |
887 | } | |
888 | if (status < 0) { | |
889 | mlog_errno(status); | |
890 | goto bail; | |
891 | } | |
892 | ||
50008630 TY |
893 | *wipe = 1; |
894 | mlog(0, "Inode %llu is ok to wipe from orphan dir %u\n", | |
895 | (unsigned long long)oi->ip_blkno, | |
896 | le16_to_cpu(di->i_orphaned_slot)); | |
ccd979bd MF |
897 | |
898 | bail: | |
899 | return status; | |
900 | } | |
901 | ||
902 | /* Support function for ocfs2_delete_inode. Will help us keep the | |
903 | * inode data in a consistent state for clear_inode. Always truncates | |
904 | * pages, optionally sync's them first. */ | |
905 | static void ocfs2_cleanup_delete_inode(struct inode *inode, | |
906 | int sync_data) | |
907 | { | |
b0697053 MF |
908 | mlog(0, "Cleanup inode %llu, sync = %d\n", |
909 | (unsigned long long)OCFS2_I(inode)->ip_blkno, sync_data); | |
ccd979bd MF |
910 | if (sync_data) |
911 | write_inode_now(inode, 1); | |
912 | truncate_inode_pages(&inode->i_data, 0); | |
913 | } | |
914 | ||
915 | void ocfs2_delete_inode(struct inode *inode) | |
916 | { | |
917 | int wipe, status; | |
918 | sigset_t blocked, oldset; | |
919 | struct buffer_head *di_bh = NULL; | |
920 | ||
921 | mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino); | |
922 | ||
a90714c1 JK |
923 | /* When we fail in read_inode() we mark inode as bad. The second test |
924 | * catches the case when inode allocation fails before allocating | |
925 | * a block for inode. */ | |
926 | if (is_bad_inode(inode) || !OCFS2_I(inode)->ip_blkno) { | |
ccd979bd MF |
927 | mlog(0, "Skipping delete of bad inode\n"); |
928 | goto bail; | |
929 | } | |
930 | ||
931 | if (!ocfs2_inode_is_valid_to_delete(inode)) { | |
932 | /* It's probably not necessary to truncate_inode_pages | |
933 | * here but we do it for safety anyway (it will most | |
934 | * likely be a no-op anyway) */ | |
935 | ocfs2_cleanup_delete_inode(inode, 0); | |
936 | goto bail; | |
937 | } | |
938 | ||
939 | /* We want to block signals in delete_inode as the lock and | |
940 | * messaging paths may return us -ERESTARTSYS. Which would | |
941 | * cause us to exit early, resulting in inodes being orphaned | |
942 | * forever. */ | |
943 | sigfillset(&blocked); | |
944 | status = sigprocmask(SIG_BLOCK, &blocked, &oldset); | |
945 | if (status < 0) { | |
946 | mlog_errno(status); | |
947 | ocfs2_cleanup_delete_inode(inode, 1); | |
948 | goto bail; | |
949 | } | |
950 | ||
951 | /* Lock down the inode. This gives us an up to date view of | |
952 | * it's metadata (for verification), and allows us to | |
34d024f8 | 953 | * serialize delete_inode on multiple nodes. |
ccd979bd MF |
954 | * |
955 | * Even though we might be doing a truncate, we don't take the | |
956 | * allocation lock here as it won't be needed - nobody will | |
957 | * have the file open. | |
958 | */ | |
e63aecb6 | 959 | status = ocfs2_inode_lock(inode, &di_bh, 1); |
ccd979bd MF |
960 | if (status < 0) { |
961 | if (status != -ENOENT) | |
962 | mlog_errno(status); | |
963 | ocfs2_cleanup_delete_inode(inode, 0); | |
964 | goto bail_unblock; | |
965 | } | |
966 | ||
967 | /* Query the cluster. This will be the final decision made | |
968 | * before we go ahead and wipe the inode. */ | |
969 | status = ocfs2_query_inode_wipe(inode, di_bh, &wipe); | |
970 | if (!wipe || status < 0) { | |
34d024f8 | 971 | /* Error and remote inode busy both mean we won't be |
ccd979bd MF |
972 | * removing the inode, so they take almost the same |
973 | * path. */ | |
974 | if (status < 0) | |
975 | mlog_errno(status); | |
976 | ||
34d024f8 MF |
977 | /* Someone in the cluster has disallowed a wipe of |
978 | * this inode, or it was never completely | |
979 | * orphaned. Write out the pages and exit now. */ | |
ccd979bd MF |
980 | ocfs2_cleanup_delete_inode(inode, 1); |
981 | goto bail_unlock_inode; | |
982 | } | |
983 | ||
984 | ocfs2_cleanup_delete_inode(inode, 0); | |
985 | ||
986 | status = ocfs2_wipe_inode(inode, di_bh); | |
987 | if (status < 0) { | |
b4df6ed8 MF |
988 | if (status != -EDEADLK) |
989 | mlog_errno(status); | |
ccd979bd MF |
990 | goto bail_unlock_inode; |
991 | } | |
992 | ||
24c19ef4 MF |
993 | /* |
994 | * Mark the inode as successfully deleted. | |
995 | * | |
996 | * This is important for ocfs2_clear_inode() as it will check | |
997 | * this flag and skip any checkpointing work | |
998 | * | |
999 | * ocfs2_stuff_meta_lvb() also uses this flag to invalidate | |
1000 | * the LVB for other nodes. | |
1001 | */ | |
ccd979bd MF |
1002 | OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED; |
1003 | ||
1004 | bail_unlock_inode: | |
e63aecb6 | 1005 | ocfs2_inode_unlock(inode, 1); |
ccd979bd MF |
1006 | brelse(di_bh); |
1007 | bail_unblock: | |
1008 | status = sigprocmask(SIG_SETMASK, &oldset, NULL); | |
1009 | if (status < 0) | |
1010 | mlog_errno(status); | |
1011 | bail: | |
1012 | clear_inode(inode); | |
1013 | mlog_exit_void(); | |
1014 | } | |
1015 | ||
1016 | void ocfs2_clear_inode(struct inode *inode) | |
1017 | { | |
1018 | int status; | |
1019 | struct ocfs2_inode_info *oi = OCFS2_I(inode); | |
1020 | ||
1021 | mlog_entry_void(); | |
1022 | ||
1023 | if (!inode) | |
1024 | goto bail; | |
1025 | ||
b0697053 MF |
1026 | mlog(0, "Clearing inode: %llu, nlink = %u\n", |
1027 | (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_nlink); | |
ccd979bd MF |
1028 | |
1029 | mlog_bug_on_msg(OCFS2_SB(inode->i_sb) == NULL, | |
1030 | "Inode=%lu\n", inode->i_ino); | |
1031 | ||
34d024f8 MF |
1032 | /* To preven remote deletes we hold open lock before, now it |
1033 | * is time to unlock PR and EX open locks. */ | |
50008630 TY |
1034 | ocfs2_open_unlock(inode); |
1035 | ||
ccd979bd | 1036 | /* Do these before all the other work so that we don't bounce |
34d024f8 | 1037 | * the downconvert thread while waiting to destroy the locks. */ |
ccd979bd | 1038 | ocfs2_mark_lockres_freeing(&oi->ip_rw_lockres); |
e63aecb6 | 1039 | ocfs2_mark_lockres_freeing(&oi->ip_inode_lockres); |
50008630 | 1040 | ocfs2_mark_lockres_freeing(&oi->ip_open_lockres); |
ccd979bd MF |
1041 | |
1042 | /* We very well may get a clear_inode before all an inodes | |
1043 | * metadata has hit disk. Of course, we can't drop any cluster | |
1044 | * locks until the journal has finished with it. The only | |
1045 | * exception here are successfully wiped inodes - their | |
1046 | * metadata can now be considered to be part of the system | |
1047 | * inodes from which it came. */ | |
1048 | if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED)) | |
1049 | ocfs2_checkpoint_inode(inode); | |
1050 | ||
1051 | mlog_bug_on_msg(!list_empty(&oi->ip_io_markers), | |
b0697053 MF |
1052 | "Clear inode of %llu, inode has io markers\n", |
1053 | (unsigned long long)oi->ip_blkno); | |
ccd979bd | 1054 | |
83418978 MF |
1055 | ocfs2_extent_map_trunc(inode, 0); |
1056 | ||
ccd979bd MF |
1057 | status = ocfs2_drop_inode_locks(inode); |
1058 | if (status < 0) | |
1059 | mlog_errno(status); | |
1060 | ||
1061 | ocfs2_lock_res_free(&oi->ip_rw_lockres); | |
e63aecb6 | 1062 | ocfs2_lock_res_free(&oi->ip_inode_lockres); |
50008630 | 1063 | ocfs2_lock_res_free(&oi->ip_open_lockres); |
ccd979bd MF |
1064 | |
1065 | ocfs2_metadata_cache_purge(inode); | |
1066 | ||
1067 | mlog_bug_on_msg(oi->ip_metadata_cache.ci_num_cached, | |
b0697053 MF |
1068 | "Clear inode of %llu, inode has %u cache items\n", |
1069 | (unsigned long long)oi->ip_blkno, oi->ip_metadata_cache.ci_num_cached); | |
ccd979bd MF |
1070 | |
1071 | mlog_bug_on_msg(!(oi->ip_flags & OCFS2_INODE_CACHE_INLINE), | |
b0697053 MF |
1072 | "Clear inode of %llu, inode has a bad flag\n", |
1073 | (unsigned long long)oi->ip_blkno); | |
ccd979bd MF |
1074 | |
1075 | mlog_bug_on_msg(spin_is_locked(&oi->ip_lock), | |
b0697053 MF |
1076 | "Clear inode of %llu, inode is locked\n", |
1077 | (unsigned long long)oi->ip_blkno); | |
ccd979bd | 1078 | |
251b6ecc | 1079 | mlog_bug_on_msg(!mutex_trylock(&oi->ip_io_mutex), |
b0697053 MF |
1080 | "Clear inode of %llu, io_mutex is locked\n", |
1081 | (unsigned long long)oi->ip_blkno); | |
251b6ecc | 1082 | mutex_unlock(&oi->ip_io_mutex); |
ccd979bd MF |
1083 | |
1084 | /* | |
1085 | * down_trylock() returns 0, down_write_trylock() returns 1 | |
1086 | * kernel 1, world 0 | |
1087 | */ | |
1088 | mlog_bug_on_msg(!down_write_trylock(&oi->ip_alloc_sem), | |
b0697053 MF |
1089 | "Clear inode of %llu, alloc_sem is locked\n", |
1090 | (unsigned long long)oi->ip_blkno); | |
ccd979bd MF |
1091 | up_write(&oi->ip_alloc_sem); |
1092 | ||
1093 | mlog_bug_on_msg(oi->ip_open_count, | |
b0697053 MF |
1094 | "Clear inode of %llu has open count %d\n", |
1095 | (unsigned long long)oi->ip_blkno, oi->ip_open_count); | |
ccd979bd MF |
1096 | |
1097 | /* Clear all other flags. */ | |
1098 | oi->ip_flags = OCFS2_INODE_CACHE_INLINE; | |
1099 | oi->ip_created_trans = 0; | |
1100 | oi->ip_last_trans = 0; | |
1101 | oi->ip_dir_start_lookup = 0; | |
1102 | oi->ip_blkno = 0ULL; | |
ae0dff68 SM |
1103 | |
1104 | /* | |
1105 | * ip_jinode is used to track txns against this inode. We ensure that | |
1106 | * the journal is flushed before journal shutdown. Thus it is safe to | |
1107 | * have inodes get cleaned up after journal shutdown. | |
1108 | */ | |
2b4e30fb JB |
1109 | jbd2_journal_release_jbd_inode(OCFS2_SB(inode->i_sb)->journal->j_journal, |
1110 | &oi->ip_jinode); | |
ccd979bd MF |
1111 | |
1112 | bail: | |
1113 | mlog_exit_void(); | |
1114 | } | |
1115 | ||
1116 | /* Called under inode_lock, with no more references on the | |
1117 | * struct inode, so it's safe here to check the flags field | |
1118 | * and to manipulate i_nlink without any other locks. */ | |
1119 | void ocfs2_drop_inode(struct inode *inode) | |
1120 | { | |
1121 | struct ocfs2_inode_info *oi = OCFS2_I(inode); | |
1122 | ||
1123 | mlog_entry_void(); | |
1124 | ||
b0697053 MF |
1125 | mlog(0, "Drop inode %llu, nlink = %u, ip_flags = 0x%x\n", |
1126 | (unsigned long long)oi->ip_blkno, inode->i_nlink, oi->ip_flags); | |
ccd979bd | 1127 | |
379dfe9d MF |
1128 | if (oi->ip_flags & OCFS2_INODE_MAYBE_ORPHANED) |
1129 | generic_delete_inode(inode); | |
1130 | else | |
1131 | generic_drop_inode(inode); | |
ccd979bd MF |
1132 | |
1133 | mlog_exit_void(); | |
1134 | } | |
1135 | ||
ccd979bd MF |
1136 | /* |
1137 | * This is called from our getattr. | |
1138 | */ | |
1139 | int ocfs2_inode_revalidate(struct dentry *dentry) | |
1140 | { | |
1141 | struct inode *inode = dentry->d_inode; | |
1142 | int status = 0; | |
1143 | ||
b0697053 MF |
1144 | mlog_entry("(inode = 0x%p, ino = %llu)\n", inode, |
1145 | inode ? (unsigned long long)OCFS2_I(inode)->ip_blkno : 0ULL); | |
ccd979bd MF |
1146 | |
1147 | if (!inode) { | |
1148 | mlog(0, "eep, no inode!\n"); | |
1149 | status = -ENOENT; | |
1150 | goto bail; | |
1151 | } | |
1152 | ||
1153 | spin_lock(&OCFS2_I(inode)->ip_lock); | |
1154 | if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) { | |
1155 | spin_unlock(&OCFS2_I(inode)->ip_lock); | |
1156 | mlog(0, "inode deleted!\n"); | |
1157 | status = -ENOENT; | |
1158 | goto bail; | |
1159 | } | |
1160 | spin_unlock(&OCFS2_I(inode)->ip_lock); | |
1161 | ||
e63aecb6 | 1162 | /* Let ocfs2_inode_lock do the work of updating our struct |
ccd979bd | 1163 | * inode for us. */ |
e63aecb6 | 1164 | status = ocfs2_inode_lock(inode, NULL, 0); |
ccd979bd MF |
1165 | if (status < 0) { |
1166 | if (status != -ENOENT) | |
1167 | mlog_errno(status); | |
1168 | goto bail; | |
1169 | } | |
e63aecb6 | 1170 | ocfs2_inode_unlock(inode, 0); |
ccd979bd MF |
1171 | bail: |
1172 | mlog_exit(status); | |
1173 | ||
1174 | return status; | |
1175 | } | |
1176 | ||
1177 | /* | |
1178 | * Updates a disk inode from a | |
1179 | * struct inode. | |
1180 | * Only takes ip_lock. | |
1181 | */ | |
1fabe148 | 1182 | int ocfs2_mark_inode_dirty(handle_t *handle, |
ccd979bd MF |
1183 | struct inode *inode, |
1184 | struct buffer_head *bh) | |
1185 | { | |
1186 | int status; | |
1187 | struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data; | |
1188 | ||
b0697053 MF |
1189 | mlog_entry("(inode %llu)\n", |
1190 | (unsigned long long)OCFS2_I(inode)->ip_blkno); | |
ccd979bd MF |
1191 | |
1192 | status = ocfs2_journal_access(handle, inode, bh, | |
1193 | OCFS2_JOURNAL_ACCESS_WRITE); | |
1194 | if (status < 0) { | |
1195 | mlog_errno(status); | |
1196 | goto leave; | |
1197 | } | |
1198 | ||
1199 | spin_lock(&OCFS2_I(inode)->ip_lock); | |
1200 | fe->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters); | |
6e4b0d56 | 1201 | ocfs2_get_inode_flags(OCFS2_I(inode)); |
ca4d147e | 1202 | fe->i_attr = cpu_to_le32(OCFS2_I(inode)->ip_attr); |
15b1e36b | 1203 | fe->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features); |
ccd979bd MF |
1204 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
1205 | ||
1206 | fe->i_size = cpu_to_le64(i_size_read(inode)); | |
1207 | fe->i_links_count = cpu_to_le16(inode->i_nlink); | |
1208 | fe->i_uid = cpu_to_le32(inode->i_uid); | |
1209 | fe->i_gid = cpu_to_le32(inode->i_gid); | |
1210 | fe->i_mode = cpu_to_le16(inode->i_mode); | |
1211 | fe->i_atime = cpu_to_le64(inode->i_atime.tv_sec); | |
1212 | fe->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec); | |
1213 | fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec); | |
1214 | fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); | |
1215 | fe->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec); | |
1216 | fe->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec); | |
1217 | ||
1218 | status = ocfs2_journal_dirty(handle, bh); | |
1219 | if (status < 0) | |
1220 | mlog_errno(status); | |
1221 | ||
1222 | status = 0; | |
1223 | leave: | |
1224 | ||
1225 | mlog_exit(status); | |
1226 | return status; | |
1227 | } | |
1228 | ||
1229 | /* | |
1230 | * | |
1231 | * Updates a struct inode from a disk inode. | |
1232 | * does no i/o, only takes ip_lock. | |
1233 | */ | |
1234 | void ocfs2_refresh_inode(struct inode *inode, | |
1235 | struct ocfs2_dinode *fe) | |
1236 | { | |
ccd979bd MF |
1237 | spin_lock(&OCFS2_I(inode)->ip_lock); |
1238 | ||
1239 | OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters); | |
ca4d147e | 1240 | OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr); |
15b1e36b | 1241 | OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features); |
ca4d147e | 1242 | ocfs2_set_inode_flags(inode); |
ccd979bd MF |
1243 | i_size_write(inode, le64_to_cpu(fe->i_size)); |
1244 | inode->i_nlink = le16_to_cpu(fe->i_links_count); | |
1245 | inode->i_uid = le32_to_cpu(fe->i_uid); | |
1246 | inode->i_gid = le32_to_cpu(fe->i_gid); | |
1247 | inode->i_mode = le16_to_cpu(fe->i_mode); | |
ccd979bd MF |
1248 | if (S_ISLNK(inode->i_mode) && le32_to_cpu(fe->i_clusters) == 0) |
1249 | inode->i_blocks = 0; | |
1250 | else | |
8110b073 | 1251 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
ccd979bd MF |
1252 | inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime); |
1253 | inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec); | |
1254 | inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime); | |
1255 | inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec); | |
1256 | inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime); | |
1257 | inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec); | |
1258 | ||
1259 | spin_unlock(&OCFS2_I(inode)->ip_lock); | |
1260 | } | |
b657c95c JB |
1261 | |
1262 | int ocfs2_validate_inode_block(struct super_block *sb, | |
1263 | struct buffer_head *bh) | |
1264 | { | |
1265 | int rc = -EINVAL; | |
1266 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data; | |
1267 | ||
970e4936 JB |
1268 | mlog(0, "Validating dinode %llu\n", |
1269 | (unsigned long long)bh->b_blocknr); | |
1270 | ||
b657c95c JB |
1271 | BUG_ON(!buffer_uptodate(bh)); |
1272 | ||
1273 | if (!OCFS2_IS_VALID_DINODE(di)) { | |
1274 | ocfs2_error(sb, "Invalid dinode #%llu: signature = %.*s\n", | |
1275 | (unsigned long long)bh->b_blocknr, 7, | |
1276 | di->i_signature); | |
1277 | goto bail; | |
1278 | } | |
1279 | ||
1280 | if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) { | |
1281 | ocfs2_error(sb, "Invalid dinode #%llu: i_blkno is %llu\n", | |
1282 | (unsigned long long)bh->b_blocknr, | |
1283 | (unsigned long long)le64_to_cpu(di->i_blkno)); | |
1284 | goto bail; | |
1285 | } | |
1286 | ||
1287 | if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) { | |
1288 | ocfs2_error(sb, | |
1289 | "Invalid dinode #%llu: OCFS2_VALID_FL not set\n", | |
1290 | (unsigned long long)bh->b_blocknr); | |
1291 | goto bail; | |
1292 | } | |
1293 | ||
1294 | if (le32_to_cpu(di->i_fs_generation) != | |
1295 | OCFS2_SB(sb)->fs_generation) { | |
1296 | ocfs2_error(sb, | |
1297 | "Invalid dinode #%llu: fs_generation is %u\n", | |
1298 | (unsigned long long)bh->b_blocknr, | |
1299 | le32_to_cpu(di->i_fs_generation)); | |
1300 | goto bail; | |
1301 | } | |
1302 | ||
1303 | rc = 0; | |
1304 | ||
1305 | bail: | |
1306 | return rc; | |
1307 | } | |
1308 | ||
1309 | int ocfs2_read_inode_block_full(struct inode *inode, struct buffer_head **bh, | |
1310 | int flags) | |
1311 | { | |
1312 | int rc; | |
1313 | struct buffer_head *tmp = *bh; | |
1314 | ||
1315 | rc = ocfs2_read_blocks(inode, OCFS2_I(inode)->ip_blkno, 1, &tmp, | |
970e4936 | 1316 | flags, ocfs2_validate_inode_block); |
b657c95c JB |
1317 | |
1318 | /* If ocfs2_read_blocks() got us a new bh, pass it up. */ | |
970e4936 | 1319 | if (!rc && !*bh) |
b657c95c JB |
1320 | *bh = tmp; |
1321 | ||
b657c95c JB |
1322 | return rc; |
1323 | } | |
1324 | ||
1325 | int ocfs2_read_inode_block(struct inode *inode, struct buffer_head **bh) | |
1326 | { | |
1327 | return ocfs2_read_inode_block_full(inode, bh, 0); | |
1328 | } |