blob: 3b87c188da41c59eabe5f082f7e832e94c0fa079 [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'Harafcb47e0b2006-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 Peterson091806ed2008-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 Peterson091806ed2008-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 Peterson091806ed2008-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 Peterson091806ed2008-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 Peterson091806ed2008-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{
Steven Whitehouseea744d02006-10-31 15:28:00 -0500250 const struct gfs2_dinode *str = buf;
Steven Whitehouse719ee342008-09-18 13:53:59 +0100251 struct timespec atime;
Steven Whitehouse9a004502008-02-01 09:23:44 +0000252 u16 height, depth;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500253
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000254 if (unlikely(ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)))
255 goto corrupt;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100256 ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500257 ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500258 ip->i_inode.i_rdev = 0;
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500259 switch (ip->i_inode.i_mode & S_IFMT) {
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500260 case S_IFBLK:
261 case S_IFCHR:
262 ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
263 be32_to_cpu(str->di_minor));
264 break;
265 };
266
Steven Whitehouse2933f922006-11-01 13:23:29 -0500267 ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
268 ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
Steven Whitehouse4f561102006-11-01 14:04:17 -0500269 /*
270 * We will need to review setting the nlink count here in the
271 * light of the forthcoming ro bind mount work. This is a reminder
272 * to do that.
273 */
274 ip->i_inode.i_nlink = be32_to_cpu(str->di_nlink);
Steven Whitehousec9e98882008-11-04 09:47:33 +0000275 ip->i_disksize = be64_to_cpu(str->di_size);
276 i_size_write(&ip->i_inode, ip->i_disksize);
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000277 gfs2_set_inode_blocks(&ip->i_inode, be64_to_cpu(str->di_blocks));
Steven Whitehouse719ee342008-09-18 13:53:59 +0100278 atime.tv_sec = be64_to_cpu(str->di_atime);
279 atime.tv_nsec = be32_to_cpu(str->di_atime_nsec);
280 if (timespec_compare(&ip->i_inode.i_atime, &atime) < 0)
281 ip->i_inode.i_atime = atime;
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -0500282 ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100283 ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec);
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -0500284 ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100285 ip->i_inode.i_ctime.tv_nsec = be32_to_cpu(str->di_ctime_nsec);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500286
Steven Whitehousece276b02008-02-06 09:25:45 +0000287 ip->i_goal = be64_to_cpu(str->di_goal_meta);
Steven Whitehousebcf0b5b2008-11-03 13:39:46 +0000288 ip->i_generation = be64_to_cpu(str->di_generation);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500289
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000290 ip->i_diskflags = be32_to_cpu(str->di_flags);
Steven Whitehouse6b124d82006-11-08 12:51:06 -0500291 gfs2_set_inode_flags(&ip->i_inode);
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000292 height = be16_to_cpu(str->di_height);
293 if (unlikely(height > GFS2_MAX_META_HEIGHT))
294 goto corrupt;
295 ip->i_height = (u8)height;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500296
Steven Whitehouse9a004502008-02-01 09:23:44 +0000297 depth = be16_to_cpu(str->di_depth);
298 if (unlikely(depth > GFS2_DIR_MAX_DEPTH))
299 goto corrupt;
300 ip->i_depth = (u8)depth;
Steven Whitehousead6203f2008-11-03 13:59:19 +0000301 ip->i_entries = be32_to_cpu(str->di_entries);
Steven Whitehouseea744d02006-10-31 15:28:00 -0500302
Steven Whitehouse3767ac22008-11-03 14:28:42 +0000303 ip->i_eattr = be64_to_cpu(str->di_eattr);
Steven Whitehouse55610932007-10-17 08:47:38 +0100304 if (S_ISREG(ip->i_inode.i_mode))
305 gfs2_set_aops(&ip->i_inode);
306
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500307 return 0;
Steven Whitehouseecc30c72008-01-28 10:37:35 +0000308corrupt:
309 if (gfs2_consist_inode(ip))
310 gfs2_dinode_print(ip);
311 return -EIO;
Steven Whitehouseea744d02006-10-31 15:28:00 -0500312}
313
David Teiglandb3b94fa2006-01-16 16:50:04 +0000314/**
315 * gfs2_inode_refresh - Refresh the incore copy of the dinode
316 * @ip: The GFS2 inode
317 *
318 * Returns: errno
319 */
320
321int gfs2_inode_refresh(struct gfs2_inode *ip)
322{
323 struct buffer_head *dibh;
324 int error;
325
326 error = gfs2_meta_inode_buffer(ip, &dibh);
327 if (error)
328 return error;
329
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400330 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000331 brelse(dibh);
332 return -EIO;
333 }
334
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500335 error = gfs2_dinode_in(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000336 brelse(dibh);
Steven Whitehousebfded272006-11-01 16:05:38 -0500337 clear_bit(GIF_INVALID, &ip->i_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000338
Steven Whitehouseaf339c02006-11-01 10:34:15 -0500339 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000340}
341
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400342int gfs2_dinode_dealloc(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000343{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400344 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000345 struct gfs2_alloc *al;
346 struct gfs2_rgrpd *rgd;
347 int error;
348
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000349 if (gfs2_get_inode_blocks(&ip->i_inode) != 1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000350 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500351 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000352 return -EIO;
353 }
354
355 al = gfs2_alloc_get(ip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300356 if (!al)
357 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000358
359 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
360 if (error)
361 goto out;
362
363 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
364 if (error)
365 goto out_qs;
366
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100367 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000368 if (!rgd) {
369 gfs2_consist_inode(ip);
370 error = -EIO;
371 goto out_rindex_relse;
372 }
373
374 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
375 &al->al_rgd_gh);
376 if (error)
377 goto out_rindex_relse;
378
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400379 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000380 if (error)
381 goto out_rg_gunlock;
382
Steven Whitehouse2bcd6102007-11-08 14:25:12 +0000383 set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
384 set_bit(GLF_LFLUSH, &ip->i_gl->gl_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000385
386 gfs2_free_di(rgd, ip);
387
David Teiglandb3b94fa2006-01-16 16:50:04 +0000388 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000389
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400390out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000391 gfs2_glock_dq_uninit(&al->al_rgd_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400392out_rindex_relse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000393 gfs2_glock_dq_uninit(&al->al_ri_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400394out_qs:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000395 gfs2_quota_unhold(ip);
Steven Whitehouse36327522006-04-28 10:46:21 -0400396out:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400397 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000398 return error;
399}
400
401/**
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500402 * gfs2_change_nlink - Change nlink count on inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000403 * @ip: The GFS2 inode
404 * @diff: The change in the nlink count required
405 *
406 * Returns: errno
407 */
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500408int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000409{
410 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400411 u32 nlink;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000412 int error;
413
Steven Whitehouse4f561102006-11-01 14:04:17 -0500414 BUG_ON(diff != 1 && diff != -1);
415 nlink = ip->i_inode.i_nlink + diff;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000416
417 /* If we are reducing the nlink count, but the new value ends up being
418 bigger than the old one, we must have underflowed. */
Steven Whitehouse4f561102006-11-01 14:04:17 -0500419 if (diff < 0 && nlink > ip->i_inode.i_nlink) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000420 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500421 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000422 return -EIO;
423 }
424
425 error = gfs2_meta_inode_buffer(ip, &dibh);
426 if (error)
427 return error;
428
Steven Whitehouse4f561102006-11-01 14:04:17 -0500429 if (diff > 0)
430 inc_nlink(&ip->i_inode);
431 else
432 drop_nlink(&ip->i_inode);
433
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100434 ip->i_inode.i_ctime = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000435
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000436 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500437 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000438 brelse(dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400439 mark_inode_dirty(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000440
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500441 if (ip->i_inode.i_nlink == 0)
Russell Cattelanddee7602007-01-29 17:13:44 -0600442 gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500443
S. Wendy Cheng55098262007-01-18 15:56:34 -0500444 return error;
445}
446
Steven Whitehousec7526662006-03-20 12:30:04 -0500447struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
448{
449 struct qstr qstr;
Russell Cattelan6c93fd12007-01-08 17:47:51 -0600450 struct inode *inode;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500451 gfs2_str2qstr(&qstr, name);
Al Viroa569c712008-07-23 14:42:05 -0400452 inode = gfs2_lookupi(dip, &qstr, 1);
Russell Cattelan6c93fd12007-01-08 17:47:51 -0600453 /* gfs2_lookupi has inconsistent callers: vfs
454 * related routines expect NULL for no entry found,
455 * gfs2_lookup_simple callers expect ENOENT
456 * and do not check for NULL.
457 */
458 if (inode == NULL)
459 return ERR_PTR(-ENOENT);
460 else
461 return inode;
Steven Whitehousec7526662006-03-20 12:30:04 -0500462}
463
464
David Teiglandb3b94fa2006-01-16 16:50:04 +0000465/**
466 * gfs2_lookupi - Look up a filename in a directory and return its inode
467 * @d_gh: An initialized holder for the directory glock
468 * @name: The name of the inode to look for
469 * @is_root: If 1, ignore the caller's permissions
470 * @i_gh: An uninitialized holder for the new inode glock
471 *
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000472 * This can be called via the VFS filldir function when NFS is doing
473 * a readdirplus and the inode which its intending to stat isn't
474 * already in cache. In this case we must not take the directory glock
475 * again, since the readdir call will have already taken that lock.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000476 *
477 * Returns: errno
478 */
479
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400480struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
Al Viroa569c712008-07-23 14:42:05 -0400481 int is_root)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000482{
Steven Whitehousec9fd4302006-03-01 15:31:02 -0500483 struct super_block *sb = dir->i_sb;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400484 struct gfs2_inode *dip = GFS2_I(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000485 struct gfs2_holder d_gh;
akpm@linux-foundation.org037bcbb2007-06-08 16:42:14 -0700486 int error = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500487 struct inode *inode = NULL;
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000488 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000489
490 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehousec7526662006-03-20 12:30:04 -0500491 return ERR_PTR(-ENAMETOOLONG);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000492
Steven Whitehousec7526662006-03-20 12:30:04 -0500493 if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
494 (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
495 dir == sb->s_root->d_inode)) {
Steven Whitehouse320dd102006-05-18 16:25:27 -0400496 igrab(dir);
497 return dir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000498 }
499
Steven Whitehouse7afd88d2008-02-22 16:07:18 +0000500 if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000501 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
502 if (error)
503 return ERR_PTR(error);
504 unlock = 1;
505 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000506
507 if (!is_root) {
Miklos Szeredif58ba882008-07-02 21:12:01 +0200508 error = gfs2_permission(dir, MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000509 if (error)
510 goto out;
511 }
512
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100513 inode = gfs2_dir_search(dir, name);
514 if (IS_ERR(inode))
515 error = PTR_ERR(inode);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000516out:
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000517 if (unlock)
518 gfs2_glock_dq_uninit(&d_gh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500519 if (error == -ENOENT)
520 return NULL;
Steven Whitehoused7c103d2007-01-25 17:14:59 +0000521 return inode ? inode : ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000522}
523
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100524static void gfs2_inum_range_in(struct gfs2_inum_range_host *ir, const void *buf)
525{
526 const struct gfs2_inum_range *str = buf;
527
528 ir->ir_start = be64_to_cpu(str->ir_start);
529 ir->ir_length = be64_to_cpu(str->ir_length);
530}
531
532static void gfs2_inum_range_out(const struct gfs2_inum_range_host *ir, void *buf)
533{
534 struct gfs2_inum_range *str = buf;
535
536 str->ir_start = cpu_to_be64(ir->ir_start);
537 str->ir_length = cpu_to_be64(ir->ir_length);
538}
539
Steven Whitehousecd915492006-09-04 12:49:07 -0400540static int pick_formal_ino_1(struct gfs2_sbd *sdp, u64 *formal_ino)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000541{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400542 struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000543 struct buffer_head *bh;
Al Viroe6972642006-10-13 21:29:46 -0400544 struct gfs2_inum_range_host ir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000545 int error;
546
547 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
548 if (error)
549 return error;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000550 mutex_lock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000551
552 error = gfs2_meta_inode_buffer(ip, &bh);
553 if (error) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000554 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000555 gfs2_trans_end(sdp);
556 return error;
557 }
558
559 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
560
561 if (ir.ir_length) {
562 *formal_ino = ir.ir_start++;
563 ir.ir_length--;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000564 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000565 gfs2_inum_range_out(&ir,
566 bh->b_data + sizeof(struct gfs2_dinode));
567 brelse(bh);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000568 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000569 gfs2_trans_end(sdp);
570 return 0;
571 }
572
573 brelse(bh);
574
Steven Whitehousef55ab262006-02-21 12:51:39 +0000575 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000576 gfs2_trans_end(sdp);
577
578 return 1;
579}
580
Steven Whitehousecd915492006-09-04 12:49:07 -0400581static int pick_formal_ino_2(struct gfs2_sbd *sdp, u64 *formal_ino)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000582{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400583 struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
584 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_inum_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000585 struct gfs2_holder gh;
586 struct buffer_head *bh;
Al Viroe6972642006-10-13 21:29:46 -0400587 struct gfs2_inum_range_host ir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000588 int error;
589
590 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
591 if (error)
592 return error;
593
594 error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
595 if (error)
596 goto out;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000597 mutex_lock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000598
599 error = gfs2_meta_inode_buffer(ip, &bh);
600 if (error)
601 goto out_end_trans;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400602
David Teiglandb3b94fa2006-01-16 16:50:04 +0000603 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
604
605 if (!ir.ir_length) {
606 struct buffer_head *m_bh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400607 u64 x, y;
Al Virob44b84d2006-10-14 10:46:30 -0400608 __be64 z;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000609
610 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
611 if (error)
612 goto out_brelse;
613
Al Virob44b84d2006-10-14 10:46:30 -0400614 z = *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode));
615 x = y = be64_to_cpu(z);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000616 ir.ir_start = x;
617 ir.ir_length = GFS2_INUM_QUANTUM;
618 x += GFS2_INUM_QUANTUM;
619 if (x < y)
620 gfs2_consist_inode(m_ip);
Al Virob44b84d2006-10-14 10:46:30 -0400621 z = cpu_to_be64(x);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000622 gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
Al Virob44b84d2006-10-14 10:46:30 -0400623 *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = z;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000624
625 brelse(m_bh);
626 }
627
628 *formal_ino = ir.ir_start++;
629 ir.ir_length--;
630
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000631 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000632 gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode));
633
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400634out_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000635 brelse(bh);
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400636out_end_trans:
Steven Whitehousef55ab262006-02-21 12:51:39 +0000637 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000638 gfs2_trans_end(sdp);
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400639out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000640 gfs2_glock_dq_uninit(&gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000641 return error;
642}
643
Steven Whitehousecd915492006-09-04 12:49:07 -0400644static int pick_formal_ino(struct gfs2_sbd *sdp, u64 *inum)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000645{
646 int error;
647
648 error = pick_formal_ino_1(sdp, inum);
649 if (error <= 0)
650 return error;
651
652 error = pick_formal_ino_2(sdp, inum);
653
654 return error;
655}
656
657/**
658 * create_ok - OK to create a new on-disk inode here?
659 * @dip: Directory in which dinode is to be created
660 * @name: Name of new dinode
661 * @mode:
662 *
663 * Returns: errno
664 */
665
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400666static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000667 unsigned int mode)
668{
669 int error;
670
Miklos Szeredif58ba882008-07-02 21:12:01 +0200671 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000672 if (error)
673 return error;
674
675 /* Don't create entries in an unlinked directory */
Steven Whitehouse4f561102006-11-01 14:04:17 -0500676 if (!dip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000677 return -EPERM;
678
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100679 error = gfs2_dir_check(&dip->i_inode, name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000680 switch (error) {
681 case -ENOENT:
682 error = 0;
683 break;
684 case 0:
685 return -EEXIST;
686 default:
687 return error;
688 }
689
Steven Whitehousead6203f2008-11-03 13:59:19 +0000690 if (dip->i_entries == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000691 return -EFBIG;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500692 if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000693 return -EMLINK;
694
695 return 0;
696}
697
698static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
699 unsigned int *uid, unsigned int *gid)
700{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400701 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
Steven Whitehouse2933f922006-11-01 13:23:29 -0500702 (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000703 if (S_ISDIR(*mode))
704 *mode |= S_ISUID;
David Howells3de7be32008-11-14 10:38:53 +1100705 else if (dip->i_inode.i_uid != current_fsuid())
David Teiglandb3b94fa2006-01-16 16:50:04 +0000706 *mode &= ~07111;
Steven Whitehouse2933f922006-11-01 13:23:29 -0500707 *uid = dip->i_inode.i_uid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000708 } else
David Howells3de7be32008-11-14 10:38:53 +1100709 *uid = current_fsuid();
David Teiglandb3b94fa2006-01-16 16:50:04 +0000710
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500711 if (dip->i_inode.i_mode & S_ISGID) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000712 if (S_ISDIR(*mode))
713 *mode |= S_ISGID;
Steven Whitehouse2933f922006-11-01 13:23:29 -0500714 *gid = dip->i_inode.i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000715 } else
David Howells3de7be32008-11-14 10:38:53 +1100716 *gid = current_fsgid();
David Teiglandb3b94fa2006-01-16 16:50:04 +0000717}
718
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100719static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000720{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400721 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000722 int error;
723
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000724 if (gfs2_alloc_get(dip) == NULL)
725 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000726
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000727 dip->i_alloc->al_requested = RES_DINODE;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000728 error = gfs2_inplace_reserve(dip);
729 if (error)
730 goto out;
731
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400732 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000733 if (error)
734 goto out_ipreserv;
735
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100736 *no_addr = gfs2_alloc_di(dip, generation);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000737
738 gfs2_trans_end(sdp);
739
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400740out_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000741 gfs2_inplace_release(dip);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400742out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000743 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000744 return error;
745}
746
747/**
748 * init_dinode - Fill in a new dinode structure
749 * @dip: the directory this inode is being created in
750 * @gl: The glock covering the new inode
751 * @inum: the inode number
752 * @mode: the file permissions
753 * @uid:
754 * @gid:
755 *
756 */
757
758static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
Al Viro629a21e2006-10-13 22:51:24 -0400759 const struct gfs2_inum_host *inum, unsigned int mode,
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400760 unsigned int uid, unsigned int gid,
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400761 const u64 *generation, dev_t dev, struct buffer_head **bhp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000762{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400763 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000764 struct gfs2_dinode *di;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000765 struct buffer_head *dibh;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100766 struct timespec tv = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000767
768 dibh = gfs2_meta_new(gl, inum->no_addr);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000769 gfs2_trans_add_bh(gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000770 gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
771 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000772 di = (struct gfs2_dinode *)dibh->b_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000773
Steven Whitehouse2442a092006-01-30 11:49:32 +0000774 di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
775 di->di_num.no_addr = cpu_to_be64(inum->no_addr);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000776 di->di_mode = cpu_to_be32(mode);
777 di->di_uid = cpu_to_be32(uid);
778 di->di_gid = cpu_to_be32(gid);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500779 di->di_nlink = 0;
780 di->di_size = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000781 di->di_blocks = cpu_to_be64(1);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100782 di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500783 di->di_major = cpu_to_be32(MAJOR(dev));
784 di->di_minor = cpu_to_be32(MINOR(dev));
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000785 di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400786 di->di_generation = cpu_to_be64(*generation);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500787 di->di_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000788
789 if (S_ISREG(mode)) {
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000790 if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
David Teiglandb3b94fa2006-01-16 16:50:04 +0000791 gfs2_tune_get(sdp, gt_new_files_jdata))
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000792 di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000793 } else if (S_ISDIR(mode)) {
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000794 di->di_flags |= cpu_to_be32(dip->i_diskflags &
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500795 GFS2_DIF_INHERIT_JDATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000796 }
797
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000798 di->__pad1 = 0;
Steven Whitehousea9583c792006-11-01 20:09:14 -0500799 di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500800 di->di_height = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000801 di->__pad2 = 0;
802 di->__pad3 = 0;
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500803 di->di_depth = 0;
804 di->di_entries = 0;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000805 memset(&di->__pad4, 0, sizeof(di->__pad4));
Steven Whitehouse294caaa2006-11-02 11:59:28 -0500806 di->di_eattr = 0;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100807 di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
808 di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
809 di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000810 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400811
812 set_buffer_uptodate(dibh);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +0000813
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400814 *bhp = dibh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000815}
816
817static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
Al Viro629a21e2006-10-13 22:51:24 -0400818 unsigned int mode, const struct gfs2_inum_host *inum,
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400819 const u64 *generation, dev_t dev, struct buffer_head **bhp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000820{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400821 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000822 unsigned int uid, gid;
823 int error;
824
825 munge_mode_uid_gid(dip, &mode, &uid, &gid);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300826 if (!gfs2_alloc_get(dip))
827 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000828
829 error = gfs2_quota_lock(dip, uid, gid);
830 if (error)
831 goto out;
832
833 error = gfs2_quota_check(dip, uid, gid);
834 if (error)
835 goto out_quota;
836
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400837 error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000838 if (error)
839 goto out_quota;
840
Wendy Chenge9bd2b32007-08-24 09:15:01 -0400841 init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, bhp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000842 gfs2_quota_change(dip, +1, uid, gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000843 gfs2_trans_end(sdp);
844
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400845out_quota:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000846 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400847out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000848 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000849 return error;
850}
851
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400852static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
853 struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000854{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400855 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000856 struct gfs2_alloc *al;
857 int alloc_required;
858 struct buffer_head *dibh;
859 int error;
860
861 al = gfs2_alloc_get(dip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300862 if (!al)
863 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000864
865 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
866 if (error)
867 goto fail;
868
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400869 error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
Steven Whitehousec7526662006-03-20 12:30:04 -0500870 if (alloc_required < 0)
Bob Peterson1b8177e2008-01-19 21:50:24 -0600871 goto fail_quota_locks;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000872 if (alloc_required) {
Steven Whitehouse2933f922006-11-01 13:23:29 -0500873 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000874 if (error)
875 goto fail_quota_locks;
876
877 al->al_requested = sdp->sd_max_dirres;
878
879 error = gfs2_inplace_reserve(dip);
880 if (error)
881 goto fail_quota_locks;
882
Steven Whitehouse320dd102006-05-18 16:25:27 -0400883 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100884 al->al_rgd->rd_length +
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400885 2 * RES_DINODE +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000886 RES_STATFS + RES_QUOTA, 0);
887 if (error)
888 goto fail_ipreserv;
889 } else {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400890 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000891 if (error)
892 goto fail_quota_locks;
893 }
894
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100895 error = gfs2_dir_add(&dip->i_inode, name, ip, IF2DT(ip->i_inode.i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000896 if (error)
897 goto fail_end_trans;
898
899 error = gfs2_meta_inode_buffer(ip, &dibh);
900 if (error)
901 goto fail_end_trans;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500902 ip->i_inode.i_nlink = 1;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000903 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500904 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000905 brelse(dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000906 return 0;
907
Steven Whitehouse320dd102006-05-18 16:25:27 -0400908fail_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000909 gfs2_trans_end(sdp);
910
Steven Whitehouse320dd102006-05-18 16:25:27 -0400911fail_ipreserv:
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000912 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000913 gfs2_inplace_release(dip);
914
Steven Whitehouse320dd102006-05-18 16:25:27 -0400915fail_quota_locks:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000916 gfs2_quota_unlock(dip);
917
Steven Whitehouse320dd102006-05-18 16:25:27 -0400918fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000919 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000920 return error;
921}
922
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -0400923static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip)
924{
925 int err;
926 size_t len;
927 void *value;
928 char *name;
929 struct gfs2_ea_request er;
930
931 err = security_inode_init_security(&ip->i_inode, &dip->i_inode,
932 &name, &value, &len);
933
934 if (err) {
935 if (err == -EOPNOTSUPP)
936 return 0;
937 return err;
938 }
939
940 memset(&er, 0, sizeof(struct gfs2_ea_request));
941
942 er.er_type = GFS2_EATYPE_SECURITY;
943 er.er_name = name;
944 er.er_data = value;
945 er.er_name_len = strlen(name);
946 er.er_data_len = len;
947
948 err = gfs2_ea_set_i(ip, &er);
949
950 kfree(value);
951 kfree(name);
952
953 return err;
954}
955
David Teiglandb3b94fa2006-01-16 16:50:04 +0000956/**
957 * gfs2_createi - Create a new inode
958 * @ghs: An array of two holders
959 * @name: The name of the new file
960 * @mode: the permissions on the new inode
961 *
962 * @ghs[0] is an initialized holder for the directory
963 * @ghs[1] is the holder for the inode lock
964 *
Steven Whitehouse7359a192006-02-13 12:27:43 +0000965 * If the return value is not NULL, the glocks on both the directory and the new
David Teiglandb3b94fa2006-01-16 16:50:04 +0000966 * file are held. A transaction has been started and an inplace reservation
967 * is held, as well.
968 *
Steven Whitehouse7359a192006-02-13 12:27:43 +0000969 * Returns: An inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000970 */
971
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400972struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500973 unsigned int mode, dev_t dev)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000974{
Steven Whitehousee1cc8602007-06-07 11:47:52 +0100975 struct inode *inode = NULL;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500976 struct gfs2_inode *dip = ghs->gh_gl->gl_object;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400977 struct inode *dir = &dip->i_inode;
978 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100979 struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000980 int error;
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400981 u64 generation;
Steven Whitehousef91a0d32007-10-15 16:29:05 +0100982 struct buffer_head *bh = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000983
984 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehouse7359a192006-02-13 12:27:43 +0000985 return ERR_PTR(-ENAMETOOLONG);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000986
David Teiglandb3b94fa2006-01-16 16:50:04 +0000987 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
988 error = gfs2_glock_nq(ghs);
989 if (error)
990 goto fail;
991
992 error = create_ok(dip, name, mode);
993 if (error)
994 goto fail_gunlock;
995
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400996 error = pick_formal_ino(sdp, &inum.no_formal_ino);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000997 if (error)
998 goto fail_gunlock;
999
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001000 error = alloc_dinode(dip, &inum.no_addr, &generation);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001001 if (error)
1002 goto fail_gunlock;
1003
Steven Whitehouse28626e22006-11-22 11:13:21 -05001004 error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
1005 LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
1006 if (error)
1007 goto fail_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001008
Wendy Chenge9bd2b32007-08-24 09:15:01 -04001009 error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001010 if (error)
1011 goto fail_gunlock2;
1012
Wendy Chengbb9bcf02007-06-27 17:07:08 -04001013 inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode),
1014 inum.no_addr,
Benjamin Marzinski7a9f53b2007-09-18 13:33:18 -05001015 inum.no_formal_ino, 0);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001016 if (IS_ERR(inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001017 goto fail_gunlock2;
1018
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001019 error = gfs2_inode_refresh(GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001020 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +01001021 goto fail_gunlock2;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001022
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001023 error = gfs2_acl_create(dip, GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001024 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +01001025 goto fail_gunlock2;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001026
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -04001027 error = gfs2_security_init(dip, GFS2_I(inode));
1028 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +01001029 goto fail_gunlock2;
Ryan O'Harafcb47e0b2006-10-03 11:57:35 -04001030
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001031 error = link_dinode(dip, name, GFS2_I(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001032 if (error)
Steven Whitehousee1cc8602007-06-07 11:47:52 +01001033 goto fail_gunlock2;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001034
Steven Whitehousef91a0d32007-10-15 16:29:05 +01001035 if (bh)
1036 brelse(bh);
Steven Whitehouse7359a192006-02-13 12:27:43 +00001037 return inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001038
Steven Whitehouse320dd102006-05-18 16:25:27 -04001039fail_gunlock2:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001040 gfs2_glock_dq_uninit(ghs + 1);
Julien Brunelbd1eb882008-09-01 10:51:22 +02001041 if (inode && !IS_ERR(inode))
Steven Whitehousee1cc8602007-06-07 11:47:52 +01001042 iput(inode);
Steven Whitehouse320dd102006-05-18 16:25:27 -04001043fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001044 gfs2_glock_dq(ghs);
Steven Whitehouse320dd102006-05-18 16:25:27 -04001045fail:
Steven Whitehousef91a0d32007-10-15 16:29:05 +01001046 if (bh)
1047 brelse(bh);
Steven Whitehouse7359a192006-02-13 12:27:43 +00001048 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001049}
1050
1051/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001052 * gfs2_rmdiri - Remove a directory
1053 * @dip: The parent directory of the directory to be removed
1054 * @name: The name of the directory to be removed
1055 * @ip: The GFS2 inode of the directory to be removed
1056 *
1057 * Assumes Glocks on dip and ip are held
1058 *
1059 * Returns: errno
1060 */
1061
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001062int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
1063 struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001064{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001065 struct qstr dotname;
1066 int error;
1067
Steven Whitehousead6203f2008-11-03 13:59:19 +00001068 if (ip->i_entries != 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001069 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -05001070 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001071 return -EIO;
1072 }
1073
1074 error = gfs2_dir_del(dip, name);
1075 if (error)
1076 return error;
1077
1078 error = gfs2_change_nlink(dip, -1);
1079 if (error)
1080 return error;
1081
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001082 gfs2_str2qstr(&dotname, ".");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001083 error = gfs2_dir_del(ip, &dotname);
1084 if (error)
1085 return error;
1086
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001087 gfs2_str2qstr(&dotname, "..");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001088 error = gfs2_dir_del(ip, &dotname);
1089 if (error)
1090 return error;
1091
Steven Whitehouse4f561102006-11-01 14:04:17 -05001092 /* It looks odd, but it really should be done twice */
1093 error = gfs2_change_nlink(ip, -1);
1094 if (error)
1095 return error;
1096
1097 error = gfs2_change_nlink(ip, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001098 if (error)
1099 return error;
1100
David Teiglandb3b94fa2006-01-16 16:50:04 +00001101 return error;
1102}
1103
1104/*
1105 * gfs2_unlink_ok - check to see that a inode is still in a directory
1106 * @dip: the directory
1107 * @name: the name of the file
1108 * @ip: the inode
1109 *
1110 * Assumes that the lock on (at least) @dip is held.
1111 *
1112 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
1113 */
1114
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001115int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001116 const struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001117{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001118 int error;
1119
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001120 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001121 return -EPERM;
1122
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001123 if ((dip->i_inode.i_mode & S_ISVTX) &&
David Howells3de7be32008-11-14 10:38:53 +11001124 dip->i_inode.i_uid != current_fsuid() &&
1125 ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001126 return -EPERM;
1127
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001128 if (IS_APPEND(&dip->i_inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001129 return -EPERM;
1130
Miklos Szeredif58ba882008-07-02 21:12:01 +02001131 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001132 if (error)
1133 return error;
1134
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001135 error = gfs2_dir_check(&dip->i_inode, name, ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001136 if (error)
1137 return error;
1138
David Teiglandb3b94fa2006-01-16 16:50:04 +00001139 return 0;
1140}
1141
David Teiglandb3b94fa2006-01-16 16:50:04 +00001142/**
1143 * gfs2_readlinki - return the contents of a symlink
1144 * @ip: the symlink's inode
1145 * @buf: a pointer to the buffer to be filled
1146 * @len: a pointer to the length of @buf
1147 *
1148 * If @buf is too small, a piece of memory is kmalloc()ed and needs
1149 * to be freed by the caller.
1150 *
1151 * Returns: errno
1152 */
1153
1154int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
1155{
1156 struct gfs2_holder i_gh;
1157 struct buffer_head *dibh;
1158 unsigned int x;
1159 int error;
1160
Steven Whitehouse719ee342008-09-18 13:53:59 +01001161 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
1162 error = gfs2_glock_nq(&i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001163 if (error) {
1164 gfs2_holder_uninit(&i_gh);
1165 return error;
1166 }
1167
Steven Whitehousec9e98882008-11-04 09:47:33 +00001168 if (!ip->i_disksize) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001169 gfs2_consist_inode(ip);
1170 error = -EIO;
1171 goto out;
1172 }
1173
1174 error = gfs2_meta_inode_buffer(ip, &dibh);
1175 if (error)
1176 goto out;
1177
Steven Whitehousec9e98882008-11-04 09:47:33 +00001178 x = ip->i_disksize + 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001179 if (x > *len) {
Josef Bacik16c5f062008-04-09 09:33:41 -04001180 *buf = kmalloc(x, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001181 if (!*buf) {
1182 error = -ENOMEM;
1183 goto out_brelse;
1184 }
1185 }
1186
1187 memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
1188 *len = x;
1189
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001190out_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001191 brelse(dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001192out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001193 gfs2_glock_dq_uninit(&i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001194 return error;
1195}
1196
David Teiglandb3b94fa2006-01-16 16:50:04 +00001197static int
1198__gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1199{
1200 struct buffer_head *dibh;
1201 int error;
1202
1203 error = gfs2_meta_inode_buffer(ip, &dibh);
1204 if (!error) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001205 error = inode_setattr(&ip->i_inode, attr);
1206 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001207 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001208 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001209 brelse(dibh);
1210 }
1211 return error;
1212}
1213
1214/**
1215 * gfs2_setattr_simple -
1216 * @ip:
1217 * @attr:
1218 *
1219 * Called with a reference on the vnode.
1220 *
1221 * Returns: errno
1222 */
1223
1224int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1225{
1226 int error;
1227
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001228 if (current->journal_info)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001229 return __gfs2_setattr_simple(ip, attr);
1230
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001231 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001232 if (error)
1233 return error;
1234
1235 error = __gfs2_setattr_simple(ip, attr);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001236 gfs2_trans_end(GFS2_SB(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001237 return error;
1238}
1239
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001240void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
1241{
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001242 struct gfs2_dinode *str = buf;
1243
1244 str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
1245 str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
1246 str->di_header.__pad0 = 0;
1247 str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
1248 str->di_header.__pad1 = 0;
1249 str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
1250 str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
1251 str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
1252 str->di_uid = cpu_to_be32(ip->i_inode.i_uid);
1253 str->di_gid = cpu_to_be32(ip->i_inode.i_gid);
1254 str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
Steven Whitehousec9e98882008-11-04 09:47:33 +00001255 str->di_size = cpu_to_be64(ip->i_disksize);
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001256 str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001257 str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
1258 str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
1259 str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
1260
Steven Whitehousece276b02008-02-06 09:25:45 +00001261 str->di_goal_meta = cpu_to_be64(ip->i_goal);
1262 str->di_goal_data = cpu_to_be64(ip->i_goal);
Steven Whitehousebcf0b5b2008-11-03 13:39:46 +00001263 str->di_generation = cpu_to_be64(ip->i_generation);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001264
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001265 str->di_flags = cpu_to_be32(ip->i_diskflags);
Steven Whitehouseecc30c72008-01-28 10:37:35 +00001266 str->di_height = cpu_to_be16(ip->i_height);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001267 str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001268 !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001269 GFS2_FORMAT_DE : 0);
Steven Whitehouse9a004502008-02-01 09:23:44 +00001270 str->di_depth = cpu_to_be16(ip->i_depth);
Steven Whitehousead6203f2008-11-03 13:59:19 +00001271 str->di_entries = cpu_to_be32(ip->i_entries);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001272
Steven Whitehouse3767ac22008-11-03 14:28:42 +00001273 str->di_eattr = cpu_to_be64(ip->i_eattr);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001274 str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
1275 str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
1276 str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001277}
1278
1279void gfs2_dinode_print(const struct gfs2_inode *ip)
1280{
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001281 printk(KERN_INFO " no_formal_ino = %llu\n",
1282 (unsigned long long)ip->i_no_formal_ino);
1283 printk(KERN_INFO " no_addr = %llu\n",
1284 (unsigned long long)ip->i_no_addr);
Steven Whitehousec9e98882008-11-04 09:47:33 +00001285 printk(KERN_INFO " i_disksize = %llu\n",
1286 (unsigned long long)ip->i_disksize);
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001287 printk(KERN_INFO " blocks = %llu\n",
1288 (unsigned long long)gfs2_get_inode_blocks(&ip->i_inode));
Steven Whitehousece276b02008-02-06 09:25:45 +00001289 printk(KERN_INFO " i_goal = %llu\n",
1290 (unsigned long long)ip->i_goal);
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001291 printk(KERN_INFO " i_diskflags = 0x%.8X\n", ip->i_diskflags);
Bob Petersonca390602008-01-28 11:15:57 -06001292 printk(KERN_INFO " i_height = %u\n", ip->i_height);
Steven Whitehouse9a004502008-02-01 09:23:44 +00001293 printk(KERN_INFO " i_depth = %u\n", ip->i_depth);
Steven Whitehousead6203f2008-11-03 13:59:19 +00001294 printk(KERN_INFO " i_entries = %u\n", ip->i_entries);
Steven Whitehouse3767ac22008-11-03 14:28:42 +00001295 printk(KERN_INFO " i_eattr = %llu\n",
1296 (unsigned long long)ip->i_eattr);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001297}
1298