blob: 9134dcb894790adfd487101565703e4e24a37075 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Bob Petersonca390602008-01-28 11:15:57 -06003 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
9
10#include <linux/sched.h>
11#include <linux/slab.h>
12#include <linux/spinlock.h>
13#include <linux/completion.h>
14#include <linux/buffer_head.h>
15#include <linux/posix_acl.h>
16#include <linux/sort.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050017#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050018#include <linux/crc32.h>
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -040019#include <linux/security.h>
Steven Whitehouse719ee342008-09-18 13:53:59 +010020#include <linux/time.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000021
22#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050023#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000024#include "acl.h"
25#include "bmap.h"
26#include "dir.h"
Steven Whitehouse307cf6e2009-08-26 18:51:04 +010027#include "xattr.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000028#include "glock.h"
29#include "glops.h"
30#include "inode.h"
31#include "log.h"
32#include "meta_io.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000033#include "quota.h"
34#include "rgrp.h"
35#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050036#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000037
Steven Whitehousebb8d8a62007-06-01 14:11:58 +010038struct gfs2_inum_range_host {
39 u64 ir_start;
40 u64 ir_length;
41};
42
Bob Peterson44ad37d2011-03-17 16:19:58 -040043struct gfs2_skip_data {
44 u64 no_addr;
45 int skipped;
46 int non_block;
47};
48
David Teiglandb3b94fa2006-01-16 16:50:04 +000049static int iget_test(struct inode *inode, void *opaque)
50{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040051 struct gfs2_inode *ip = GFS2_I(inode);
Bob Peterson44ad37d2011-03-17 16:19:58 -040052 struct gfs2_skip_data *data = opaque;
David Teiglandb3b94fa2006-01-16 16:50:04 +000053
Bob Peterson44ad37d2011-03-17 16:19:58 -040054 if (ip->i_no_addr == data->no_addr) {
55 if (data->non_block &&
56 inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) {
57 data->skipped = 1;
58 return 0;
59 }
David Teiglandb3b94fa2006-01-16 16:50:04 +000060 return 1;
Bob Peterson44ad37d2011-03-17 16:19:58 -040061 }
David Teiglandb3b94fa2006-01-16 16:50:04 +000062 return 0;
63}
64
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040065static int iget_set(struct inode *inode, void *opaque)
66{
67 struct gfs2_inode *ip = GFS2_I(inode);
Bob Peterson44ad37d2011-03-17 16:19:58 -040068 struct gfs2_skip_data *data = opaque;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040069
Bob Peterson44ad37d2011-03-17 16:19:58 -040070 if (data->skipped)
71 return -ENOENT;
72 inode->i_ino = (unsigned long)(data->no_addr);
73 ip->i_no_addr = data->no_addr;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040074 return 0;
75}
76
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010077struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr)
David Teiglandb3b94fa2006-01-16 16:50:04 +000078{
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010079 unsigned long hash = (unsigned long)no_addr;
Bob Peterson44ad37d2011-03-17 16:19:58 -040080 struct gfs2_skip_data data;
81
82 data.no_addr = no_addr;
83 data.skipped = 0;
84 data.non_block = 0;
85 return ilookup5(sb, hash, iget_test, &data);
David Teiglandb3b94fa2006-01-16 16:50:04 +000086}
87
Bob Peterson44ad37d2011-03-17 16:19:58 -040088static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr,
89 int non_block)
David Teiglandb3b94fa2006-01-16 16:50:04 +000090{
Bob Peterson44ad37d2011-03-17 16:19:58 -040091 struct gfs2_skip_data data;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010092 unsigned long hash = (unsigned long)no_addr;
Bob Peterson44ad37d2011-03-17 16:19:58 -040093
94 data.no_addr = no_addr;
95 data.skipped = 0;
96 data.non_block = non_block;
97 return iget5_locked(sb, hash, iget_test, iget_set, &data);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040098}
99
100/**
Steven Whitehouse24d97652011-01-18 14:49:08 +0000101 * gfs2_set_iop - Sets inode operations
102 * @inode: The inode with correct i_mode filled in
Wendy Cheng35dcc522007-06-27 17:07:53 -0400103 *
Steven Whitehouse24d97652011-01-18 14:49:08 +0000104 * GFS2 lookup code fills in vfs inode contents based on info obtained
105 * from directory entry inside gfs2_inode_lookup().
106 */
Wendy Cheng35dcc522007-06-27 17:07:53 -0400107
Steven Whitehouse24d97652011-01-18 14:49:08 +0000108static void gfs2_set_iop(struct inode *inode)
Wendy Cheng35dcc522007-06-27 17:07:53 -0400109{
Wendy Chengc97bfe42007-11-29 17:56:51 -0500110 struct gfs2_sbd *sdp = GFS2_SB(inode);
Wendy Cheng35dcc522007-06-27 17:07:53 -0400111 umode_t mode = inode->i_mode;
112
113 if (S_ISREG(mode)) {
114 inode->i_op = &gfs2_file_iops;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000115 if (gfs2_localflocks(sdp))
Christoph Hellwig10d21982009-04-07 19:42:17 +0200116 inode->i_fop = &gfs2_file_fops_nolock;
Wendy Chengc97bfe42007-11-29 17:56:51 -0500117 else
Christoph Hellwig10d21982009-04-07 19:42:17 +0200118 inode->i_fop = &gfs2_file_fops;
Wendy Cheng35dcc522007-06-27 17:07:53 -0400119 } else if (S_ISDIR(mode)) {
120 inode->i_op = &gfs2_dir_iops;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000121 if (gfs2_localflocks(sdp))
Christoph Hellwig10d21982009-04-07 19:42:17 +0200122 inode->i_fop = &gfs2_dir_fops_nolock;
Wendy Chengc97bfe42007-11-29 17:56:51 -0500123 else
Christoph Hellwig10d21982009-04-07 19:42:17 +0200124 inode->i_fop = &gfs2_dir_fops;
Wendy Cheng35dcc522007-06-27 17:07:53 -0400125 } else if (S_ISLNK(mode)) {
126 inode->i_op = &gfs2_symlink_iops;
127 } else {
Denis Chengd83225d2008-02-26 15:25:03 +0800128 inode->i_op = &gfs2_file_iops;
Denis Cheng43a33c52008-02-26 15:25:04 +0800129 init_special_inode(inode, inode->i_mode, inode->i_rdev);
Wendy Cheng35dcc522007-06-27 17:07:53 -0400130 }
Wendy Cheng35dcc522007-06-27 17:07:53 -0400131}
132
133/**
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400134 * gfs2_inode_lookup - Lookup an inode
135 * @sb: The super block
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100136 * @no_addr: The inode number
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400137 * @type: The type of the inode
Bob Peterson44ad37d2011-03-17 16:19:58 -0400138 * non_block: Can we block on inodes that are being freed?
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400139 *
140 * Returns: A VFS inode, or an error
141 */
142
Steven Whitehouse24d97652011-01-18 14:49:08 +0000143struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
Bob Peterson44ad37d2011-03-17 16:19:58 -0400144 u64 no_addr, u64 no_formal_ino, int non_block)
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400145{
Benjamin Marzinski7a9f53b2007-09-18 13:33:18 -0500146 struct inode *inode;
147 struct gfs2_inode *ip;
Bob Petersonb1becbd2010-06-17 16:45:37 -0400148 struct gfs2_glock *io_gl = NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400149 int error;
150
Bob Peterson44ad37d2011-03-17 16:19:58 -0400151 inode = gfs2_iget(sb, no_addr, non_block);
Benjamin Marzinski7a9f53b2007-09-18 13:33:18 -0500152 ip = GFS2_I(inode);
153
Steven Whitehouse26d83de2006-10-30 16:59:08 -0500154 if (!inode)
155 return ERR_PTR(-ENOBUFS);
156
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400157 if (inode->i_state & I_NEW) {
158 struct gfs2_sbd *sdp = GFS2_SB(inode);
Wendy Chengbb9bcf02007-06-27 17:07:08 -0400159 ip->i_no_formal_ino = no_formal_ino;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400160
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100161 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400162 if (unlikely(error))
163 goto fail;
164 ip->i_gl->gl_object = ip;
165
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100166 error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400167 if (unlikely(error))
168 goto fail_put;
169
Steven Whitehousebfded272006-11-01 16:05:38 -0500170 set_bit(GIF_INVALID, &ip->i_flags);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400171 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
172 if (unlikely(error))
173 goto fail_iopen;
174
Steven Whitehouse24d97652011-01-18 14:49:08 +0000175 ip->i_iopen_gh.gh_gl->gl_object = ip;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400176 gfs2_glock_put(io_gl);
Bob Petersonb1becbd2010-06-17 16:45:37 -0400177 io_gl = NULL;
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100178
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100179 if (type == DT_UNKNOWN) {
Steven Whitehouse24d97652011-01-18 14:49:08 +0000180 /* Inode glock must be locked already */
181 error = gfs2_inode_refresh(GFS2_I(inode));
182 if (error)
183 goto fail_refresh;
184 } else {
185 inode->i_mode = DT2IF(type);
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100186 }
187
Wendy Cheng35dcc522007-06-27 17:07:53 -0400188 gfs2_set_iop(inode);
Steven Whitehouse24d97652011-01-18 14:49:08 +0000189 unlock_new_inode(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000190 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400191
192 return inode;
Steven Whitehouse24d97652011-01-18 14:49:08 +0000193
194fail_refresh:
195 ip->i_iopen_gh.gh_gl->gl_object = NULL;
196 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400197fail_iopen:
Bob Petersonb1becbd2010-06-17 16:45:37 -0400198 if (io_gl)
199 gfs2_glock_put(io_gl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400200fail_put:
Steven Whitehouse24d97652011-01-18 14:49:08 +0000201 ip->i_gl->gl_object = NULL;
Bob Peterson1a0eae82010-04-14 11:58:16 -0400202 gfs2_glock_put(ip->i_gl);
203fail:
Steven Whitehouse24d97652011-01-18 14:49:08 +0000204 iget_failed(inode);
Bob Peterson1a0eae82010-04-14 11:58:16 -0400205 return ERR_PTR(error);
206}
207
Steven Whitehouse044b9412010-11-03 20:01:07 +0000208struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
209 u64 *no_formal_ino, unsigned int blktype)
Bob Peterson1a0eae82010-04-14 11:58:16 -0400210{
Steven Whitehouse044b9412010-11-03 20:01:07 +0000211 struct super_block *sb = sdp->sd_vfs;
212 struct gfs2_holder i_gh;
Bob Peterson44ad37d2011-03-17 16:19:58 -0400213 struct inode *inode = NULL;
Steven Whitehouse044b9412010-11-03 20:01:07 +0000214 int error;
Bob Peterson1a0eae82010-04-14 11:58:16 -0400215
Bob Peterson44ad37d2011-03-17 16:19:58 -0400216 /* Must not read in block until block type is verified */
Steven Whitehouse044b9412010-11-03 20:01:07 +0000217 error = gfs2_glock_nq_num(sdp, no_addr, &gfs2_inode_glops,
Bob Peterson44ad37d2011-03-17 16:19:58 -0400218 LM_ST_EXCLUSIVE, GL_SKIP, &i_gh);
Steven Whitehouse044b9412010-11-03 20:01:07 +0000219 if (error)
220 return ERR_PTR(error);
Bob Peterson1a0eae82010-04-14 11:58:16 -0400221
Steven Whitehouse044b9412010-11-03 20:01:07 +0000222 error = gfs2_check_blk_type(sdp, no_addr, blktype);
223 if (error)
224 goto fail;
Bob Peterson1a0eae82010-04-14 11:58:16 -0400225
Bob Peterson44ad37d2011-03-17 16:19:58 -0400226 inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0, 1);
Steven Whitehouse044b9412010-11-03 20:01:07 +0000227 if (IS_ERR(inode))
228 goto fail;
229
Steven Whitehouse044b9412010-11-03 20:01:07 +0000230 /* Two extra checks for NFS only */
231 if (no_formal_ino) {
232 error = -ESTALE;
233 if (GFS2_I(inode)->i_no_formal_ino != *no_formal_ino)
234 goto fail_iput;
235
236 error = -EIO;
237 if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
238 goto fail_iput;
239
240 error = 0;
Bob Petersoned4878e2010-05-20 23:30:11 -0400241 }
Bob Peterson1a0eae82010-04-14 11:58:16 -0400242
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400243fail:
Steven Whitehouse044b9412010-11-03 20:01:07 +0000244 gfs2_glock_dq_uninit(&i_gh);
245 return error ? ERR_PTR(error) : inode;
246fail_iput:
247 iput(inode);
248 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000249}
250
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500251static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
Steven Whitehouseea744d02006-10-31 15:28:00 -0500252{
Steven Whitehouseea744d02006-10-31 15:28:00 -0500253 const struct gfs2_dinode *str = buf;
Steven Whitehouse719ee342008-09-18 13:53:59 +0100254 struct timespec atime;
Steven Whitehouse9a004502008-02-01 09:23:44 +0000255 u16 height, depth;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500256
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000257 if (unlikely(ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)))
258 goto corrupt;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100259 ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500260 ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500261 ip->i_inode.i_rdev = 0;
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500262 switch (ip->i_inode.i_mode & S_IFMT) {
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500263 case S_IFBLK:
264 case S_IFCHR:
265 ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
266 be32_to_cpu(str->di_minor));
267 break;
268 };
269
Steven Whitehouse2933f922006-11-01 13:23:29 -0500270 ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
271 ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
Steven Whitehouse4f561102006-11-01 14:04:17 -0500272 /*
273 * We will need to review setting the nlink count here in the
274 * light of the forthcoming ro bind mount work. This is a reminder
275 * to do that.
276 */
277 ip->i_inode.i_nlink = be32_to_cpu(str->di_nlink);
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100278 i_size_write(&ip->i_inode, be64_to_cpu(str->di_size));
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000279 gfs2_set_inode_blocks(&ip->i_inode, be64_to_cpu(str->di_blocks));
Steven Whitehouse719ee342008-09-18 13:53:59 +0100280 atime.tv_sec = be64_to_cpu(str->di_atime);
281 atime.tv_nsec = be32_to_cpu(str->di_atime_nsec);
282 if (timespec_compare(&ip->i_inode.i_atime, &atime) < 0)
283 ip->i_inode.i_atime = atime;
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -0500284 ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100285 ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec);
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -0500286 ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100287 ip->i_inode.i_ctime.tv_nsec = be32_to_cpu(str->di_ctime_nsec);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500288
Steven Whitehousece276b02008-02-06 09:25:45 +0000289 ip->i_goal = be64_to_cpu(str->di_goal_meta);
Steven Whitehousebcf0b5b2008-11-03 13:39:46 +0000290 ip->i_generation = be64_to_cpu(str->di_generation);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500291
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000292 ip->i_diskflags = be32_to_cpu(str->di_flags);
Steven Whitehouse6b124d82006-11-08 12:51:06 -0500293 gfs2_set_inode_flags(&ip->i_inode);
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000294 height = be16_to_cpu(str->di_height);
295 if (unlikely(height > GFS2_MAX_META_HEIGHT))
296 goto corrupt;
297 ip->i_height = (u8)height;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500298
Steven Whitehouse9a004502008-02-01 09:23:44 +0000299 depth = be16_to_cpu(str->di_depth);
300 if (unlikely(depth > GFS2_DIR_MAX_DEPTH))
301 goto corrupt;
302 ip->i_depth = (u8)depth;
Steven Whitehousead6203f2008-11-03 13:59:19 +0000303 ip->i_entries = be32_to_cpu(str->di_entries);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500304
Steven Whitehouse3767ac22008-11-03 14:28:42 +0000305 ip->i_eattr = be64_to_cpu(str->di_eattr);
Steven Whitehouse55610932007-10-17 08:47:38 +0100306 if (S_ISREG(ip->i_inode.i_mode))
307 gfs2_set_aops(&ip->i_inode);
308
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500309 return 0;
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000310corrupt:
311 if (gfs2_consist_inode(ip))
312 gfs2_dinode_print(ip);
313 return -EIO;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500314}
315
David Teiglandb3b94fa2006-01-16 16:50:04 +0000316/**
317 * gfs2_inode_refresh - Refresh the incore copy of the dinode
318 * @ip: The GFS2 inode
319 *
320 * Returns: errno
321 */
322
323int gfs2_inode_refresh(struct gfs2_inode *ip)
324{
325 struct buffer_head *dibh;
326 int error;
327
328 error = gfs2_meta_inode_buffer(ip, &dibh);
329 if (error)
330 return error;
331
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400332 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000333 brelse(dibh);
334 return -EIO;
335 }
336
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500337 error = gfs2_dinode_in(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000338 brelse(dibh);
Steven Whitehousebfded272006-11-01 16:05:38 -0500339 clear_bit(GIF_INVALID, &ip->i_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000340
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500341 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000342}
343
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400344int gfs2_dinode_dealloc(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000345{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400346 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000347 struct gfs2_alloc *al;
348 struct gfs2_rgrpd *rgd;
349 int error;
350
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000351 if (gfs2_get_inode_blocks(&ip->i_inode) != 1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000352 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500353 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000354 return -EIO;
355 }
356
357 al = gfs2_alloc_get(ip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300358 if (!al)
359 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000360
361 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
362 if (error)
363 goto out;
364
365 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
366 if (error)
367 goto out_qs;
368
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100369 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000370 if (!rgd) {
371 gfs2_consist_inode(ip);
372 error = -EIO;
373 goto out_rindex_relse;
374 }
375
376 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
377 &al->al_rgd_gh);
378 if (error)
379 goto out_rindex_relse;
380
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400381 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000382 if (error)
383 goto out_rg_gunlock;
384
Steven Whitehouse2bcd6102007-11-08 14:25:12 +0000385 set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
386 set_bit(GLF_LFLUSH, &ip->i_gl->gl_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000387
388 gfs2_free_di(rgd, ip);
389
David Teiglandb3b94fa2006-01-16 16:50:04 +0000390 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000391
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400392out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000393 gfs2_glock_dq_uninit(&al->al_rgd_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400394out_rindex_relse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000395 gfs2_glock_dq_uninit(&al->al_ri_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400396out_qs:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000397 gfs2_quota_unhold(ip);
Steven Whitehouse36327522006-04-28 10:46:21 -0400398out:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400399 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000400 return error;
401}
402
403/**
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500404 * gfs2_change_nlink - Change nlink count on inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000405 * @ip: The GFS2 inode
406 * @diff: The change in the nlink count required
407 *
408 * Returns: errno
409 */
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500410int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000411{
412 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400413 u32 nlink;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000414 int error;
415
Steven Whitehouse4f561102006-11-01 14:04:17 -0500416 BUG_ON(diff != 1 && diff != -1);
417 nlink = ip->i_inode.i_nlink + diff;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000418
419 /* If we are reducing the nlink count, but the new value ends up being
420 bigger than the old one, we must have underflowed. */
Steven Whitehouse4f561102006-11-01 14:04:17 -0500421 if (diff < 0 && nlink > ip->i_inode.i_nlink) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000422 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500423 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000424 return -EIO;
425 }
426
427 error = gfs2_meta_inode_buffer(ip, &dibh);
428 if (error)
429 return error;
430
Steven Whitehouse4f561102006-11-01 14:04:17 -0500431 if (diff > 0)
432 inc_nlink(&ip->i_inode);
433 else
434 drop_nlink(&ip->i_inode);
435
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100436 ip->i_inode.i_ctime = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000437
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000438 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500439 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000440 brelse(dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400441 mark_inode_dirty(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000442
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500443 if (ip->i_inode.i_nlink == 0)
Russell Cattelanddee7602007-01-29 17:13:44 -0600444 gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500445
S. Wendy Cheng55098262007-01-18 15:56:34 -0500446 return error;
447}
448
Steven Whitehousec7526662006-03-20 12:30:04 -0500449struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
450{
451 struct qstr qstr;
Russell Cattelan6c93fd12007-01-08 17:47:51 -0600452 struct inode *inode;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500453 gfs2_str2qstr(&qstr, name);
Al Viroa569c712008-07-23 14:42:05 -0400454 inode = gfs2_lookupi(dip, &qstr, 1);
Russell Cattelan6c93fd12007-01-08 17:47:51 -0600455 /* gfs2_lookupi has inconsistent callers: vfs
456 * related routines expect NULL for no entry found,
457 * gfs2_lookup_simple callers expect ENOENT
458 * and do not check for NULL.
459 */
460 if (inode == NULL)
461 return ERR_PTR(-ENOENT);
462 else
463 return inode;
Steven Whitehousec7526662006-03-20 12:30:04 -0500464}
465
466
David Teiglandb3b94fa2006-01-16 16:50:04 +0000467/**
468 * gfs2_lookupi - Look up a filename in a directory and return its inode
469 * @d_gh: An initialized holder for the directory glock
470 * @name: The name of the inode to look for
471 * @is_root: If 1, ignore the caller's permissions
472 * @i_gh: An uninitialized holder for the new inode glock
473 *
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000474 * This can be called via the VFS filldir function when NFS is doing
475 * a readdirplus and the inode which its intending to stat isn't
476 * already in cache. In this case we must not take the directory glock
477 * again, since the readdir call will have already taken that lock.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000478 *
479 * Returns: errno
480 */
481
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400482struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
Al Viroa569c712008-07-23 14:42:05 -0400483 int is_root)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000484{
Steven Whitehousec9fd4302006-03-01 15:31:02 -0500485 struct super_block *sb = dir->i_sb;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400486 struct gfs2_inode *dip = GFS2_I(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000487 struct gfs2_holder d_gh;
akpm@linux-foundation.org037bcbb2007-06-08 16:42:14 -0700488 int error = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500489 struct inode *inode = NULL;
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000490 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000491
492 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehousec7526662006-03-20 12:30:04 -0500493 return ERR_PTR(-ENAMETOOLONG);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000494
Steven Whitehousec7526662006-03-20 12:30:04 -0500495 if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
496 (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
497 dir == sb->s_root->d_inode)) {
Steven Whitehouse320dd102006-05-18 16:25:27 -0400498 igrab(dir);
499 return dir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000500 }
501
Steven Whitehouse7afd88d2008-02-22 16:07:18 +0000502 if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000503 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
504 if (error)
505 return ERR_PTR(error);
506 unlock = 1;
507 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000508
509 if (!is_root) {
Nick Pigginb74c79e2011-01-07 17:49:58 +1100510 error = gfs2_permission(dir, MAY_EXEC, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000511 if (error)
512 goto out;
513 }
514
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100515 inode = gfs2_dir_search(dir, name);
516 if (IS_ERR(inode))
517 error = PTR_ERR(inode);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000518out:
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000519 if (unlock)
520 gfs2_glock_dq_uninit(&d_gh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500521 if (error == -ENOENT)
522 return NULL;
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000523 return inode ? inode : ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000524}
525
David Teiglandb3b94fa2006-01-16 16:50:04 +0000526/**
527 * create_ok - OK to create a new on-disk inode here?
528 * @dip: Directory in which dinode is to be created
529 * @name: Name of new dinode
530 * @mode:
531 *
532 * Returns: errno
533 */
534
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400535static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000536 unsigned int mode)
537{
538 int error;
539
Nick Pigginb74c79e2011-01-07 17:49:58 +1100540 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000541 if (error)
542 return error;
543
544 /* Don't create entries in an unlinked directory */
Steven Whitehouse4f561102006-11-01 14:04:17 -0500545 if (!dip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000546 return -EPERM;
547
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100548 error = gfs2_dir_check(&dip->i_inode, name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000549 switch (error) {
550 case -ENOENT:
551 error = 0;
552 break;
553 case 0:
554 return -EEXIST;
555 default:
556 return error;
557 }
558
Steven Whitehousead6203f2008-11-03 13:59:19 +0000559 if (dip->i_entries == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000560 return -EFBIG;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500561 if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000562 return -EMLINK;
563
564 return 0;
565}
566
567static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
568 unsigned int *uid, unsigned int *gid)
569{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400570 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
Steven Whitehouse2933f922006-11-01 13:23:29 -0500571 (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000572 if (S_ISDIR(*mode))
573 *mode |= S_ISUID;
David Howells3de7be32008-11-14 10:38:53 +1100574 else if (dip->i_inode.i_uid != current_fsuid())
David Teiglandb3b94fa2006-01-16 16:50:04 +0000575 *mode &= ~07111;
Steven Whitehouse2933f922006-11-01 13:23:29 -0500576 *uid = dip->i_inode.i_uid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000577 } else
David Howells3de7be32008-11-14 10:38:53 +1100578 *uid = current_fsuid();
David Teiglandb3b94fa2006-01-16 16:50:04 +0000579
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500580 if (dip->i_inode.i_mode & S_ISGID) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000581 if (S_ISDIR(*mode))
582 *mode |= S_ISGID;
Steven Whitehouse2933f922006-11-01 13:23:29 -0500583 *gid = dip->i_inode.i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000584 } else
David Howells3de7be32008-11-14 10:38:53 +1100585 *gid = current_fsgid();
David Teiglandb3b94fa2006-01-16 16:50:04 +0000586}
587
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100588static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000589{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400590 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000591 int error;
592
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000593 if (gfs2_alloc_get(dip) == NULL)
594 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000595
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000596 dip->i_alloc->al_requested = RES_DINODE;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000597 error = gfs2_inplace_reserve(dip);
598 if (error)
599 goto out;
600
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400601 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000602 if (error)
603 goto out_ipreserv;
604
Steven Whitehouse6050b9c2009-07-31 16:19:40 +0100605 error = gfs2_alloc_di(dip, no_addr, generation);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000606
607 gfs2_trans_end(sdp);
608
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400609out_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000610 gfs2_inplace_release(dip);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400611out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000612 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000613 return error;
614}
615
616/**
617 * init_dinode - Fill in a new dinode structure
618 * @dip: the directory this inode is being created in
619 * @gl: The glock covering the new inode
620 * @inum: the inode number
621 * @mode: the file permissions
622 * @uid:
623 * @gid:
624 *
625 */
626
627static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
Al Viro629a21e2006-10-13 22:51:24 -0400628 const struct gfs2_inum_host *inum, unsigned int mode,
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400629 unsigned int uid, unsigned int gid,
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400630 const u64 *generation, dev_t dev, struct buffer_head **bhp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000631{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400632 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000633 struct gfs2_dinode *di;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000634 struct buffer_head *dibh;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100635 struct timespec tv = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000636
637 dibh = gfs2_meta_new(gl, inum->no_addr);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000638 gfs2_trans_add_bh(gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000639 gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
640 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000641 di = (struct gfs2_dinode *)dibh->b_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000642
Steven Whitehouse2442a092006-01-30 11:49:32 +0000643 di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
644 di->di_num.no_addr = cpu_to_be64(inum->no_addr);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000645 di->di_mode = cpu_to_be32(mode);
646 di->di_uid = cpu_to_be32(uid);
647 di->di_gid = cpu_to_be32(gid);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500648 di->di_nlink = 0;
649 di->di_size = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000650 di->di_blocks = cpu_to_be64(1);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100651 di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500652 di->di_major = cpu_to_be32(MAJOR(dev));
653 di->di_minor = cpu_to_be32(MINOR(dev));
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000654 di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400655 di->di_generation = cpu_to_be64(*generation);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500656 di->di_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000657
658 if (S_ISREG(mode)) {
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000659 if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
David Teiglandb3b94fa2006-01-16 16:50:04 +0000660 gfs2_tune_get(sdp, gt_new_files_jdata))
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000661 di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000662 } else if (S_ISDIR(mode)) {
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000663 di->di_flags |= cpu_to_be32(dip->i_diskflags &
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500664 GFS2_DIF_INHERIT_JDATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000665 }
666
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000667 di->__pad1 = 0;
Steven Whitehousea9583c72006-11-01 20:09:14 -0500668 di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500669 di->di_height = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000670 di->__pad2 = 0;
671 di->__pad3 = 0;
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500672 di->di_depth = 0;
673 di->di_entries = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000674 memset(&di->__pad4, 0, sizeof(di->__pad4));
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500675 di->di_eattr = 0;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100676 di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
677 di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
678 di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000679 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400680
681 set_buffer_uptodate(dibh);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000682
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400683 *bhp = dibh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000684}
685
686static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
Al Viro629a21e2006-10-13 22:51:24 -0400687 unsigned int mode, const struct gfs2_inum_host *inum,
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400688 const u64 *generation, dev_t dev, struct buffer_head **bhp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000689{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400690 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000691 unsigned int uid, gid;
692 int error;
693
694 munge_mode_uid_gid(dip, &mode, &uid, &gid);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300695 if (!gfs2_alloc_get(dip))
696 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000697
698 error = gfs2_quota_lock(dip, uid, gid);
699 if (error)
700 goto out;
701
702 error = gfs2_quota_check(dip, uid, gid);
703 if (error)
704 goto out_quota;
705
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400706 error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000707 if (error)
708 goto out_quota;
709
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400710 init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, bhp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000711 gfs2_quota_change(dip, +1, uid, gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000712 gfs2_trans_end(sdp);
713
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400714out_quota:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000715 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400716out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000717 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000718 return error;
719}
720
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400721static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
722 struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000723{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400724 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000725 struct gfs2_alloc *al;
726 int alloc_required;
727 struct buffer_head *dibh;
728 int error;
729
730 al = gfs2_alloc_get(dip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300731 if (!al)
732 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000733
734 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
735 if (error)
736 goto fail;
737
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400738 error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
Steven Whitehousec7526662006-03-20 12:30:04 -0500739 if (alloc_required < 0)
Bob Peterson1b8177e2008-01-19 21:50:24 -0600740 goto fail_quota_locks;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000741 if (alloc_required) {
Steven Whitehouse2933f922006-11-01 13:23:29 -0500742 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000743 if (error)
744 goto fail_quota_locks;
745
746 al->al_requested = sdp->sd_max_dirres;
747
748 error = gfs2_inplace_reserve(dip);
749 if (error)
750 goto fail_quota_locks;
751
Steven Whitehouse320dd102006-05-18 16:25:27 -0400752 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100753 al->al_rgd->rd_length +
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400754 2 * RES_DINODE +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000755 RES_STATFS + RES_QUOTA, 0);
756 if (error)
757 goto fail_ipreserv;
758 } else {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400759 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000760 if (error)
761 goto fail_quota_locks;
762 }
763
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100764 error = gfs2_dir_add(&dip->i_inode, name, ip, IF2DT(ip->i_inode.i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000765 if (error)
766 goto fail_end_trans;
767
768 error = gfs2_meta_inode_buffer(ip, &dibh);
769 if (error)
770 goto fail_end_trans;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500771 ip->i_inode.i_nlink = 1;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000772 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500773 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000774 brelse(dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000775 return 0;
776
Steven Whitehouse320dd102006-05-18 16:25:27 -0400777fail_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000778 gfs2_trans_end(sdp);
779
Steven Whitehouse320dd102006-05-18 16:25:27 -0400780fail_ipreserv:
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000781 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000782 gfs2_inplace_release(dip);
783
Steven Whitehouse320dd102006-05-18 16:25:27 -0400784fail_quota_locks:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000785 gfs2_quota_unlock(dip);
786
Steven Whitehouse320dd102006-05-18 16:25:27 -0400787fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000788 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000789 return error;
790}
791
Eric Paris2a7dba32011-02-01 11:05:39 -0500792static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip,
793 const struct qstr *qstr)
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -0400794{
795 int err;
796 size_t len;
797 void *value;
798 char *name;
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -0400799
Eric Paris2a7dba32011-02-01 11:05:39 -0500800 err = security_inode_init_security(&ip->i_inode, &dip->i_inode, qstr,
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -0400801 &name, &value, &len);
802
803 if (err) {
804 if (err == -EOPNOTSUPP)
805 return 0;
806 return err;
807 }
808
Christoph Hellwig431547b2009-11-13 09:52:56 +0000809 err = __gfs2_xattr_set(&ip->i_inode, name, value, len, 0,
810 GFS2_EATYPE_SECURITY);
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -0400811 kfree(value);
812 kfree(name);
813
814 return err;
815}
816
David Teiglandb3b94fa2006-01-16 16:50:04 +0000817/**
818 * gfs2_createi - Create a new inode
819 * @ghs: An array of two holders
820 * @name: The name of the new file
821 * @mode: the permissions on the new inode
822 *
823 * @ghs[0] is an initialized holder for the directory
824 * @ghs[1] is the holder for the inode lock
825 *
Steven Whitehouse7359a192006-02-13 12:27:43 +0000826 * If the return value is not NULL, the glocks on both the directory and the new
David Teiglandb3b94fa2006-01-16 16:50:04 +0000827 * file are held. A transaction has been started and an inplace reservation
828 * is held, as well.
829 *
Steven Whitehouse7359a192006-02-13 12:27:43 +0000830 * Returns: An inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000831 */
832
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400833struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500834 unsigned int mode, dev_t dev)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000835{
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100836 struct inode *inode = NULL;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500837 struct gfs2_inode *dip = ghs->gh_gl->gl_object;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400838 struct inode *dir = &dip->i_inode;
839 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100840 struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000841 int error;
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400842 u64 generation;
Steven Whitehousef91a0d32007-10-15 16:29:05 +0100843 struct buffer_head *bh = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000844
845 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehouse7359a192006-02-13 12:27:43 +0000846 return ERR_PTR(-ENAMETOOLONG);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000847
David Teiglandb3b94fa2006-01-16 16:50:04 +0000848 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
849 error = gfs2_glock_nq(ghs);
850 if (error)
851 goto fail;
852
853 error = create_ok(dip, name, mode);
854 if (error)
855 goto fail_gunlock;
856
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100857 error = alloc_dinode(dip, &inum.no_addr, &generation);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000858 if (error)
859 goto fail_gunlock;
Steven Whitehouse8d8291a2009-08-27 15:51:07 +0100860 inum.no_formal_ino = generation;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000861
Steven Whitehouse28626e22006-11-22 11:13:21 -0500862 error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
863 LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
864 if (error)
865 goto fail_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000866
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400867 error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000868 if (error)
869 goto fail_gunlock2;
870
Steven Whitehouse8d8291a2009-08-27 15:51:07 +0100871 inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode), inum.no_addr,
Bob Peterson44ad37d2011-03-17 16:19:58 -0400872 inum.no_formal_ino, 0);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400873 if (IS_ERR(inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000874 goto fail_gunlock2;
875
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400876 error = gfs2_inode_refresh(GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000877 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100878 goto fail_gunlock2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000879
Steven Whitehouse479c4272009-10-02 12:00:00 +0100880 error = gfs2_acl_create(dip, inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000881 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100882 goto fail_gunlock2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000883
Eric Paris2a7dba32011-02-01 11:05:39 -0500884 error = gfs2_security_init(dip, GFS2_I(inode), name);
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -0400885 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100886 goto fail_gunlock2;
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -0400887
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400888 error = link_dinode(dip, name, GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000889 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100890 goto fail_gunlock2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000891
Steven Whitehousef91a0d32007-10-15 16:29:05 +0100892 if (bh)
893 brelse(bh);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000894 return inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000895
Steven Whitehouse320dd102006-05-18 16:25:27 -0400896fail_gunlock2:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000897 gfs2_glock_dq_uninit(ghs + 1);
Julien Brunelbd1eb882008-09-01 10:51:22 +0200898 if (inode && !IS_ERR(inode))
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100899 iput(inode);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400900fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000901 gfs2_glock_dq(ghs);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400902fail:
Steven Whitehousef91a0d32007-10-15 16:29:05 +0100903 if (bh)
904 brelse(bh);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000905 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000906}
907
Steven Whitehouse536baf02009-05-22 10:48:59 +0100908static int __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000909{
Christoph Hellwig10257742010-06-04 11:30:02 +0200910 struct inode *inode = &ip->i_inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000911 struct buffer_head *dibh;
912 int error;
913
914 error = gfs2_meta_inode_buffer(ip, &dibh);
Christoph Hellwig10257742010-06-04 11:30:02 +0200915 if (error)
916 return error;
917
Christoph Hellwig10257742010-06-04 11:30:02 +0200918 setattr_copy(inode, attr);
919 mark_inode_dirty(inode);
Christoph Hellwig10257742010-06-04 11:30:02 +0200920 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
921 gfs2_dinode_out(ip, dibh->b_data);
922 brelse(dibh);
923 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000924}
925
926/**
927 * gfs2_setattr_simple -
928 * @ip:
929 * @attr:
930 *
931 * Called with a reference on the vnode.
932 *
933 * Returns: errno
934 */
935
936int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
937{
938 int error;
939
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500940 if (current->journal_info)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000941 return __gfs2_setattr_simple(ip, attr);
942
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400943 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000944 if (error)
945 return error;
946
947 error = __gfs2_setattr_simple(ip, attr);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400948 gfs2_trans_end(GFS2_SB(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000949 return error;
950}
951
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100952void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
953{
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100954 struct gfs2_dinode *str = buf;
955
956 str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
957 str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100958 str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100959 str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
960 str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
961 str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
962 str->di_uid = cpu_to_be32(ip->i_inode.i_uid);
963 str->di_gid = cpu_to_be32(ip->i_inode.i_gid);
964 str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100965 str->di_size = cpu_to_be64(i_size_read(&ip->i_inode));
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000966 str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100967 str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
968 str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
969 str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
970
Steven Whitehousece276b02008-02-06 09:25:45 +0000971 str->di_goal_meta = cpu_to_be64(ip->i_goal);
972 str->di_goal_data = cpu_to_be64(ip->i_goal);
Steven Whitehousebcf0b5b2008-11-03 13:39:46 +0000973 str->di_generation = cpu_to_be64(ip->i_generation);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100974
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000975 str->di_flags = cpu_to_be32(ip->i_diskflags);
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000976 str->di_height = cpu_to_be16(ip->i_height);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100977 str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000978 !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100979 GFS2_FORMAT_DE : 0);
Steven Whitehouse9a004502008-02-01 09:23:44 +0000980 str->di_depth = cpu_to_be16(ip->i_depth);
Steven Whitehousead6203f2008-11-03 13:59:19 +0000981 str->di_entries = cpu_to_be32(ip->i_entries);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100982
Steven Whitehouse3767ac22008-11-03 14:28:42 +0000983 str->di_eattr = cpu_to_be64(ip->i_eattr);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100984 str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
985 str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
986 str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100987}
988
989void gfs2_dinode_print(const struct gfs2_inode *ip)
990{
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100991 printk(KERN_INFO " no_formal_ino = %llu\n",
992 (unsigned long long)ip->i_no_formal_ino);
993 printk(KERN_INFO " no_addr = %llu\n",
994 (unsigned long long)ip->i_no_addr);
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100995 printk(KERN_INFO " i_size = %llu\n",
996 (unsigned long long)i_size_read(&ip->i_inode));
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000997 printk(KERN_INFO " blocks = %llu\n",
998 (unsigned long long)gfs2_get_inode_blocks(&ip->i_inode));
Steven Whitehousece276b02008-02-06 09:25:45 +0000999 printk(KERN_INFO " i_goal = %llu\n",
1000 (unsigned long long)ip->i_goal);
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001001 printk(KERN_INFO " i_diskflags = 0x%.8X\n", ip->i_diskflags);
Bob Petersonca390602008-01-28 11:15:57 -06001002 printk(KERN_INFO " i_height = %u\n", ip->i_height);
Steven Whitehouse9a004502008-02-01 09:23:44 +00001003 printk(KERN_INFO " i_depth = %u\n", ip->i_depth);
Steven Whitehousead6203f2008-11-03 13:59:19 +00001004 printk(KERN_INFO " i_entries = %u\n", ip->i_entries);
Steven Whitehouse3767ac22008-11-03 14:28:42 +00001005 printk(KERN_INFO " i_eattr = %llu\n",
1006 (unsigned long long)ip->i_eattr);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001007}
1008