blob: b1bf2694fb2bd725394de1f5306a0eda1f4a6a66 [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
David Teiglandb3b94fa2006-01-16 16:50:04 +000043static int iget_test(struct inode *inode, void *opaque)
44{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040045 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010046 u64 *no_addr = opaque;
David Teiglandb3b94fa2006-01-16 16:50:04 +000047
Steven Whitehouse009d8512009-12-08 12:12:13 +000048 if (ip->i_no_addr == *no_addr)
David Teiglandb3b94fa2006-01-16 16:50:04 +000049 return 1;
50
51 return 0;
52}
53
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040054static int iget_set(struct inode *inode, void *opaque)
55{
56 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010057 u64 *no_addr = opaque;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040058
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010059 inode->i_ino = (unsigned long)*no_addr;
60 ip->i_no_addr = *no_addr;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040061 return 0;
62}
63
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010064struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr)
David Teiglandb3b94fa2006-01-16 16:50:04 +000065{
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010066 unsigned long hash = (unsigned long)no_addr;
67 return ilookup5(sb, hash, iget_test, &no_addr);
David Teiglandb3b94fa2006-01-16 16:50:04 +000068}
69
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010070static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr)
David Teiglandb3b94fa2006-01-16 16:50:04 +000071{
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010072 unsigned long hash = (unsigned long)no_addr;
73 return iget5_locked(sb, hash, iget_test, iget_set, &no_addr);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040074}
75
Benjamin Marzinski7a9f53b2007-09-18 13:33:18 -050076struct gfs2_skip_data {
77 u64 no_addr;
78 int skipped;
79};
80
81static int iget_skip_test(struct inode *inode, void *opaque)
82{
83 struct gfs2_inode *ip = GFS2_I(inode);
84 struct gfs2_skip_data *data = opaque;
85
Steven Whitehouse009d8512009-12-08 12:12:13 +000086 if (ip->i_no_addr == data->no_addr) {
Benjamin Marzinski7a9f53b2007-09-18 13:33:18 -050087 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)){
88 data->skipped = 1;
89 return 0;
90 }
91 return 1;
92 }
93 return 0;
94}
95
96static int iget_skip_set(struct inode *inode, void *opaque)
97{
98 struct gfs2_inode *ip = GFS2_I(inode);
99 struct gfs2_skip_data *data = opaque;
100
101 if (data->skipped)
102 return 1;
103 inode->i_ino = (unsigned long)(data->no_addr);
104 ip->i_no_addr = data->no_addr;
105 return 0;
106}
107
108static struct inode *gfs2_iget_skip(struct super_block *sb,
109 u64 no_addr)
110{
111 struct gfs2_skip_data data;
112 unsigned long hash = (unsigned long)no_addr;
113
114 data.no_addr = no_addr;
115 data.skipped = 0;
116 return iget5_locked(sb, hash, iget_skip_test, iget_skip_set, &data);
117}
118
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400119/**
Wendy Cheng35dcc522007-06-27 17:07:53 -0400120 * GFS2 lookup code fills in vfs inode contents based on info obtained
121 * from directory entry inside gfs2_inode_lookup(). This has caused issues
122 * with NFS code path since its get_dentry routine doesn't have the relevant
123 * directory entry when gfs2_inode_lookup() is invoked. Part of the code
124 * segment inside gfs2_inode_lookup code needs to get moved around.
125 *
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100126 * Clears I_NEW as well.
Wendy Cheng35dcc522007-06-27 17:07:53 -0400127 **/
128
129void gfs2_set_iop(struct inode *inode)
130{
Wendy Chengc97bfe42007-11-29 17:56:51 -0500131 struct gfs2_sbd *sdp = GFS2_SB(inode);
Wendy Cheng35dcc522007-06-27 17:07:53 -0400132 umode_t mode = inode->i_mode;
133
134 if (S_ISREG(mode)) {
135 inode->i_op = &gfs2_file_iops;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000136 if (gfs2_localflocks(sdp))
Christoph Hellwig10d21982009-04-07 19:42:17 +0200137 inode->i_fop = &gfs2_file_fops_nolock;
Wendy Chengc97bfe42007-11-29 17:56:51 -0500138 else
Christoph Hellwig10d21982009-04-07 19:42:17 +0200139 inode->i_fop = &gfs2_file_fops;
Wendy Cheng35dcc522007-06-27 17:07:53 -0400140 } else if (S_ISDIR(mode)) {
141 inode->i_op = &gfs2_dir_iops;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000142 if (gfs2_localflocks(sdp))
Christoph Hellwig10d21982009-04-07 19:42:17 +0200143 inode->i_fop = &gfs2_dir_fops_nolock;
Wendy Chengc97bfe42007-11-29 17:56:51 -0500144 else
Christoph Hellwig10d21982009-04-07 19:42:17 +0200145 inode->i_fop = &gfs2_dir_fops;
Wendy Cheng35dcc522007-06-27 17:07:53 -0400146 } else if (S_ISLNK(mode)) {
147 inode->i_op = &gfs2_symlink_iops;
148 } else {
Denis Chengd83225d2008-02-26 15:25:03 +0800149 inode->i_op = &gfs2_file_iops;
Denis Cheng43a33c52008-02-26 15:25:04 +0800150 init_special_inode(inode, inode->i_mode, inode->i_rdev);
Wendy Cheng35dcc522007-06-27 17:07:53 -0400151 }
152
153 unlock_new_inode(inode);
154}
155
156/**
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400157 * gfs2_inode_lookup - Lookup an inode
158 * @sb: The super block
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100159 * @no_addr: The inode number
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400160 * @type: The type of the inode
Benjamin Marzinski7a9f53b2007-09-18 13:33:18 -0500161 * @skip_freeing: set this not return an inode if it is currently being freed.
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400162 *
163 * Returns: A VFS inode, or an error
164 */
165
Bob Peterson091806ed2008-04-29 12:35:48 -0500166struct inode *gfs2_inode_lookup(struct super_block *sb,
Wendy Chengbb9bcf02007-06-27 17:07:08 -0400167 unsigned int type,
168 u64 no_addr,
Benjamin Marzinski7a9f53b2007-09-18 13:33:18 -0500169 u64 no_formal_ino, int skip_freeing)
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400170{
Benjamin Marzinski7a9f53b2007-09-18 13:33:18 -0500171 struct inode *inode;
172 struct gfs2_inode *ip;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400173 struct gfs2_glock *io_gl;
174 int error;
175
Benjamin Marzinski7a9f53b2007-09-18 13:33:18 -0500176 if (skip_freeing)
177 inode = gfs2_iget_skip(sb, no_addr);
178 else
179 inode = gfs2_iget(sb, no_addr);
180 ip = GFS2_I(inode);
181
Steven Whitehouse26d83de2006-10-30 16:59:08 -0500182 if (!inode)
183 return ERR_PTR(-ENOBUFS);
184
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400185 if (inode->i_state & I_NEW) {
186 struct gfs2_sbd *sdp = GFS2_SB(inode);
Wendy Chengbb9bcf02007-06-27 17:07:08 -0400187 ip->i_no_formal_ino = no_formal_ino;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400188
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100189 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400190 if (unlikely(error))
191 goto fail;
192 ip->i_gl->gl_object = ip;
193
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100194 error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400195 if (unlikely(error))
196 goto fail_put;
197
Steven Whitehousebfded272006-11-01 16:05:38 -0500198 set_bit(GIF_INVALID, &ip->i_flags);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400199 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
200 if (unlikely(error))
201 goto fail_iopen;
Abhijith Dasd93cfa92007-06-11 08:22:32 +0100202 ip->i_iopen_gh.gh_gl->gl_object = ip;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400203
204 gfs2_glock_put(io_gl);
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100205
Wendy Cheng35dcc522007-06-27 17:07:53 -0400206 if ((type == DT_UNKNOWN) && (no_formal_ino == 0))
207 goto gfs2_nfsbypass;
208
209 inode->i_mode = DT2IF(type);
210
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100211 /*
212 * We must read the inode in order to work out its type in
213 * this case. Note that this doesn't happen often as we normally
214 * know the type beforehand. This code path only occurs during
215 * unlinked inode recovery (where it is safe to do this glock,
216 * which is not true in the general case).
217 */
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100218 if (type == DT_UNKNOWN) {
219 struct gfs2_holder gh;
220 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
221 if (unlikely(error))
222 goto fail_glock;
223 /* Inode is now uptodate */
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100224 gfs2_glock_dq_uninit(&gh);
225 }
226
Wendy Cheng35dcc522007-06-27 17:07:53 -0400227 gfs2_set_iop(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000228 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400229
Wendy Cheng35dcc522007-06-27 17:07:53 -0400230gfs2_nfsbypass:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400231 return inode;
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100232fail_glock:
233 gfs2_glock_dq(&ip->i_iopen_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400234fail_iopen:
235 gfs2_glock_put(io_gl);
236fail_put:
237 ip->i_gl->gl_object = NULL;
238 gfs2_glock_put(ip->i_gl);
239fail:
David Howells69840b02008-02-07 00:15:29 -0800240 iget_failed(inode);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400241 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000242}
243
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500244static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
Steven Whitehouseea744d02006-10-31 15:28:00 -0500245{
Steven Whitehouseea744d02006-10-31 15:28:00 -0500246 const struct gfs2_dinode *str = buf;
Steven Whitehouse719ee342008-09-18 13:53:59 +0100247 struct timespec atime;
Steven Whitehouse9a004502008-02-01 09:23:44 +0000248 u16 height, depth;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500249
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000250 if (unlikely(ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)))
251 goto corrupt;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100252 ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500253 ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500254 ip->i_inode.i_rdev = 0;
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500255 switch (ip->i_inode.i_mode & S_IFMT) {
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500256 case S_IFBLK:
257 case S_IFCHR:
258 ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
259 be32_to_cpu(str->di_minor));
260 break;
261 };
262
Steven Whitehouse2933f922006-11-01 13:23:29 -0500263 ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
264 ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
Steven Whitehouse4f561102006-11-01 14:04:17 -0500265 /*
266 * We will need to review setting the nlink count here in the
267 * light of the forthcoming ro bind mount work. This is a reminder
268 * to do that.
269 */
270 ip->i_inode.i_nlink = be32_to_cpu(str->di_nlink);
Steven Whitehousec9e98882008-11-04 09:47:33 +0000271 ip->i_disksize = be64_to_cpu(str->di_size);
272 i_size_write(&ip->i_inode, ip->i_disksize);
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000273 gfs2_set_inode_blocks(&ip->i_inode, be64_to_cpu(str->di_blocks));
Steven Whitehouse719ee342008-09-18 13:53:59 +0100274 atime.tv_sec = be64_to_cpu(str->di_atime);
275 atime.tv_nsec = be32_to_cpu(str->di_atime_nsec);
276 if (timespec_compare(&ip->i_inode.i_atime, &atime) < 0)
277 ip->i_inode.i_atime = atime;
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -0500278 ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100279 ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec);
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -0500280 ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100281 ip->i_inode.i_ctime.tv_nsec = be32_to_cpu(str->di_ctime_nsec);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500282
Steven Whitehousece276b02008-02-06 09:25:45 +0000283 ip->i_goal = be64_to_cpu(str->di_goal_meta);
Steven Whitehousebcf0b5b2008-11-03 13:39:46 +0000284 ip->i_generation = be64_to_cpu(str->di_generation);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500285
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000286 ip->i_diskflags = be32_to_cpu(str->di_flags);
Steven Whitehouse6b124d82006-11-08 12:51:06 -0500287 gfs2_set_inode_flags(&ip->i_inode);
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000288 height = be16_to_cpu(str->di_height);
289 if (unlikely(height > GFS2_MAX_META_HEIGHT))
290 goto corrupt;
291 ip->i_height = (u8)height;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500292
Steven Whitehouse9a004502008-02-01 09:23:44 +0000293 depth = be16_to_cpu(str->di_depth);
294 if (unlikely(depth > GFS2_DIR_MAX_DEPTH))
295 goto corrupt;
296 ip->i_depth = (u8)depth;
Steven Whitehousead6203f2008-11-03 13:59:19 +0000297 ip->i_entries = be32_to_cpu(str->di_entries);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500298
Steven Whitehouse3767ac22008-11-03 14:28:42 +0000299 ip->i_eattr = be64_to_cpu(str->di_eattr);
Steven Whitehouse55610932007-10-17 08:47:38 +0100300 if (S_ISREG(ip->i_inode.i_mode))
301 gfs2_set_aops(&ip->i_inode);
302
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500303 return 0;
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000304corrupt:
305 if (gfs2_consist_inode(ip))
306 gfs2_dinode_print(ip);
307 return -EIO;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500308}
309
David Teiglandb3b94fa2006-01-16 16:50:04 +0000310/**
311 * gfs2_inode_refresh - Refresh the incore copy of the dinode
312 * @ip: The GFS2 inode
313 *
314 * Returns: errno
315 */
316
317int gfs2_inode_refresh(struct gfs2_inode *ip)
318{
319 struct buffer_head *dibh;
320 int error;
321
322 error = gfs2_meta_inode_buffer(ip, &dibh);
323 if (error)
324 return error;
325
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400326 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000327 brelse(dibh);
328 return -EIO;
329 }
330
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500331 error = gfs2_dinode_in(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000332 brelse(dibh);
Steven Whitehousebfded272006-11-01 16:05:38 -0500333 clear_bit(GIF_INVALID, &ip->i_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000334
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500335 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000336}
337
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400338int gfs2_dinode_dealloc(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000339{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400340 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000341 struct gfs2_alloc *al;
342 struct gfs2_rgrpd *rgd;
343 int error;
344
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000345 if (gfs2_get_inode_blocks(&ip->i_inode) != 1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000346 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500347 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000348 return -EIO;
349 }
350
351 al = gfs2_alloc_get(ip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300352 if (!al)
353 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000354
355 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
356 if (error)
357 goto out;
358
359 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
360 if (error)
361 goto out_qs;
362
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100363 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000364 if (!rgd) {
365 gfs2_consist_inode(ip);
366 error = -EIO;
367 goto out_rindex_relse;
368 }
369
370 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
371 &al->al_rgd_gh);
372 if (error)
373 goto out_rindex_relse;
374
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400375 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000376 if (error)
377 goto out_rg_gunlock;
378
Steven Whitehouse2bcd6102007-11-08 14:25:12 +0000379 set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
380 set_bit(GLF_LFLUSH, &ip->i_gl->gl_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000381
382 gfs2_free_di(rgd, ip);
383
David Teiglandb3b94fa2006-01-16 16:50:04 +0000384 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000385
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400386out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000387 gfs2_glock_dq_uninit(&al->al_rgd_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400388out_rindex_relse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000389 gfs2_glock_dq_uninit(&al->al_ri_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400390out_qs:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000391 gfs2_quota_unhold(ip);
Steven Whitehouse36327522006-04-28 10:46:21 -0400392out:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400393 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000394 return error;
395}
396
397/**
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500398 * gfs2_change_nlink - Change nlink count on inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000399 * @ip: The GFS2 inode
400 * @diff: The change in the nlink count required
401 *
402 * Returns: errno
403 */
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500404int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000405{
406 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400407 u32 nlink;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000408 int error;
409
Steven Whitehouse4f561102006-11-01 14:04:17 -0500410 BUG_ON(diff != 1 && diff != -1);
411 nlink = ip->i_inode.i_nlink + diff;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000412
413 /* If we are reducing the nlink count, but the new value ends up being
414 bigger than the old one, we must have underflowed. */
Steven Whitehouse4f561102006-11-01 14:04:17 -0500415 if (diff < 0 && nlink > ip->i_inode.i_nlink) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000416 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500417 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000418 return -EIO;
419 }
420
421 error = gfs2_meta_inode_buffer(ip, &dibh);
422 if (error)
423 return error;
424
Steven Whitehouse4f561102006-11-01 14:04:17 -0500425 if (diff > 0)
426 inc_nlink(&ip->i_inode);
427 else
428 drop_nlink(&ip->i_inode);
429
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100430 ip->i_inode.i_ctime = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000431
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000432 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500433 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000434 brelse(dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400435 mark_inode_dirty(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000436
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500437 if (ip->i_inode.i_nlink == 0)
Russell Cattelanddee7602007-01-29 17:13:44 -0600438 gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500439
S. Wendy Cheng55098262007-01-18 15:56:34 -0500440 return error;
441}
442
Steven Whitehousec7526662006-03-20 12:30:04 -0500443struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
444{
445 struct qstr qstr;
Russell Cattelan6c93fd12007-01-08 17:47:51 -0600446 struct inode *inode;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500447 gfs2_str2qstr(&qstr, name);
Al Viroa569c712008-07-23 14:42:05 -0400448 inode = gfs2_lookupi(dip, &qstr, 1);
Russell Cattelan6c93fd12007-01-08 17:47:51 -0600449 /* gfs2_lookupi has inconsistent callers: vfs
450 * related routines expect NULL for no entry found,
451 * gfs2_lookup_simple callers expect ENOENT
452 * and do not check for NULL.
453 */
454 if (inode == NULL)
455 return ERR_PTR(-ENOENT);
456 else
457 return inode;
Steven Whitehousec7526662006-03-20 12:30:04 -0500458}
459
460
David Teiglandb3b94fa2006-01-16 16:50:04 +0000461/**
462 * gfs2_lookupi - Look up a filename in a directory and return its inode
463 * @d_gh: An initialized holder for the directory glock
464 * @name: The name of the inode to look for
465 * @is_root: If 1, ignore the caller's permissions
466 * @i_gh: An uninitialized holder for the new inode glock
467 *
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000468 * This can be called via the VFS filldir function when NFS is doing
469 * a readdirplus and the inode which its intending to stat isn't
470 * already in cache. In this case we must not take the directory glock
471 * again, since the readdir call will have already taken that lock.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000472 *
473 * Returns: errno
474 */
475
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400476struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
Al Viroa569c712008-07-23 14:42:05 -0400477 int is_root)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000478{
Steven Whitehousec9fd4302006-03-01 15:31:02 -0500479 struct super_block *sb = dir->i_sb;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400480 struct gfs2_inode *dip = GFS2_I(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000481 struct gfs2_holder d_gh;
akpm@linux-foundation.org037bcbb2007-06-08 16:42:14 -0700482 int error = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500483 struct inode *inode = NULL;
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000484 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000485
486 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehousec7526662006-03-20 12:30:04 -0500487 return ERR_PTR(-ENAMETOOLONG);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000488
Steven Whitehousec7526662006-03-20 12:30:04 -0500489 if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
490 (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
491 dir == sb->s_root->d_inode)) {
Steven Whitehouse320dd102006-05-18 16:25:27 -0400492 igrab(dir);
493 return dir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000494 }
495
Steven Whitehouse7afd88d2008-02-22 16:07:18 +0000496 if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000497 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
498 if (error)
499 return ERR_PTR(error);
500 unlock = 1;
501 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000502
503 if (!is_root) {
Miklos Szeredif58ba882008-07-02 21:12:01 +0200504 error = gfs2_permission(dir, MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000505 if (error)
506 goto out;
507 }
508
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100509 inode = gfs2_dir_search(dir, name);
510 if (IS_ERR(inode))
511 error = PTR_ERR(inode);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000512out:
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000513 if (unlock)
514 gfs2_glock_dq_uninit(&d_gh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500515 if (error == -ENOENT)
516 return NULL;
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000517 return inode ? inode : ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000518}
519
David Teiglandb3b94fa2006-01-16 16:50:04 +0000520/**
521 * create_ok - OK to create a new on-disk inode here?
522 * @dip: Directory in which dinode is to be created
523 * @name: Name of new dinode
524 * @mode:
525 *
526 * Returns: errno
527 */
528
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400529static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000530 unsigned int mode)
531{
532 int error;
533
Miklos Szeredif58ba882008-07-02 21:12:01 +0200534 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000535 if (error)
536 return error;
537
538 /* Don't create entries in an unlinked directory */
Steven Whitehouse4f561102006-11-01 14:04:17 -0500539 if (!dip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000540 return -EPERM;
541
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100542 error = gfs2_dir_check(&dip->i_inode, name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000543 switch (error) {
544 case -ENOENT:
545 error = 0;
546 break;
547 case 0:
548 return -EEXIST;
549 default:
550 return error;
551 }
552
Steven Whitehousead6203f2008-11-03 13:59:19 +0000553 if (dip->i_entries == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000554 return -EFBIG;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500555 if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000556 return -EMLINK;
557
558 return 0;
559}
560
561static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
562 unsigned int *uid, unsigned int *gid)
563{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400564 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
Steven Whitehouse2933f922006-11-01 13:23:29 -0500565 (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000566 if (S_ISDIR(*mode))
567 *mode |= S_ISUID;
David Howells3de7be32008-11-14 10:38:53 +1100568 else if (dip->i_inode.i_uid != current_fsuid())
David Teiglandb3b94fa2006-01-16 16:50:04 +0000569 *mode &= ~07111;
Steven Whitehouse2933f922006-11-01 13:23:29 -0500570 *uid = dip->i_inode.i_uid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000571 } else
David Howells3de7be32008-11-14 10:38:53 +1100572 *uid = current_fsuid();
David Teiglandb3b94fa2006-01-16 16:50:04 +0000573
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500574 if (dip->i_inode.i_mode & S_ISGID) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000575 if (S_ISDIR(*mode))
576 *mode |= S_ISGID;
Steven Whitehouse2933f922006-11-01 13:23:29 -0500577 *gid = dip->i_inode.i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000578 } else
David Howells3de7be32008-11-14 10:38:53 +1100579 *gid = current_fsgid();
David Teiglandb3b94fa2006-01-16 16:50:04 +0000580}
581
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100582static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000583{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400584 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000585 int error;
586
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000587 if (gfs2_alloc_get(dip) == NULL)
588 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000589
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000590 dip->i_alloc->al_requested = RES_DINODE;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000591 error = gfs2_inplace_reserve(dip);
592 if (error)
593 goto out;
594
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400595 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000596 if (error)
597 goto out_ipreserv;
598
Steven Whitehouse6050b9c2009-07-31 16:19:40 +0100599 error = gfs2_alloc_di(dip, no_addr, generation);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000600
601 gfs2_trans_end(sdp);
602
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400603out_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000604 gfs2_inplace_release(dip);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400605out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000606 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000607 return error;
608}
609
610/**
611 * init_dinode - Fill in a new dinode structure
612 * @dip: the directory this inode is being created in
613 * @gl: The glock covering the new inode
614 * @inum: the inode number
615 * @mode: the file permissions
616 * @uid:
617 * @gid:
618 *
619 */
620
621static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
Al Viro629a21e2006-10-13 22:51:24 -0400622 const struct gfs2_inum_host *inum, unsigned int mode,
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400623 unsigned int uid, unsigned int gid,
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400624 const u64 *generation, dev_t dev, struct buffer_head **bhp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000625{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400626 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000627 struct gfs2_dinode *di;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000628 struct buffer_head *dibh;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100629 struct timespec tv = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000630
631 dibh = gfs2_meta_new(gl, inum->no_addr);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000632 gfs2_trans_add_bh(gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000633 gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
634 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000635 di = (struct gfs2_dinode *)dibh->b_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000636
Steven Whitehouse2442a092006-01-30 11:49:32 +0000637 di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
638 di->di_num.no_addr = cpu_to_be64(inum->no_addr);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000639 di->di_mode = cpu_to_be32(mode);
640 di->di_uid = cpu_to_be32(uid);
641 di->di_gid = cpu_to_be32(gid);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500642 di->di_nlink = 0;
643 di->di_size = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000644 di->di_blocks = cpu_to_be64(1);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100645 di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500646 di->di_major = cpu_to_be32(MAJOR(dev));
647 di->di_minor = cpu_to_be32(MINOR(dev));
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000648 di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400649 di->di_generation = cpu_to_be64(*generation);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500650 di->di_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000651
652 if (S_ISREG(mode)) {
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000653 if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
David Teiglandb3b94fa2006-01-16 16:50:04 +0000654 gfs2_tune_get(sdp, gt_new_files_jdata))
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000655 di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000656 } else if (S_ISDIR(mode)) {
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000657 di->di_flags |= cpu_to_be32(dip->i_diskflags &
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500658 GFS2_DIF_INHERIT_JDATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000659 }
660
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000661 di->__pad1 = 0;
Steven Whitehousea9583c72006-11-01 20:09:14 -0500662 di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500663 di->di_height = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000664 di->__pad2 = 0;
665 di->__pad3 = 0;
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500666 di->di_depth = 0;
667 di->di_entries = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000668 memset(&di->__pad4, 0, sizeof(di->__pad4));
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500669 di->di_eattr = 0;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100670 di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
671 di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
672 di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000673 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400674
675 set_buffer_uptodate(dibh);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000676
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400677 *bhp = dibh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000678}
679
680static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
Al Viro629a21e2006-10-13 22:51:24 -0400681 unsigned int mode, const struct gfs2_inum_host *inum,
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400682 const u64 *generation, dev_t dev, struct buffer_head **bhp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000683{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400684 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000685 unsigned int uid, gid;
686 int error;
687
688 munge_mode_uid_gid(dip, &mode, &uid, &gid);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300689 if (!gfs2_alloc_get(dip))
690 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000691
692 error = gfs2_quota_lock(dip, uid, gid);
693 if (error)
694 goto out;
695
696 error = gfs2_quota_check(dip, uid, gid);
697 if (error)
698 goto out_quota;
699
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400700 error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000701 if (error)
702 goto out_quota;
703
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400704 init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, bhp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000705 gfs2_quota_change(dip, +1, uid, gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000706 gfs2_trans_end(sdp);
707
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400708out_quota:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000709 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400710out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000711 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000712 return error;
713}
714
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400715static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
716 struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000717{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400718 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000719 struct gfs2_alloc *al;
720 int alloc_required;
721 struct buffer_head *dibh;
722 int error;
723
724 al = gfs2_alloc_get(dip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300725 if (!al)
726 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000727
728 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
729 if (error)
730 goto fail;
731
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400732 error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
Steven Whitehousec7526662006-03-20 12:30:04 -0500733 if (alloc_required < 0)
Bob Peterson1b8177e2008-01-19 21:50:24 -0600734 goto fail_quota_locks;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000735 if (alloc_required) {
Steven Whitehouse2933f922006-11-01 13:23:29 -0500736 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000737 if (error)
738 goto fail_quota_locks;
739
740 al->al_requested = sdp->sd_max_dirres;
741
742 error = gfs2_inplace_reserve(dip);
743 if (error)
744 goto fail_quota_locks;
745
Steven Whitehouse320dd102006-05-18 16:25:27 -0400746 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100747 al->al_rgd->rd_length +
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400748 2 * RES_DINODE +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000749 RES_STATFS + RES_QUOTA, 0);
750 if (error)
751 goto fail_ipreserv;
752 } else {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400753 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000754 if (error)
755 goto fail_quota_locks;
756 }
757
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100758 error = gfs2_dir_add(&dip->i_inode, name, ip, IF2DT(ip->i_inode.i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000759 if (error)
760 goto fail_end_trans;
761
762 error = gfs2_meta_inode_buffer(ip, &dibh);
763 if (error)
764 goto fail_end_trans;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500765 ip->i_inode.i_nlink = 1;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000766 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500767 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000768 brelse(dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000769 return 0;
770
Steven Whitehouse320dd102006-05-18 16:25:27 -0400771fail_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000772 gfs2_trans_end(sdp);
773
Steven Whitehouse320dd102006-05-18 16:25:27 -0400774fail_ipreserv:
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000775 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000776 gfs2_inplace_release(dip);
777
Steven Whitehouse320dd102006-05-18 16:25:27 -0400778fail_quota_locks:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000779 gfs2_quota_unlock(dip);
780
Steven Whitehouse320dd102006-05-18 16:25:27 -0400781fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000782 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000783 return error;
784}
785
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -0400786static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip)
787{
788 int err;
789 size_t len;
790 void *value;
791 char *name;
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -0400792
793 err = security_inode_init_security(&ip->i_inode, &dip->i_inode,
794 &name, &value, &len);
795
796 if (err) {
797 if (err == -EOPNOTSUPP)
798 return 0;
799 return err;
800 }
801
Christoph Hellwig431547b2009-11-13 09:52:56 +0000802 err = __gfs2_xattr_set(&ip->i_inode, name, value, len, 0,
803 GFS2_EATYPE_SECURITY);
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -0400804 kfree(value);
805 kfree(name);
806
807 return err;
808}
809
David Teiglandb3b94fa2006-01-16 16:50:04 +0000810/**
811 * gfs2_createi - Create a new inode
812 * @ghs: An array of two holders
813 * @name: The name of the new file
814 * @mode: the permissions on the new inode
815 *
816 * @ghs[0] is an initialized holder for the directory
817 * @ghs[1] is the holder for the inode lock
818 *
Steven Whitehouse7359a192006-02-13 12:27:43 +0000819 * If the return value is not NULL, the glocks on both the directory and the new
David Teiglandb3b94fa2006-01-16 16:50:04 +0000820 * file are held. A transaction has been started and an inplace reservation
821 * is held, as well.
822 *
Steven Whitehouse7359a192006-02-13 12:27:43 +0000823 * Returns: An inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000824 */
825
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400826struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500827 unsigned int mode, dev_t dev)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000828{
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100829 struct inode *inode = NULL;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500830 struct gfs2_inode *dip = ghs->gh_gl->gl_object;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400831 struct inode *dir = &dip->i_inode;
832 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100833 struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000834 int error;
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400835 u64 generation;
Steven Whitehousef91a0d32007-10-15 16:29:05 +0100836 struct buffer_head *bh = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000837
838 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehouse7359a192006-02-13 12:27:43 +0000839 return ERR_PTR(-ENAMETOOLONG);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000840
David Teiglandb3b94fa2006-01-16 16:50:04 +0000841 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
842 error = gfs2_glock_nq(ghs);
843 if (error)
844 goto fail;
845
846 error = create_ok(dip, name, mode);
847 if (error)
848 goto fail_gunlock;
849
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100850 error = alloc_dinode(dip, &inum.no_addr, &generation);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000851 if (error)
852 goto fail_gunlock;
Steven Whitehouse8d8291a2009-08-27 15:51:07 +0100853 inum.no_formal_ino = generation;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000854
Steven Whitehouse28626e22006-11-22 11:13:21 -0500855 error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
856 LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
857 if (error)
858 goto fail_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000859
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400860 error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000861 if (error)
862 goto fail_gunlock2;
863
Steven Whitehouse8d8291a2009-08-27 15:51:07 +0100864 inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode), inum.no_addr,
865 inum.no_formal_ino, 0);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400866 if (IS_ERR(inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000867 goto fail_gunlock2;
868
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400869 error = gfs2_inode_refresh(GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000870 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100871 goto fail_gunlock2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000872
Steven Whitehouse479c4272009-10-02 12:00:00 +0100873 error = gfs2_acl_create(dip, inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000874 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100875 goto fail_gunlock2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000876
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -0400877 error = gfs2_security_init(dip, GFS2_I(inode));
878 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100879 goto fail_gunlock2;
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -0400880
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400881 error = link_dinode(dip, name, GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000882 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100883 goto fail_gunlock2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000884
Steven Whitehousef91a0d32007-10-15 16:29:05 +0100885 if (bh)
886 brelse(bh);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000887 return inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000888
Steven Whitehouse320dd102006-05-18 16:25:27 -0400889fail_gunlock2:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000890 gfs2_glock_dq_uninit(ghs + 1);
Julien Brunelbd1eb882008-09-01 10:51:22 +0200891 if (inode && !IS_ERR(inode))
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100892 iput(inode);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400893fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000894 gfs2_glock_dq(ghs);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400895fail:
Steven Whitehousef91a0d32007-10-15 16:29:05 +0100896 if (bh)
897 brelse(bh);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000898 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000899}
900
Steven Whitehouse536baf02009-05-22 10:48:59 +0100901static int __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000902{
903 struct buffer_head *dibh;
904 int error;
905
906 error = gfs2_meta_inode_buffer(ip, &dibh);
907 if (!error) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400908 error = inode_setattr(&ip->i_inode, attr);
909 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000910 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500911 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000912 brelse(dibh);
913 }
914 return error;
915}
916
917/**
918 * gfs2_setattr_simple -
919 * @ip:
920 * @attr:
921 *
922 * Called with a reference on the vnode.
923 *
924 * Returns: errno
925 */
926
927int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
928{
929 int error;
930
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500931 if (current->journal_info)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000932 return __gfs2_setattr_simple(ip, attr);
933
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400934 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000935 if (error)
936 return error;
937
938 error = __gfs2_setattr_simple(ip, attr);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400939 gfs2_trans_end(GFS2_SB(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000940 return error;
941}
942
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100943void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
944{
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100945 struct gfs2_dinode *str = buf;
946
947 str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
948 str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100949 str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100950 str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
951 str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
952 str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
953 str->di_uid = cpu_to_be32(ip->i_inode.i_uid);
954 str->di_gid = cpu_to_be32(ip->i_inode.i_gid);
955 str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
Steven Whitehousec9e98882008-11-04 09:47:33 +0000956 str->di_size = cpu_to_be64(ip->i_disksize);
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000957 str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100958 str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
959 str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
960 str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
961
Steven Whitehousece276b02008-02-06 09:25:45 +0000962 str->di_goal_meta = cpu_to_be64(ip->i_goal);
963 str->di_goal_data = cpu_to_be64(ip->i_goal);
Steven Whitehousebcf0b5b2008-11-03 13:39:46 +0000964 str->di_generation = cpu_to_be64(ip->i_generation);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100965
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000966 str->di_flags = cpu_to_be32(ip->i_diskflags);
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000967 str->di_height = cpu_to_be16(ip->i_height);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100968 str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000969 !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100970 GFS2_FORMAT_DE : 0);
Steven Whitehouse9a004502008-02-01 09:23:44 +0000971 str->di_depth = cpu_to_be16(ip->i_depth);
Steven Whitehousead6203f2008-11-03 13:59:19 +0000972 str->di_entries = cpu_to_be32(ip->i_entries);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100973
Steven Whitehouse3767ac22008-11-03 14:28:42 +0000974 str->di_eattr = cpu_to_be64(ip->i_eattr);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100975 str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
976 str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
977 str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100978}
979
980void gfs2_dinode_print(const struct gfs2_inode *ip)
981{
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100982 printk(KERN_INFO " no_formal_ino = %llu\n",
983 (unsigned long long)ip->i_no_formal_ino);
984 printk(KERN_INFO " no_addr = %llu\n",
985 (unsigned long long)ip->i_no_addr);
Steven Whitehousec9e98882008-11-04 09:47:33 +0000986 printk(KERN_INFO " i_disksize = %llu\n",
987 (unsigned long long)ip->i_disksize);
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000988 printk(KERN_INFO " blocks = %llu\n",
989 (unsigned long long)gfs2_get_inode_blocks(&ip->i_inode));
Steven Whitehousece276b02008-02-06 09:25:45 +0000990 printk(KERN_INFO " i_goal = %llu\n",
991 (unsigned long long)ip->i_goal);
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000992 printk(KERN_INFO " i_diskflags = 0x%.8X\n", ip->i_diskflags);
Bob Petersonca390602008-01-28 11:15:57 -0600993 printk(KERN_INFO " i_height = %u\n", ip->i_height);
Steven Whitehouse9a004502008-02-01 09:23:44 +0000994 printk(KERN_INFO " i_depth = %u\n", ip->i_depth);
Steven Whitehousead6203f2008-11-03 13:59:19 +0000995 printk(KERN_INFO " i_entries = %u\n", ip->i_entries);
Steven Whitehouse3767ac22008-11-03 14:28:42 +0000996 printk(KERN_INFO " i_eattr = %llu\n",
997 (unsigned long long)ip->i_eattr);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100998}
999