blob: f63c455c3f6964c88515e53a947b3955a92d2298 [file] [log] [blame]
Dave Chinner1fd71152013-08-12 20:49:35 +10001/*
2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18#include "xfs.h"
19#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +110020#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Dave Chinner1fd71152013-08-12 20:49:35 +100024#include "xfs_sb.h"
Dave Chinner1fd71152013-08-12 20:49:35 +100025#include "xfs_mount.h"
Dave Chinner1fd71152013-08-12 20:49:35 +100026#include "xfs_inode.h"
27#include "xfs_error.h"
28#include "xfs_cksum.h"
29#include "xfs_icache.h"
Dave Chinner239880e2013-10-23 10:50:10 +110030#include "xfs_trans.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110031#include "xfs_ialloc.h"
Dave Chinner1fd71152013-08-12 20:49:35 +100032
33/*
34 * Check that none of the inode's in the buffer have a next
35 * unlinked field of 0.
36 */
37#if defined(DEBUG)
38void
39xfs_inobp_check(
40 xfs_mount_t *mp,
41 xfs_buf_t *bp)
42{
43 int i;
44 int j;
45 xfs_dinode_t *dip;
46
47 j = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
48
49 for (i = 0; i < j; i++) {
50 dip = (xfs_dinode_t *)xfs_buf_offset(bp,
51 i * mp->m_sb.sb_inodesize);
52 if (!dip->di_next_unlinked) {
53 xfs_alert(mp,
Dave Chinner74ffa792013-09-03 21:47:38 +100054 "Detected bogus zero next_unlinked field in inode %d buffer 0x%llx.",
55 i, (long long)bp->b_bn);
Dave Chinner1fd71152013-08-12 20:49:35 +100056 }
57 }
58}
59#endif
60
Dave Chinnerd8914002013-08-27 11:39:37 +100061/*
62 * If we are doing readahead on an inode buffer, we might be in log recovery
63 * reading an inode allocation buffer that hasn't yet been replayed, and hence
64 * has not had the inode cores stamped into it. Hence for readahead, the buffer
65 * may be potentially invalid.
66 *
67 * If the readahead buffer is invalid, we don't want to mark it with an error,
68 * but we do want to clear the DONE status of the buffer so that a followup read
69 * will re-read it from disk. This will ensure that we don't get an unnecessary
70 * warnings during log recovery and we don't get unnecssary panics on debug
71 * kernels.
72 */
Dave Chinner1fd71152013-08-12 20:49:35 +100073static void
74xfs_inode_buf_verify(
Dave Chinnerd8914002013-08-27 11:39:37 +100075 struct xfs_buf *bp,
76 bool readahead)
Dave Chinner1fd71152013-08-12 20:49:35 +100077{
78 struct xfs_mount *mp = bp->b_target->bt_mount;
79 int i;
80 int ni;
81
82 /*
83 * Validate the magic number and version of every inode in the buffer
84 */
85 ni = XFS_BB_TO_FSB(mp, bp->b_length) * mp->m_sb.sb_inopblock;
86 for (i = 0; i < ni; i++) {
87 int di_ok;
88 xfs_dinode_t *dip;
89
90 dip = (struct xfs_dinode *)xfs_buf_offset(bp,
91 (i << mp->m_sb.sb_inodelog));
92 di_ok = dip->di_magic == cpu_to_be16(XFS_DINODE_MAGIC) &&
93 XFS_DINODE_GOOD_VERSION(dip->di_version);
94 if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
95 XFS_ERRTAG_ITOBP_INOTOBP,
96 XFS_RANDOM_ITOBP_INOTOBP))) {
Dave Chinnerd8914002013-08-27 11:39:37 +100097 if (readahead) {
98 bp->b_flags &= ~XBF_DONE;
99 return;
100 }
101
Dave Chinner24513372014-06-25 14:58:08 +1000102 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100103 xfs_verifier_error(bp);
Dave Chinner1fd71152013-08-12 20:49:35 +1000104#ifdef DEBUG
Dave Chinner74ffa792013-09-03 21:47:38 +1000105 xfs_alert(mp,
Dave Chinner1fd71152013-08-12 20:49:35 +1000106 "bad inode magic/vsn daddr %lld #%d (magic=%x)",
107 (unsigned long long)bp->b_bn, i,
108 be16_to_cpu(dip->di_magic));
Dave Chinner1fd71152013-08-12 20:49:35 +1000109#endif
110 }
111 }
112 xfs_inobp_check(mp, bp);
113}
114
115
116static void
117xfs_inode_buf_read_verify(
118 struct xfs_buf *bp)
119{
Dave Chinnerd8914002013-08-27 11:39:37 +1000120 xfs_inode_buf_verify(bp, false);
121}
122
123static void
124xfs_inode_buf_readahead_verify(
125 struct xfs_buf *bp)
126{
127 xfs_inode_buf_verify(bp, true);
Dave Chinner1fd71152013-08-12 20:49:35 +1000128}
129
130static void
131xfs_inode_buf_write_verify(
132 struct xfs_buf *bp)
133{
Dave Chinnerd8914002013-08-27 11:39:37 +1000134 xfs_inode_buf_verify(bp, false);
Dave Chinner1fd71152013-08-12 20:49:35 +1000135}
136
137const struct xfs_buf_ops xfs_inode_buf_ops = {
138 .verify_read = xfs_inode_buf_read_verify,
139 .verify_write = xfs_inode_buf_write_verify,
140};
141
Dave Chinnerd8914002013-08-27 11:39:37 +1000142const struct xfs_buf_ops xfs_inode_buf_ra_ops = {
143 .verify_read = xfs_inode_buf_readahead_verify,
144 .verify_write = xfs_inode_buf_write_verify,
145};
146
Dave Chinner1fd71152013-08-12 20:49:35 +1000147
148/*
149 * This routine is called to map an inode to the buffer containing the on-disk
150 * version of the inode. It returns a pointer to the buffer containing the
151 * on-disk inode in the bpp parameter, and in the dipp parameter it returns a
152 * pointer to the on-disk inode within that buffer.
153 *
154 * If a non-zero error is returned, then the contents of bpp and dipp are
155 * undefined.
156 */
157int
158xfs_imap_to_bp(
159 struct xfs_mount *mp,
160 struct xfs_trans *tp,
161 struct xfs_imap *imap,
162 struct xfs_dinode **dipp,
163 struct xfs_buf **bpp,
164 uint buf_flags,
165 uint iget_flags)
166{
167 struct xfs_buf *bp;
168 int error;
169
170 buf_flags |= XBF_UNMAPPED;
171 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap->im_blkno,
172 (int)imap->im_len, buf_flags, &bp,
173 &xfs_inode_buf_ops);
174 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +1000175 if (error == -EAGAIN) {
Dave Chinner1fd71152013-08-12 20:49:35 +1000176 ASSERT(buf_flags & XBF_TRYLOCK);
177 return error;
178 }
179
Dave Chinner24513372014-06-25 14:58:08 +1000180 if (error == -EFSCORRUPTED &&
Dave Chinner1fd71152013-08-12 20:49:35 +1000181 (iget_flags & XFS_IGET_UNTRUSTED))
Dave Chinner24513372014-06-25 14:58:08 +1000182 return -EINVAL;
Dave Chinner1fd71152013-08-12 20:49:35 +1000183
184 xfs_warn(mp, "%s: xfs_trans_read_buf() returned error %d.",
185 __func__, error);
186 return error;
187 }
188
189 *bpp = bp;
190 *dipp = (struct xfs_dinode *)xfs_buf_offset(bp, imap->im_boffset);
191 return 0;
192}
193
Dave Chinner638f44162013-08-30 10:23:45 +1000194void
Dave Chinner1fd71152013-08-12 20:49:35 +1000195xfs_dinode_from_disk(
196 xfs_icdinode_t *to,
197 xfs_dinode_t *from)
198{
199 to->di_magic = be16_to_cpu(from->di_magic);
200 to->di_mode = be16_to_cpu(from->di_mode);
201 to->di_version = from ->di_version;
202 to->di_format = from->di_format;
203 to->di_onlink = be16_to_cpu(from->di_onlink);
204 to->di_uid = be32_to_cpu(from->di_uid);
205 to->di_gid = be32_to_cpu(from->di_gid);
206 to->di_nlink = be32_to_cpu(from->di_nlink);
207 to->di_projid_lo = be16_to_cpu(from->di_projid_lo);
208 to->di_projid_hi = be16_to_cpu(from->di_projid_hi);
209 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
210 to->di_flushiter = be16_to_cpu(from->di_flushiter);
211 to->di_atime.t_sec = be32_to_cpu(from->di_atime.t_sec);
212 to->di_atime.t_nsec = be32_to_cpu(from->di_atime.t_nsec);
213 to->di_mtime.t_sec = be32_to_cpu(from->di_mtime.t_sec);
214 to->di_mtime.t_nsec = be32_to_cpu(from->di_mtime.t_nsec);
215 to->di_ctime.t_sec = be32_to_cpu(from->di_ctime.t_sec);
216 to->di_ctime.t_nsec = be32_to_cpu(from->di_ctime.t_nsec);
217 to->di_size = be64_to_cpu(from->di_size);
218 to->di_nblocks = be64_to_cpu(from->di_nblocks);
219 to->di_extsize = be32_to_cpu(from->di_extsize);
220 to->di_nextents = be32_to_cpu(from->di_nextents);
221 to->di_anextents = be16_to_cpu(from->di_anextents);
222 to->di_forkoff = from->di_forkoff;
223 to->di_aformat = from->di_aformat;
224 to->di_dmevmask = be32_to_cpu(from->di_dmevmask);
225 to->di_dmstate = be16_to_cpu(from->di_dmstate);
226 to->di_flags = be16_to_cpu(from->di_flags);
227 to->di_gen = be32_to_cpu(from->di_gen);
228
229 if (to->di_version == 3) {
230 to->di_changecount = be64_to_cpu(from->di_changecount);
231 to->di_crtime.t_sec = be32_to_cpu(from->di_crtime.t_sec);
232 to->di_crtime.t_nsec = be32_to_cpu(from->di_crtime.t_nsec);
233 to->di_flags2 = be64_to_cpu(from->di_flags2);
234 to->di_ino = be64_to_cpu(from->di_ino);
235 to->di_lsn = be64_to_cpu(from->di_lsn);
236 memcpy(to->di_pad2, from->di_pad2, sizeof(to->di_pad2));
237 uuid_copy(&to->di_uuid, &from->di_uuid);
238 }
239}
240
241void
242xfs_dinode_to_disk(
243 xfs_dinode_t *to,
244 xfs_icdinode_t *from)
245{
246 to->di_magic = cpu_to_be16(from->di_magic);
247 to->di_mode = cpu_to_be16(from->di_mode);
248 to->di_version = from ->di_version;
249 to->di_format = from->di_format;
250 to->di_onlink = cpu_to_be16(from->di_onlink);
251 to->di_uid = cpu_to_be32(from->di_uid);
252 to->di_gid = cpu_to_be32(from->di_gid);
253 to->di_nlink = cpu_to_be32(from->di_nlink);
254 to->di_projid_lo = cpu_to_be16(from->di_projid_lo);
255 to->di_projid_hi = cpu_to_be16(from->di_projid_hi);
256 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
257 to->di_atime.t_sec = cpu_to_be32(from->di_atime.t_sec);
258 to->di_atime.t_nsec = cpu_to_be32(from->di_atime.t_nsec);
259 to->di_mtime.t_sec = cpu_to_be32(from->di_mtime.t_sec);
260 to->di_mtime.t_nsec = cpu_to_be32(from->di_mtime.t_nsec);
261 to->di_ctime.t_sec = cpu_to_be32(from->di_ctime.t_sec);
262 to->di_ctime.t_nsec = cpu_to_be32(from->di_ctime.t_nsec);
263 to->di_size = cpu_to_be64(from->di_size);
264 to->di_nblocks = cpu_to_be64(from->di_nblocks);
265 to->di_extsize = cpu_to_be32(from->di_extsize);
266 to->di_nextents = cpu_to_be32(from->di_nextents);
267 to->di_anextents = cpu_to_be16(from->di_anextents);
268 to->di_forkoff = from->di_forkoff;
269 to->di_aformat = from->di_aformat;
270 to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
271 to->di_dmstate = cpu_to_be16(from->di_dmstate);
272 to->di_flags = cpu_to_be16(from->di_flags);
273 to->di_gen = cpu_to_be32(from->di_gen);
274
275 if (from->di_version == 3) {
276 to->di_changecount = cpu_to_be64(from->di_changecount);
277 to->di_crtime.t_sec = cpu_to_be32(from->di_crtime.t_sec);
278 to->di_crtime.t_nsec = cpu_to_be32(from->di_crtime.t_nsec);
279 to->di_flags2 = cpu_to_be64(from->di_flags2);
280 to->di_ino = cpu_to_be64(from->di_ino);
281 to->di_lsn = cpu_to_be64(from->di_lsn);
282 memcpy(to->di_pad2, from->di_pad2, sizeof(to->di_pad2));
283 uuid_copy(&to->di_uuid, &from->di_uuid);
284 to->di_flushiter = 0;
285 } else {
286 to->di_flushiter = cpu_to_be16(from->di_flushiter);
287 }
288}
289
290static bool
291xfs_dinode_verify(
292 struct xfs_mount *mp,
293 struct xfs_inode *ip,
294 struct xfs_dinode *dip)
295{
296 if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC))
297 return false;
298
299 /* only version 3 or greater inodes are extensively verified here */
300 if (dip->di_version < 3)
301 return true;
302
303 if (!xfs_sb_version_hascrc(&mp->m_sb))
304 return false;
305 if (!xfs_verify_cksum((char *)dip, mp->m_sb.sb_inodesize,
Eric Sandeen533b81c2014-02-27 15:15:27 +1100306 XFS_DINODE_CRC_OFF))
Dave Chinner1fd71152013-08-12 20:49:35 +1000307 return false;
308 if (be64_to_cpu(dip->di_ino) != ip->i_ino)
309 return false;
310 if (!uuid_equal(&dip->di_uuid, &mp->m_sb.sb_uuid))
311 return false;
312 return true;
313}
314
315void
316xfs_dinode_calc_crc(
317 struct xfs_mount *mp,
318 struct xfs_dinode *dip)
319{
320 __uint32_t crc;
321
322 if (dip->di_version < 3)
323 return;
324
325 ASSERT(xfs_sb_version_hascrc(&mp->m_sb));
326 crc = xfs_start_cksum((char *)dip, mp->m_sb.sb_inodesize,
Eric Sandeen533b81c2014-02-27 15:15:27 +1100327 XFS_DINODE_CRC_OFF);
Dave Chinner1fd71152013-08-12 20:49:35 +1000328 dip->di_crc = xfs_end_cksum(crc);
329}
330
331/*
332 * Read the disk inode attributes into the in-core inode structure.
333 *
334 * For version 5 superblocks, if we are initialising a new inode and we are not
335 * utilising the XFS_MOUNT_IKEEP inode cluster mode, we can simple build the new
336 * inode core with a random generation number. If we are keeping inodes around,
337 * we need to read the inode cluster to get the existing generation number off
338 * disk. Further, if we are using version 4 superblocks (i.e. v1/v2 inode
339 * format) then log recovery is dependent on the di_flushiter field being
340 * initialised from the current on-disk value and hence we must also read the
341 * inode off disk.
342 */
343int
344xfs_iread(
345 xfs_mount_t *mp,
346 xfs_trans_t *tp,
347 xfs_inode_t *ip,
348 uint iget_flags)
349{
350 xfs_buf_t *bp;
351 xfs_dinode_t *dip;
352 int error;
353
354 /*
355 * Fill in the location information in the in-core inode.
356 */
357 error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, iget_flags);
358 if (error)
359 return error;
360
361 /* shortcut IO on inode allocation if possible */
362 if ((iget_flags & XFS_IGET_CREATE) &&
363 xfs_sb_version_hascrc(&mp->m_sb) &&
364 !(mp->m_flags & XFS_MOUNT_IKEEP)) {
365 /* initialise the on-disk inode core */
366 memset(&ip->i_d, 0, sizeof(ip->i_d));
367 ip->i_d.di_magic = XFS_DINODE_MAGIC;
368 ip->i_d.di_gen = prandom_u32();
369 if (xfs_sb_version_hascrc(&mp->m_sb)) {
370 ip->i_d.di_version = 3;
371 ip->i_d.di_ino = ip->i_ino;
372 uuid_copy(&ip->i_d.di_uuid, &mp->m_sb.sb_uuid);
373 } else
374 ip->i_d.di_version = 2;
375 return 0;
376 }
377
378 /*
379 * Get pointers to the on-disk inode and the buffer containing it.
380 */
381 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &bp, 0, iget_flags);
382 if (error)
383 return error;
384
385 /* even unallocated inodes are verified */
386 if (!xfs_dinode_verify(mp, ip, dip)) {
387 xfs_alert(mp, "%s: validation failed for inode %lld failed",
388 __func__, ip->i_ino);
389
390 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, dip);
Dave Chinner24513372014-06-25 14:58:08 +1000391 error = -EFSCORRUPTED;
Dave Chinner1fd71152013-08-12 20:49:35 +1000392 goto out_brelse;
393 }
394
395 /*
396 * If the on-disk inode is already linked to a directory
397 * entry, copy all of the inode into the in-core inode.
398 * xfs_iformat_fork() handles copying in the inode format
399 * specific information.
400 * Otherwise, just get the truly permanent information.
401 */
402 if (dip->di_mode) {
403 xfs_dinode_from_disk(&ip->i_d, dip);
404 error = xfs_iformat_fork(ip, dip);
405 if (error) {
406#ifdef DEBUG
407 xfs_alert(mp, "%s: xfs_iformat() returned error %d",
408 __func__, error);
409#endif /* DEBUG */
410 goto out_brelse;
411 }
412 } else {
413 /*
414 * Partial initialisation of the in-core inode. Just the bits
415 * that xfs_ialloc won't overwrite or relies on being correct.
416 */
417 ip->i_d.di_magic = be16_to_cpu(dip->di_magic);
418 ip->i_d.di_version = dip->di_version;
419 ip->i_d.di_gen = be32_to_cpu(dip->di_gen);
420 ip->i_d.di_flushiter = be16_to_cpu(dip->di_flushiter);
421
422 if (dip->di_version == 3) {
423 ip->i_d.di_ino = be64_to_cpu(dip->di_ino);
424 uuid_copy(&ip->i_d.di_uuid, &dip->di_uuid);
425 }
426
427 /*
428 * Make sure to pull in the mode here as well in
429 * case the inode is released without being used.
430 * This ensures that xfs_inactive() will see that
431 * the inode is already free and not try to mess
432 * with the uninitialized part of it.
433 */
434 ip->i_d.di_mode = 0;
435 }
436
437 /*
Dave Chinner263997a2014-05-20 07:46:40 +1000438 * Automatically convert version 1 inode formats in memory to version 2
439 * inode format. If the inode is modified, it will get logged and
440 * rewritten as a version 2 inode. We can do this because we set the
441 * superblock feature bit for v2 inodes unconditionally during mount
442 * and it means the reast of the code can assume the inode version is 2
443 * or higher.
Dave Chinner1fd71152013-08-12 20:49:35 +1000444 */
445 if (ip->i_d.di_version == 1) {
Dave Chinner263997a2014-05-20 07:46:40 +1000446 ip->i_d.di_version = 2;
447 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
Dave Chinner1fd71152013-08-12 20:49:35 +1000448 ip->i_d.di_nlink = ip->i_d.di_onlink;
449 ip->i_d.di_onlink = 0;
450 xfs_set_projid(ip, 0);
451 }
452
453 ip->i_delayed_blks = 0;
454
455 /*
456 * Mark the buffer containing the inode as something to keep
457 * around for a while. This helps to keep recently accessed
458 * meta-data in-core longer.
459 */
460 xfs_buf_set_ref(bp, XFS_INO_REF);
461
462 /*
463 * Use xfs_trans_brelse() to release the buffer containing the on-disk
464 * inode, because it was acquired with xfs_trans_read_buf() in
465 * xfs_imap_to_bp() above. If tp is NULL, this is just a normal
466 * brelse(). If we're within a transaction, then xfs_trans_brelse()
467 * will only release the buffer if it is not dirty within the
468 * transaction. It will be OK to release the buffer in this case,
469 * because inodes on disk are never destroyed and we will be locking the
470 * new in-core inode before putting it in the cache where other
471 * processes can find it. Thus we don't have to worry about the inode
472 * being changed just because we released the buffer.
473 */
474 out_brelse:
475 xfs_trans_brelse(tp, bp);
476 return error;
477}