blob: 015d4c007086dae46ecef6b51970dd9e3604c643 [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>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020019#include <linux/lm_interface.h>
Ryan O'Harafcb47e02006-10-03 11:57:35 -040020#include <linux/security.h>
Steven Whitehouse719ee342008-09-18 13:53:59 +010021#include <linux/time.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000022
23#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050024#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000025#include "acl.h"
26#include "bmap.h"
27#include "dir.h"
28#include "eattr.h"
29#include "glock.h"
30#include "glops.h"
31#include "inode.h"
32#include "log.h"
33#include "meta_io.h"
34#include "ops_address.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000035#include "quota.h"
36#include "rgrp.h"
37#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050038#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000039
Steven Whitehousebb8d8a62007-06-01 14:11:58 +010040struct gfs2_inum_range_host {
41 u64 ir_start;
42 u64 ir_length;
43};
44
David Teiglandb3b94fa2006-01-16 16:50:04 +000045static int iget_test(struct inode *inode, void *opaque)
46{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040047 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010048 u64 *no_addr = opaque;
David Teiglandb3b94fa2006-01-16 16:50:04 +000049
Bob Peterson091806e2008-04-29 12:35:48 -050050 if (ip->i_no_addr == *no_addr && test_bit(GIF_USER, &ip->i_flags))
David Teiglandb3b94fa2006-01-16 16:50:04 +000051 return 1;
52
53 return 0;
54}
55
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040056static int iget_set(struct inode *inode, void *opaque)
57{
58 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010059 u64 *no_addr = opaque;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040060
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010061 inode->i_ino = (unsigned long)*no_addr;
62 ip->i_no_addr = *no_addr;
Bob Peterson091806e2008-04-29 12:35:48 -050063 set_bit(GIF_USER, &ip->i_flags);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040064 return 0;
65}
66
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010067struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr)
David Teiglandb3b94fa2006-01-16 16:50:04 +000068{
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010069 unsigned long hash = (unsigned long)no_addr;
70 return ilookup5(sb, hash, iget_test, &no_addr);
David Teiglandb3b94fa2006-01-16 16:50:04 +000071}
72
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010073static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr)
David Teiglandb3b94fa2006-01-16 16:50:04 +000074{
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010075 unsigned long hash = (unsigned long)no_addr;
76 return iget5_locked(sb, hash, iget_test, iget_set, &no_addr);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040077}
78
Benjamin Marzinski7a9f53b2007-09-18 13:33:18 -050079struct gfs2_skip_data {
80 u64 no_addr;
81 int skipped;
82};
83
84static int iget_skip_test(struct inode *inode, void *opaque)
85{
86 struct gfs2_inode *ip = GFS2_I(inode);
87 struct gfs2_skip_data *data = opaque;
88
Bob Peterson091806e2008-04-29 12:35:48 -050089 if (ip->i_no_addr == data->no_addr && test_bit(GIF_USER, &ip->i_flags)){
Benjamin Marzinski7a9f53b2007-09-18 13:33:18 -050090 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)){
91 data->skipped = 1;
92 return 0;
93 }
94 return 1;
95 }
96 return 0;
97}
98
99static int iget_skip_set(struct inode *inode, void *opaque)
100{
101 struct gfs2_inode *ip = GFS2_I(inode);
102 struct gfs2_skip_data *data = opaque;
103
104 if (data->skipped)
105 return 1;
106 inode->i_ino = (unsigned long)(data->no_addr);
107 ip->i_no_addr = data->no_addr;
Bob Peterson091806e2008-04-29 12:35:48 -0500108 set_bit(GIF_USER, &ip->i_flags);
Benjamin Marzinski7a9f53b2007-09-18 13:33:18 -0500109 return 0;
110}
111
112static struct inode *gfs2_iget_skip(struct super_block *sb,
113 u64 no_addr)
114{
115 struct gfs2_skip_data data;
116 unsigned long hash = (unsigned long)no_addr;
117
118 data.no_addr = no_addr;
119 data.skipped = 0;
120 return iget5_locked(sb, hash, iget_skip_test, iget_skip_set, &data);
121}
122
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400123/**
Wendy Cheng35dcc522007-06-27 17:07:53 -0400124 * GFS2 lookup code fills in vfs inode contents based on info obtained
125 * from directory entry inside gfs2_inode_lookup(). This has caused issues
126 * with NFS code path since its get_dentry routine doesn't have the relevant
127 * directory entry when gfs2_inode_lookup() is invoked. Part of the code
128 * segment inside gfs2_inode_lookup code needs to get moved around.
129 *
130 * Clean up I_LOCK and I_NEW as well.
131 **/
132
133void gfs2_set_iop(struct inode *inode)
134{
Wendy Chengc97bfe42007-11-29 17:56:51 -0500135 struct gfs2_sbd *sdp = GFS2_SB(inode);
Wendy Cheng35dcc522007-06-27 17:07:53 -0400136 umode_t mode = inode->i_mode;
137
138 if (S_ISREG(mode)) {
139 inode->i_op = &gfs2_file_iops;
Wendy Chengc97bfe42007-11-29 17:56:51 -0500140 if (sdp->sd_args.ar_localflocks)
141 inode->i_fop = &gfs2_file_fops_nolock;
142 else
143 inode->i_fop = &gfs2_file_fops;
Wendy Cheng35dcc522007-06-27 17:07:53 -0400144 } else if (S_ISDIR(mode)) {
145 inode->i_op = &gfs2_dir_iops;
Wendy Chengc97bfe42007-11-29 17:56:51 -0500146 if (sdp->sd_args.ar_localflocks)
147 inode->i_fop = &gfs2_dir_fops_nolock;
148 else
149 inode->i_fop = &gfs2_dir_fops;
Wendy Cheng35dcc522007-06-27 17:07:53 -0400150 } else if (S_ISLNK(mode)) {
151 inode->i_op = &gfs2_symlink_iops;
152 } else {
Denis Chengd83225d2008-02-26 15:25:03 +0800153 inode->i_op = &gfs2_file_iops;
Denis Cheng43a33c52008-02-26 15:25:04 +0800154 init_special_inode(inode, inode->i_mode, inode->i_rdev);
Wendy Cheng35dcc522007-06-27 17:07:53 -0400155 }
156
157 unlock_new_inode(inode);
158}
159
160/**
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400161 * gfs2_inode_lookup - Lookup an inode
162 * @sb: The super block
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100163 * @no_addr: The inode number
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400164 * @type: The type of the inode
Benjamin Marzinski7a9f53b2007-09-18 13:33:18 -0500165 * @skip_freeing: set this not return an inode if it is currently being freed.
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400166 *
167 * Returns: A VFS inode, or an error
168 */
169
Bob Peterson091806e2008-04-29 12:35:48 -0500170struct inode *gfs2_inode_lookup(struct super_block *sb,
Wendy Chengbb9bcf02007-06-27 17:07:08 -0400171 unsigned int type,
172 u64 no_addr,
Benjamin Marzinski7a9f53b2007-09-18 13:33:18 -0500173 u64 no_formal_ino, int skip_freeing)
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400174{
Benjamin Marzinski7a9f53b2007-09-18 13:33:18 -0500175 struct inode *inode;
176 struct gfs2_inode *ip;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400177 struct gfs2_glock *io_gl;
178 int error;
179
Benjamin Marzinski7a9f53b2007-09-18 13:33:18 -0500180 if (skip_freeing)
181 inode = gfs2_iget_skip(sb, no_addr);
182 else
183 inode = gfs2_iget(sb, no_addr);
184 ip = GFS2_I(inode);
185
Steven Whitehouse26d83de2006-10-30 16:59:08 -0500186 if (!inode)
187 return ERR_PTR(-ENOBUFS);
188
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400189 if (inode->i_state & I_NEW) {
190 struct gfs2_sbd *sdp = GFS2_SB(inode);
Wendy Chengbb9bcf02007-06-27 17:07:08 -0400191 ip->i_no_formal_ino = no_formal_ino;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400192
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100193 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400194 if (unlikely(error))
195 goto fail;
196 ip->i_gl->gl_object = ip;
197
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100198 error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400199 if (unlikely(error))
200 goto fail_put;
201
Steven Whitehousebfded272006-11-01 16:05:38 -0500202 set_bit(GIF_INVALID, &ip->i_flags);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400203 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
204 if (unlikely(error))
205 goto fail_iopen;
Abhijith Dasd93cfa92007-06-11 08:22:32 +0100206 ip->i_iopen_gh.gh_gl->gl_object = ip;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400207
208 gfs2_glock_put(io_gl);
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100209
Wendy Cheng35dcc522007-06-27 17:07:53 -0400210 if ((type == DT_UNKNOWN) && (no_formal_ino == 0))
211 goto gfs2_nfsbypass;
212
213 inode->i_mode = DT2IF(type);
214
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100215 /*
216 * We must read the inode in order to work out its type in
217 * this case. Note that this doesn't happen often as we normally
218 * know the type beforehand. This code path only occurs during
219 * unlinked inode recovery (where it is safe to do this glock,
220 * which is not true in the general case).
221 */
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100222 if (type == DT_UNKNOWN) {
223 struct gfs2_holder gh;
224 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
225 if (unlikely(error))
226 goto fail_glock;
227 /* Inode is now uptodate */
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100228 gfs2_glock_dq_uninit(&gh);
229 }
230
Wendy Cheng35dcc522007-06-27 17:07:53 -0400231 gfs2_set_iop(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000232 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400233
Wendy Cheng35dcc522007-06-27 17:07:53 -0400234gfs2_nfsbypass:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400235 return inode;
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100236fail_glock:
237 gfs2_glock_dq(&ip->i_iopen_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400238fail_iopen:
239 gfs2_glock_put(io_gl);
240fail_put:
241 ip->i_gl->gl_object = NULL;
242 gfs2_glock_put(ip->i_gl);
243fail:
David Howells69840b02008-02-07 00:15:29 -0800244 iget_failed(inode);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400245 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000246}
247
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500248static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
Steven Whitehouseea744d02006-10-31 15:28:00 -0500249{
250 struct gfs2_dinode_host *di = &ip->i_di;
251 const struct gfs2_dinode *str = buf;
Steven Whitehouse719ee342008-09-18 13:53:59 +0100252 struct timespec atime;
Steven Whitehouse9a004502008-02-01 09:23:44 +0000253 u16 height, depth;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500254
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000255 if (unlikely(ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)))
256 goto corrupt;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100257 ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500258 ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500259 ip->i_inode.i_rdev = 0;
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500260 switch (ip->i_inode.i_mode & S_IFMT) {
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500261 case S_IFBLK:
262 case S_IFCHR:
263 ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
264 be32_to_cpu(str->di_minor));
265 break;
266 };
267
Steven Whitehouse2933f922006-11-01 13:23:29 -0500268 ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
269 ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
Steven Whitehouse4f561102006-11-01 14:04:17 -0500270 /*
271 * We will need to review setting the nlink count here in the
272 * light of the forthcoming ro bind mount work. This is a reminder
273 * to do that.
274 */
275 ip->i_inode.i_nlink = be32_to_cpu(str->di_nlink);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500276 di->di_size = be64_to_cpu(str->di_size);
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -0500277 i_size_write(&ip->i_inode, di->di_size);
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000278 gfs2_set_inode_blocks(&ip->i_inode, be64_to_cpu(str->di_blocks));
Steven Whitehouse719ee342008-09-18 13:53:59 +0100279 atime.tv_sec = be64_to_cpu(str->di_atime);
280 atime.tv_nsec = be32_to_cpu(str->di_atime_nsec);
281 if (timespec_compare(&ip->i_inode.i_atime, &atime) < 0)
282 ip->i_inode.i_atime = atime;
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -0500283 ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100284 ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec);
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -0500285 ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100286 ip->i_inode.i_ctime.tv_nsec = be32_to_cpu(str->di_ctime_nsec);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500287
Steven Whitehousece276b02008-02-06 09:25:45 +0000288 ip->i_goal = be64_to_cpu(str->di_goal_meta);
Steven Whitehousebcf0b5b2008-11-03 13:39:46 +0000289 ip->i_generation = be64_to_cpu(str->di_generation);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500290
291 di->di_flags = be32_to_cpu(str->di_flags);
Steven Whitehouse6b124d82006-11-08 12:51:06 -0500292 gfs2_set_inode_flags(&ip->i_inode);
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000293 height = be16_to_cpu(str->di_height);
294 if (unlikely(height > GFS2_MAX_META_HEIGHT))
295 goto corrupt;
296 ip->i_height = (u8)height;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500297
Steven Whitehouse9a004502008-02-01 09:23:44 +0000298 depth = be16_to_cpu(str->di_depth);
299 if (unlikely(depth > GFS2_DIR_MAX_DEPTH))
300 goto corrupt;
301 ip->i_depth = (u8)depth;
Steven Whitehousead6203f2008-11-03 13:59:19 +0000302 ip->i_entries = be32_to_cpu(str->di_entries);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500303
304 di->di_eattr = be64_to_cpu(str->di_eattr);
Steven Whitehouse55610932007-10-17 08:47:38 +0100305 if (S_ISREG(ip->i_inode.i_mode))
306 gfs2_set_aops(&ip->i_inode);
307
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500308 return 0;
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000309corrupt:
310 if (gfs2_consist_inode(ip))
311 gfs2_dinode_print(ip);
312 return -EIO;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500313}
314
David Teiglandb3b94fa2006-01-16 16:50:04 +0000315/**
316 * gfs2_inode_refresh - Refresh the incore copy of the dinode
317 * @ip: The GFS2 inode
318 *
319 * Returns: errno
320 */
321
322int gfs2_inode_refresh(struct gfs2_inode *ip)
323{
324 struct buffer_head *dibh;
325 int error;
326
327 error = gfs2_meta_inode_buffer(ip, &dibh);
328 if (error)
329 return error;
330
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400331 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000332 brelse(dibh);
333 return -EIO;
334 }
335
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500336 error = gfs2_dinode_in(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000337 brelse(dibh);
Steven Whitehousebfded272006-11-01 16:05:38 -0500338 clear_bit(GIF_INVALID, &ip->i_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000339
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500340 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000341}
342
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400343int gfs2_dinode_dealloc(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000344{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400345 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000346 struct gfs2_alloc *al;
347 struct gfs2_rgrpd *rgd;
348 int error;
349
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000350 if (gfs2_get_inode_blocks(&ip->i_inode) != 1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000351 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500352 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000353 return -EIO;
354 }
355
356 al = gfs2_alloc_get(ip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300357 if (!al)
358 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000359
360 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
361 if (error)
362 goto out;
363
364 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
365 if (error)
366 goto out_qs;
367
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100368 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000369 if (!rgd) {
370 gfs2_consist_inode(ip);
371 error = -EIO;
372 goto out_rindex_relse;
373 }
374
375 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
376 &al->al_rgd_gh);
377 if (error)
378 goto out_rindex_relse;
379
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400380 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000381 if (error)
382 goto out_rg_gunlock;
383
Steven Whitehouse2bcd6102007-11-08 14:25:12 +0000384 set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
385 set_bit(GLF_LFLUSH, &ip->i_gl->gl_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000386
387 gfs2_free_di(rgd, ip);
388
David Teiglandb3b94fa2006-01-16 16:50:04 +0000389 gfs2_trans_end(sdp);
390 clear_bit(GLF_STICKY, &ip->i_gl->gl_flags);
391
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) {
Miklos Szeredif58ba882008-07-02 21:12:01 +0200510 error = gfs2_permission(dir, MAY_EXEC);
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
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100526static void gfs2_inum_range_in(struct gfs2_inum_range_host *ir, const void *buf)
527{
528 const struct gfs2_inum_range *str = buf;
529
530 ir->ir_start = be64_to_cpu(str->ir_start);
531 ir->ir_length = be64_to_cpu(str->ir_length);
532}
533
534static void gfs2_inum_range_out(const struct gfs2_inum_range_host *ir, void *buf)
535{
536 struct gfs2_inum_range *str = buf;
537
538 str->ir_start = cpu_to_be64(ir->ir_start);
539 str->ir_length = cpu_to_be64(ir->ir_length);
540}
541
Steven Whitehousecd915492006-09-04 12:49:07 -0400542static int pick_formal_ino_1(struct gfs2_sbd *sdp, u64 *formal_ino)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000543{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400544 struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000545 struct buffer_head *bh;
Al Viroe6972642006-10-13 21:29:46 -0400546 struct gfs2_inum_range_host ir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000547 int error;
548
549 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
550 if (error)
551 return error;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000552 mutex_lock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000553
554 error = gfs2_meta_inode_buffer(ip, &bh);
555 if (error) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000556 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000557 gfs2_trans_end(sdp);
558 return error;
559 }
560
561 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
562
563 if (ir.ir_length) {
564 *formal_ino = ir.ir_start++;
565 ir.ir_length--;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000566 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000567 gfs2_inum_range_out(&ir,
568 bh->b_data + sizeof(struct gfs2_dinode));
569 brelse(bh);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000570 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000571 gfs2_trans_end(sdp);
572 return 0;
573 }
574
575 brelse(bh);
576
Steven Whitehousef55ab262006-02-21 12:51:39 +0000577 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000578 gfs2_trans_end(sdp);
579
580 return 1;
581}
582
Steven Whitehousecd915492006-09-04 12:49:07 -0400583static int pick_formal_ino_2(struct gfs2_sbd *sdp, u64 *formal_ino)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000584{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400585 struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
586 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_inum_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000587 struct gfs2_holder gh;
588 struct buffer_head *bh;
Al Viroe6972642006-10-13 21:29:46 -0400589 struct gfs2_inum_range_host ir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000590 int error;
591
592 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
593 if (error)
594 return error;
595
596 error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
597 if (error)
598 goto out;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000599 mutex_lock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000600
601 error = gfs2_meta_inode_buffer(ip, &bh);
602 if (error)
603 goto out_end_trans;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400604
David Teiglandb3b94fa2006-01-16 16:50:04 +0000605 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
606
607 if (!ir.ir_length) {
608 struct buffer_head *m_bh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400609 u64 x, y;
Al Virob44b84d2006-10-14 10:46:30 -0400610 __be64 z;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000611
612 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
613 if (error)
614 goto out_brelse;
615
Al Virob44b84d2006-10-14 10:46:30 -0400616 z = *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode));
617 x = y = be64_to_cpu(z);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000618 ir.ir_start = x;
619 ir.ir_length = GFS2_INUM_QUANTUM;
620 x += GFS2_INUM_QUANTUM;
621 if (x < y)
622 gfs2_consist_inode(m_ip);
Al Virob44b84d2006-10-14 10:46:30 -0400623 z = cpu_to_be64(x);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000624 gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
Al Virob44b84d2006-10-14 10:46:30 -0400625 *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = z;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000626
627 brelse(m_bh);
628 }
629
630 *formal_ino = ir.ir_start++;
631 ir.ir_length--;
632
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000633 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000634 gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode));
635
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400636out_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000637 brelse(bh);
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400638out_end_trans:
Steven Whitehousef55ab262006-02-21 12:51:39 +0000639 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000640 gfs2_trans_end(sdp);
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400641out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000642 gfs2_glock_dq_uninit(&gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000643 return error;
644}
645
Steven Whitehousecd915492006-09-04 12:49:07 -0400646static int pick_formal_ino(struct gfs2_sbd *sdp, u64 *inum)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000647{
648 int error;
649
650 error = pick_formal_ino_1(sdp, inum);
651 if (error <= 0)
652 return error;
653
654 error = pick_formal_ino_2(sdp, inum);
655
656 return error;
657}
658
659/**
660 * create_ok - OK to create a new on-disk inode here?
661 * @dip: Directory in which dinode is to be created
662 * @name: Name of new dinode
663 * @mode:
664 *
665 * Returns: errno
666 */
667
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400668static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000669 unsigned int mode)
670{
671 int error;
672
Miklos Szeredif58ba882008-07-02 21:12:01 +0200673 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000674 if (error)
675 return error;
676
677 /* Don't create entries in an unlinked directory */
Steven Whitehouse4f561102006-11-01 14:04:17 -0500678 if (!dip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000679 return -EPERM;
680
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100681 error = gfs2_dir_check(&dip->i_inode, name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000682 switch (error) {
683 case -ENOENT:
684 error = 0;
685 break;
686 case 0:
687 return -EEXIST;
688 default:
689 return error;
690 }
691
Steven Whitehousead6203f2008-11-03 13:59:19 +0000692 if (dip->i_entries == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000693 return -EFBIG;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500694 if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000695 return -EMLINK;
696
697 return 0;
698}
699
700static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
701 unsigned int *uid, unsigned int *gid)
702{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400703 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
Steven Whitehouse2933f922006-11-01 13:23:29 -0500704 (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000705 if (S_ISDIR(*mode))
706 *mode |= S_ISUID;
David Howells3de7be32008-11-14 10:38:53 +1100707 else if (dip->i_inode.i_uid != current_fsuid())
David Teiglandb3b94fa2006-01-16 16:50:04 +0000708 *mode &= ~07111;
Steven Whitehouse2933f922006-11-01 13:23:29 -0500709 *uid = dip->i_inode.i_uid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000710 } else
David Howells3de7be32008-11-14 10:38:53 +1100711 *uid = current_fsuid();
David Teiglandb3b94fa2006-01-16 16:50:04 +0000712
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500713 if (dip->i_inode.i_mode & S_ISGID) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000714 if (S_ISDIR(*mode))
715 *mode |= S_ISGID;
Steven Whitehouse2933f922006-11-01 13:23:29 -0500716 *gid = dip->i_inode.i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000717 } else
David Howells3de7be32008-11-14 10:38:53 +1100718 *gid = current_fsgid();
David Teiglandb3b94fa2006-01-16 16:50:04 +0000719}
720
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100721static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000722{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400723 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000724 int error;
725
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000726 if (gfs2_alloc_get(dip) == NULL)
727 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000728
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000729 dip->i_alloc->al_requested = RES_DINODE;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000730 error = gfs2_inplace_reserve(dip);
731 if (error)
732 goto out;
733
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400734 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000735 if (error)
736 goto out_ipreserv;
737
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100738 *no_addr = gfs2_alloc_di(dip, generation);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000739
740 gfs2_trans_end(sdp);
741
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400742out_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000743 gfs2_inplace_release(dip);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400744out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000745 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000746 return error;
747}
748
749/**
750 * init_dinode - Fill in a new dinode structure
751 * @dip: the directory this inode is being created in
752 * @gl: The glock covering the new inode
753 * @inum: the inode number
754 * @mode: the file permissions
755 * @uid:
756 * @gid:
757 *
758 */
759
760static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
Al Viro629a21e2006-10-13 22:51:24 -0400761 const struct gfs2_inum_host *inum, unsigned int mode,
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400762 unsigned int uid, unsigned int gid,
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400763 const u64 *generation, dev_t dev, struct buffer_head **bhp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000764{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400765 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000766 struct gfs2_dinode *di;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000767 struct buffer_head *dibh;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100768 struct timespec tv = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000769
770 dibh = gfs2_meta_new(gl, inum->no_addr);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000771 gfs2_trans_add_bh(gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000772 gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
773 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000774 di = (struct gfs2_dinode *)dibh->b_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000775
Steven Whitehouse2442a092006-01-30 11:49:32 +0000776 di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
777 di->di_num.no_addr = cpu_to_be64(inum->no_addr);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000778 di->di_mode = cpu_to_be32(mode);
779 di->di_uid = cpu_to_be32(uid);
780 di->di_gid = cpu_to_be32(gid);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500781 di->di_nlink = 0;
782 di->di_size = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000783 di->di_blocks = cpu_to_be64(1);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100784 di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500785 di->di_major = cpu_to_be32(MAJOR(dev));
786 di->di_minor = cpu_to_be32(MINOR(dev));
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000787 di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400788 di->di_generation = cpu_to_be64(*generation);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500789 di->di_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000790
791 if (S_ISREG(mode)) {
792 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA) ||
793 gfs2_tune_get(sdp, gt_new_files_jdata))
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000794 di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000795 } else if (S_ISDIR(mode)) {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500796 di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500797 GFS2_DIF_INHERIT_JDATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000798 }
799
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000800 di->__pad1 = 0;
Steven Whitehousea9583c72006-11-01 20:09:14 -0500801 di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500802 di->di_height = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000803 di->__pad2 = 0;
804 di->__pad3 = 0;
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500805 di->di_depth = 0;
806 di->di_entries = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000807 memset(&di->__pad4, 0, sizeof(di->__pad4));
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500808 di->di_eattr = 0;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100809 di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
810 di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
811 di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000812 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400813
814 set_buffer_uptodate(dibh);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000815
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400816 *bhp = dibh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000817}
818
819static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
Al Viro629a21e2006-10-13 22:51:24 -0400820 unsigned int mode, const struct gfs2_inum_host *inum,
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400821 const u64 *generation, dev_t dev, struct buffer_head **bhp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000822{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400823 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000824 unsigned int uid, gid;
825 int error;
826
827 munge_mode_uid_gid(dip, &mode, &uid, &gid);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300828 if (!gfs2_alloc_get(dip))
829 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000830
831 error = gfs2_quota_lock(dip, uid, gid);
832 if (error)
833 goto out;
834
835 error = gfs2_quota_check(dip, uid, gid);
836 if (error)
837 goto out_quota;
838
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400839 error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000840 if (error)
841 goto out_quota;
842
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400843 init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, bhp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000844 gfs2_quota_change(dip, +1, uid, gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000845 gfs2_trans_end(sdp);
846
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400847out_quota:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000848 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400849out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000850 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000851 return error;
852}
853
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400854static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
855 struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000856{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400857 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000858 struct gfs2_alloc *al;
859 int alloc_required;
860 struct buffer_head *dibh;
861 int error;
862
863 al = gfs2_alloc_get(dip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300864 if (!al)
865 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000866
867 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
868 if (error)
869 goto fail;
870
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400871 error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
Steven Whitehousec7526662006-03-20 12:30:04 -0500872 if (alloc_required < 0)
Bob Peterson1b8177e2008-01-19 21:50:24 -0600873 goto fail_quota_locks;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000874 if (alloc_required) {
Steven Whitehouse2933f922006-11-01 13:23:29 -0500875 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000876 if (error)
877 goto fail_quota_locks;
878
879 al->al_requested = sdp->sd_max_dirres;
880
881 error = gfs2_inplace_reserve(dip);
882 if (error)
883 goto fail_quota_locks;
884
Steven Whitehouse320dd102006-05-18 16:25:27 -0400885 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100886 al->al_rgd->rd_length +
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400887 2 * RES_DINODE +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000888 RES_STATFS + RES_QUOTA, 0);
889 if (error)
890 goto fail_ipreserv;
891 } else {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400892 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000893 if (error)
894 goto fail_quota_locks;
895 }
896
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100897 error = gfs2_dir_add(&dip->i_inode, name, ip, IF2DT(ip->i_inode.i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000898 if (error)
899 goto fail_end_trans;
900
901 error = gfs2_meta_inode_buffer(ip, &dibh);
902 if (error)
903 goto fail_end_trans;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500904 ip->i_inode.i_nlink = 1;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000905 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500906 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000907 brelse(dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000908 return 0;
909
Steven Whitehouse320dd102006-05-18 16:25:27 -0400910fail_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000911 gfs2_trans_end(sdp);
912
Steven Whitehouse320dd102006-05-18 16:25:27 -0400913fail_ipreserv:
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000914 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000915 gfs2_inplace_release(dip);
916
Steven Whitehouse320dd102006-05-18 16:25:27 -0400917fail_quota_locks:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000918 gfs2_quota_unlock(dip);
919
Steven Whitehouse320dd102006-05-18 16:25:27 -0400920fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000921 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000922 return error;
923}
924
Ryan O'Harafcb47e02006-10-03 11:57:35 -0400925static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip)
926{
927 int err;
928 size_t len;
929 void *value;
930 char *name;
931 struct gfs2_ea_request er;
932
933 err = security_inode_init_security(&ip->i_inode, &dip->i_inode,
934 &name, &value, &len);
935
936 if (err) {
937 if (err == -EOPNOTSUPP)
938 return 0;
939 return err;
940 }
941
942 memset(&er, 0, sizeof(struct gfs2_ea_request));
943
944 er.er_type = GFS2_EATYPE_SECURITY;
945 er.er_name = name;
946 er.er_data = value;
947 er.er_name_len = strlen(name);
948 er.er_data_len = len;
949
950 err = gfs2_ea_set_i(ip, &er);
951
952 kfree(value);
953 kfree(name);
954
955 return err;
956}
957
David Teiglandb3b94fa2006-01-16 16:50:04 +0000958/**
959 * gfs2_createi - Create a new inode
960 * @ghs: An array of two holders
961 * @name: The name of the new file
962 * @mode: the permissions on the new inode
963 *
964 * @ghs[0] is an initialized holder for the directory
965 * @ghs[1] is the holder for the inode lock
966 *
Steven Whitehouse7359a192006-02-13 12:27:43 +0000967 * If the return value is not NULL, the glocks on both the directory and the new
David Teiglandb3b94fa2006-01-16 16:50:04 +0000968 * file are held. A transaction has been started and an inplace reservation
969 * is held, as well.
970 *
Steven Whitehouse7359a192006-02-13 12:27:43 +0000971 * Returns: An inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000972 */
973
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400974struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500975 unsigned int mode, dev_t dev)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000976{
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100977 struct inode *inode = NULL;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500978 struct gfs2_inode *dip = ghs->gh_gl->gl_object;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400979 struct inode *dir = &dip->i_inode;
980 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100981 struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000982 int error;
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400983 u64 generation;
Steven Whitehousef91a0d32007-10-15 16:29:05 +0100984 struct buffer_head *bh = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000985
986 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehouse7359a192006-02-13 12:27:43 +0000987 return ERR_PTR(-ENAMETOOLONG);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000988
David Teiglandb3b94fa2006-01-16 16:50:04 +0000989 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
990 error = gfs2_glock_nq(ghs);
991 if (error)
992 goto fail;
993
994 error = create_ok(dip, name, mode);
995 if (error)
996 goto fail_gunlock;
997
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400998 error = pick_formal_ino(sdp, &inum.no_formal_ino);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000999 if (error)
1000 goto fail_gunlock;
1001
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001002 error = alloc_dinode(dip, &inum.no_addr, &generation);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001003 if (error)
1004 goto fail_gunlock;
1005
Steven Whitehouse28626e22006-11-22 11:13:21 -05001006 error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
1007 LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
1008 if (error)
1009 goto fail_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001010
Wendy Chenge9bd2b32007-08-24 09:15:01 -04001011 error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001012 if (error)
1013 goto fail_gunlock2;
1014
Wendy Chengbb9bcf02007-06-27 17:07:08 -04001015 inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode),
1016 inum.no_addr,
Benjamin Marzinski7a9f53b2007-09-18 13:33:18 -05001017 inum.no_formal_ino, 0);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001018 if (IS_ERR(inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001019 goto fail_gunlock2;
1020
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001021 error = gfs2_inode_refresh(GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001022 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +01001023 goto fail_gunlock2;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001024
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001025 error = gfs2_acl_create(dip, GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001026 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +01001027 goto fail_gunlock2;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001028
Ryan O'Harafcb47e02006-10-03 11:57:35 -04001029 error = gfs2_security_init(dip, GFS2_I(inode));
1030 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +01001031 goto fail_gunlock2;
Ryan O'Harafcb47e02006-10-03 11:57:35 -04001032
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001033 error = link_dinode(dip, name, GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001034 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +01001035 goto fail_gunlock2;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001036
Steven Whitehousef91a0d32007-10-15 16:29:05 +01001037 if (bh)
1038 brelse(bh);
Steven Whitehouse7359a192006-02-13 12:27:43 +00001039 return inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001040
Steven Whitehouse320dd102006-05-18 16:25:27 -04001041fail_gunlock2:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001042 gfs2_glock_dq_uninit(ghs + 1);
Julien Brunelbd1eb882008-09-01 10:51:22 +02001043 if (inode && !IS_ERR(inode))
Steven Whitehousee1cc8602007-06-07 11:47:52 +01001044 iput(inode);
Steven Whitehouse320dd102006-05-18 16:25:27 -04001045fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001046 gfs2_glock_dq(ghs);
Steven Whitehouse320dd102006-05-18 16:25:27 -04001047fail:
Steven Whitehousef91a0d32007-10-15 16:29:05 +01001048 if (bh)
1049 brelse(bh);
Steven Whitehouse7359a192006-02-13 12:27:43 +00001050 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001051}
1052
1053/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001054 * gfs2_rmdiri - Remove a directory
1055 * @dip: The parent directory of the directory to be removed
1056 * @name: The name of the directory to be removed
1057 * @ip: The GFS2 inode of the directory to be removed
1058 *
1059 * Assumes Glocks on dip and ip are held
1060 *
1061 * Returns: errno
1062 */
1063
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001064int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
1065 struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001066{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001067 struct qstr dotname;
1068 int error;
1069
Steven Whitehousead6203f2008-11-03 13:59:19 +00001070 if (ip->i_entries != 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001071 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -05001072 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001073 return -EIO;
1074 }
1075
1076 error = gfs2_dir_del(dip, name);
1077 if (error)
1078 return error;
1079
1080 error = gfs2_change_nlink(dip, -1);
1081 if (error)
1082 return error;
1083
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001084 gfs2_str2qstr(&dotname, ".");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001085 error = gfs2_dir_del(ip, &dotname);
1086 if (error)
1087 return error;
1088
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001089 gfs2_str2qstr(&dotname, "..");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001090 error = gfs2_dir_del(ip, &dotname);
1091 if (error)
1092 return error;
1093
Steven Whitehouse4f561102006-11-01 14:04:17 -05001094 /* It looks odd, but it really should be done twice */
1095 error = gfs2_change_nlink(ip, -1);
1096 if (error)
1097 return error;
1098
1099 error = gfs2_change_nlink(ip, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001100 if (error)
1101 return error;
1102
David Teiglandb3b94fa2006-01-16 16:50:04 +00001103 return error;
1104}
1105
1106/*
1107 * gfs2_unlink_ok - check to see that a inode is still in a directory
1108 * @dip: the directory
1109 * @name: the name of the file
1110 * @ip: the inode
1111 *
1112 * Assumes that the lock on (at least) @dip is held.
1113 *
1114 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
1115 */
1116
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001117int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001118 const struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001119{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001120 int error;
1121
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001122 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001123 return -EPERM;
1124
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001125 if ((dip->i_inode.i_mode & S_ISVTX) &&
David Howells3de7be32008-11-14 10:38:53 +11001126 dip->i_inode.i_uid != current_fsuid() &&
1127 ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001128 return -EPERM;
1129
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001130 if (IS_APPEND(&dip->i_inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001131 return -EPERM;
1132
Miklos Szeredif58ba882008-07-02 21:12:01 +02001133 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001134 if (error)
1135 return error;
1136
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001137 error = gfs2_dir_check(&dip->i_inode, name, ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001138 if (error)
1139 return error;
1140
David Teiglandb3b94fa2006-01-16 16:50:04 +00001141 return 0;
1142}
1143
David Teiglandb3b94fa2006-01-16 16:50:04 +00001144/**
1145 * gfs2_readlinki - return the contents of a symlink
1146 * @ip: the symlink's inode
1147 * @buf: a pointer to the buffer to be filled
1148 * @len: a pointer to the length of @buf
1149 *
1150 * If @buf is too small, a piece of memory is kmalloc()ed and needs
1151 * to be freed by the caller.
1152 *
1153 * Returns: errno
1154 */
1155
1156int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
1157{
1158 struct gfs2_holder i_gh;
1159 struct buffer_head *dibh;
1160 unsigned int x;
1161 int error;
1162
Steven Whitehouse719ee342008-09-18 13:53:59 +01001163 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
1164 error = gfs2_glock_nq(&i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001165 if (error) {
1166 gfs2_holder_uninit(&i_gh);
1167 return error;
1168 }
1169
1170 if (!ip->i_di.di_size) {
1171 gfs2_consist_inode(ip);
1172 error = -EIO;
1173 goto out;
1174 }
1175
1176 error = gfs2_meta_inode_buffer(ip, &dibh);
1177 if (error)
1178 goto out;
1179
1180 x = ip->i_di.di_size + 1;
1181 if (x > *len) {
Josef Bacik16c5f062008-04-09 09:33:41 -04001182 *buf = kmalloc(x, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001183 if (!*buf) {
1184 error = -ENOMEM;
1185 goto out_brelse;
1186 }
1187 }
1188
1189 memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
1190 *len = x;
1191
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001192out_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001193 brelse(dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001194out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001195 gfs2_glock_dq_uninit(&i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001196 return error;
1197}
1198
David Teiglandb3b94fa2006-01-16 16:50:04 +00001199static int
1200__gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1201{
1202 struct buffer_head *dibh;
1203 int error;
1204
1205 error = gfs2_meta_inode_buffer(ip, &dibh);
1206 if (!error) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001207 error = inode_setattr(&ip->i_inode, attr);
1208 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001209 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001210 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001211 brelse(dibh);
1212 }
1213 return error;
1214}
1215
1216/**
1217 * gfs2_setattr_simple -
1218 * @ip:
1219 * @attr:
1220 *
1221 * Called with a reference on the vnode.
1222 *
1223 * Returns: errno
1224 */
1225
1226int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1227{
1228 int error;
1229
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001230 if (current->journal_info)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001231 return __gfs2_setattr_simple(ip, attr);
1232
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001233 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001234 if (error)
1235 return error;
1236
1237 error = __gfs2_setattr_simple(ip, attr);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001238 gfs2_trans_end(GFS2_SB(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001239 return error;
1240}
1241
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001242void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
1243{
1244 const struct gfs2_dinode_host *di = &ip->i_di;
1245 struct gfs2_dinode *str = buf;
1246
1247 str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
1248 str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
1249 str->di_header.__pad0 = 0;
1250 str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
1251 str->di_header.__pad1 = 0;
1252 str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
1253 str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
1254 str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
1255 str->di_uid = cpu_to_be32(ip->i_inode.i_uid);
1256 str->di_gid = cpu_to_be32(ip->i_inode.i_gid);
1257 str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
1258 str->di_size = cpu_to_be64(di->di_size);
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001259 str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001260 str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
1261 str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
1262 str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
1263
Steven Whitehousece276b02008-02-06 09:25:45 +00001264 str->di_goal_meta = cpu_to_be64(ip->i_goal);
1265 str->di_goal_data = cpu_to_be64(ip->i_goal);
Steven Whitehousebcf0b5b2008-11-03 13:39:46 +00001266 str->di_generation = cpu_to_be64(ip->i_generation);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001267
1268 str->di_flags = cpu_to_be32(di->di_flags);
Steven Whitehouseecc30c72008-01-28 10:37:35 +00001269 str->di_height = cpu_to_be16(ip->i_height);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001270 str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
1271 !(ip->i_di.di_flags & GFS2_DIF_EXHASH) ?
1272 GFS2_FORMAT_DE : 0);
Steven Whitehouse9a004502008-02-01 09:23:44 +00001273 str->di_depth = cpu_to_be16(ip->i_depth);
Steven Whitehousead6203f2008-11-03 13:59:19 +00001274 str->di_entries = cpu_to_be32(ip->i_entries);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001275
1276 str->di_eattr = cpu_to_be64(di->di_eattr);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001277 str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
1278 str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
1279 str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001280}
1281
1282void gfs2_dinode_print(const struct gfs2_inode *ip)
1283{
1284 const struct gfs2_dinode_host *di = &ip->i_di;
1285
1286 printk(KERN_INFO " no_formal_ino = %llu\n",
1287 (unsigned long long)ip->i_no_formal_ino);
1288 printk(KERN_INFO " no_addr = %llu\n",
1289 (unsigned long long)ip->i_no_addr);
1290 printk(KERN_INFO " di_size = %llu\n", (unsigned long long)di->di_size);
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001291 printk(KERN_INFO " blocks = %llu\n",
1292 (unsigned long long)gfs2_get_inode_blocks(&ip->i_inode));
Steven Whitehousece276b02008-02-06 09:25:45 +00001293 printk(KERN_INFO " i_goal = %llu\n",
1294 (unsigned long long)ip->i_goal);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001295 printk(KERN_INFO " di_flags = 0x%.8X\n", di->di_flags);
Bob Petersonca390602008-01-28 11:15:57 -06001296 printk(KERN_INFO " i_height = %u\n", ip->i_height);
Steven Whitehouse9a004502008-02-01 09:23:44 +00001297 printk(KERN_INFO " i_depth = %u\n", ip->i_depth);
Steven Whitehousead6203f2008-11-03 13:59:19 +00001298 printk(KERN_INFO " i_entries = %u\n", ip->i_entries);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001299 printk(KERN_INFO " di_eattr = %llu\n",
1300 (unsigned long long)di->di_eattr);
1301}
1302