eCryptfs: Fix lockdep warning in miscdev operations
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / xfs / xfs_utils.c
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
1da177e4 13 *
7b718769
NS
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1da177e4 17 */
1da177e4 18#include "xfs.h"
a844f451 19#include "xfs_fs.h"
1da177e4 20#include "xfs_types.h"
1da177e4
LT
21#include "xfs_log.h"
22#include "xfs_trans.h"
23#include "xfs_sb.h"
a844f451 24#include "xfs_ag.h"
1da177e4 25#include "xfs_dir2.h"
1da177e4
LT
26#include "xfs_mount.h"
27#include "xfs_bmap_btree.h"
1da177e4 28#include "xfs_dinode.h"
1da177e4 29#include "xfs_inode.h"
a844f451 30#include "xfs_inode_item.h"
1da177e4
LT
31#include "xfs_bmap.h"
32#include "xfs_error.h"
33#include "xfs_quota.h"
1da177e4
LT
34#include "xfs_itable.h"
35#include "xfs_utils.h"
36
1da177e4 37
1da177e4
LT
38/*
39 * Allocates a new inode from disk and return a pointer to the
40 * incore copy. This routine will internally commit the current
41 * transaction and allocate a new one if the Space Manager needed
42 * to do an allocation to replenish the inode free-list.
43 *
44 * This routine is designed to be called from xfs_create and
45 * xfs_create_dir.
46 *
47 */
48int
49xfs_dir_ialloc(
50 xfs_trans_t **tpp, /* input: current transaction;
51 output: may be a new transaction. */
52 xfs_inode_t *dp, /* directory within whose allocate
53 the inode. */
576b1d67 54 umode_t mode,
31b084ae 55 xfs_nlink_t nlink,
1da177e4 56 xfs_dev_t rdev,
1da177e4
LT
57 prid_t prid, /* project id */
58 int okalloc, /* ok to allocate new space */
59 xfs_inode_t **ipp, /* pointer to inode; it will be
60 locked. */
61 int *committed)
62
63{
64 xfs_trans_t *tp;
65 xfs_trans_t *ntp;
66 xfs_inode_t *ip;
67 xfs_buf_t *ialloc_context = NULL;
68 boolean_t call_again = B_FALSE;
69 int code;
70 uint log_res;
71 uint log_count;
72 void *dqinfo;
73 uint tflags;
74
75 tp = *tpp;
76 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
77
78 /*
79 * xfs_ialloc will return a pointer to an incore inode if
80 * the Space Manager has an available inode on the free
81 * list. Otherwise, it will do an allocation and replenish
82 * the freelist. Since we can only do one allocation per
83 * transaction without deadlocks, we will need to commit the
84 * current transaction and start a new one. We will then
85 * need to call xfs_ialloc again to get the inode.
86 *
87 * If xfs_ialloc did an allocation to replenish the freelist,
88 * it returns the bp containing the head of the freelist as
89 * ialloc_context. We will hold a lock on it across the
90 * transaction commit so that no other process can steal
91 * the inode(s) that we've just allocated.
92 */
6c77b0ea 93 code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, okalloc,
1da177e4
LT
94 &ialloc_context, &call_again, &ip);
95
96 /*
97 * Return an error if we were unable to allocate a new inode.
98 * This should only happen if we run out of space on disk or
99 * encounter a disk error.
100 */
101 if (code) {
102 *ipp = NULL;
103 return code;
104 }
105 if (!call_again && (ip == NULL)) {
106 *ipp = NULL;
107 return XFS_ERROR(ENOSPC);
108 }
109
110 /*
111 * If call_again is set, then we were unable to get an
112 * inode in one operation. We need to commit the current
113 * transaction and call xfs_ialloc() again. It is guaranteed
114 * to succeed the second time.
115 */
116 if (call_again) {
117
118 /*
119 * Normally, xfs_trans_commit releases all the locks.
120 * We call bhold to hang on to the ialloc_context across
121 * the commit. Holding this buffer prevents any other
122 * processes from doing any allocations in this
123 * allocation group.
124 */
125 xfs_trans_bhold(tp, ialloc_context);
126 /*
127 * Save the log reservation so we can use
128 * them in the next transaction.
129 */
130 log_res = xfs_trans_get_log_res(tp);
131 log_count = xfs_trans_get_log_count(tp);
132
133 /*
134 * We want the quota changes to be associated with the next
135 * transaction, NOT this one. So, detach the dqinfo from this
136 * and attach it to the next transaction.
137 */
138 dqinfo = NULL;
139 tflags = 0;
140 if (tp->t_dqinfo) {
141 dqinfo = (void *)tp->t_dqinfo;
142 tp->t_dqinfo = NULL;
143 tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
144 tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
145 }
146
147 ntp = xfs_trans_dup(tp);
1c72bf90 148 code = xfs_trans_commit(tp, 0);
1da177e4
LT
149 tp = ntp;
150 if (committed != NULL) {
151 *committed = 1;
152 }
153 /*
154 * If we get an error during the commit processing,
155 * release the buffer that is still held and return
156 * to the caller.
157 */
158 if (code) {
159 xfs_buf_relse(ialloc_context);
160 if (dqinfo) {
161 tp->t_dqinfo = dqinfo;
7d095257 162 xfs_trans_free_dqinfo(tp);
1da177e4
LT
163 }
164 *tpp = ntp;
165 *ipp = NULL;
166 return code;
167 }
cc09c0dc
DC
168
169 /*
170 * transaction commit worked ok so we can drop the extra ticket
171 * reference that we gained in xfs_trans_dup()
172 */
173 xfs_log_ticket_put(tp->t_ticket);
1da177e4
LT
174 code = xfs_trans_reserve(tp, 0, log_res, 0,
175 XFS_TRANS_PERM_LOG_RES, log_count);
176 /*
177 * Re-attach the quota info that we detached from prev trx.
178 */
179 if (dqinfo) {
180 tp->t_dqinfo = dqinfo;
181 tp->t_flags |= tflags;
182 }
183
184 if (code) {
185 xfs_buf_relse(ialloc_context);
186 *tpp = ntp;
187 *ipp = NULL;
188 return code;
189 }
190 xfs_trans_bjoin(tp, ialloc_context);
191
192 /*
193 * Call ialloc again. Since we've locked out all
194 * other allocations in this allocation group,
195 * this call should always succeed.
196 */
6c77b0ea 197 code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid,
1da177e4
LT
198 okalloc, &ialloc_context, &call_again, &ip);
199
200 /*
201 * If we get an error at this point, return to the caller
202 * so that the current transaction can be aborted.
203 */
204 if (code) {
205 *tpp = tp;
206 *ipp = NULL;
207 return code;
208 }
209 ASSERT ((!call_again) && (ip != NULL));
210
211 } else {
212 if (committed != NULL) {
213 *committed = 0;
214 }
215 }
216
217 *ipp = ip;
218 *tpp = tp;
219
220 return 0;
221}
222
223/*
224 * Decrement the link count on an inode & log the change.
225 * If this causes the link count to go to zero, initiate the
226 * logging activity required to truncate a file.
227 */
228int /* error */
229xfs_droplink(
230 xfs_trans_t *tp,
231 xfs_inode_t *ip)
232{
233 int error;
234
dcd79a14 235 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1da177e4
LT
236
237 ASSERT (ip->i_d.di_nlink > 0);
238 ip->i_d.di_nlink--;
01651646 239 drop_nlink(VFS_I(ip));
1da177e4
LT
240 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
241
242 error = 0;
243 if (ip->i_d.di_nlink == 0) {
244 /*
245 * We're dropping the last link to this file.
246 * Move the on-disk inode to the AGI unlinked list.
247 * From xfs_inactive() we will pull the inode from
248 * the list and free it.
249 */
250 error = xfs_iunlink(tp, ip);
251 }
252 return error;
253}
254
255/*
256 * This gets called when the inode's version needs to be changed from 1 to 2.
257 * Currently this happens when the nlink field overflows the old 16-bit value
258 * or when chproj is called to change the project for the first time.
259 * As a side effect the superblock version will also get rev'd
260 * to contain the NLINK bit.
261 */
262void
263xfs_bump_ino_vers2(
264 xfs_trans_t *tp,
265 xfs_inode_t *ip)
266{
267 xfs_mount_t *mp;
1da177e4 268
579aa9ca 269 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
51ce16d5 270 ASSERT(ip->i_d.di_version == 1);
1da177e4 271
51ce16d5 272 ip->i_d.di_version = 2;
1da177e4
LT
273 ip->i_d.di_onlink = 0;
274 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
275 mp = tp->t_mountp;
62118709 276 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
3685c2a1 277 spin_lock(&mp->m_sb_lock);
62118709
ES
278 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
279 xfs_sb_version_addnlink(&mp->m_sb);
3685c2a1 280 spin_unlock(&mp->m_sb_lock);
1da177e4
LT
281 xfs_mod_sb(tp, XFS_SB_VERSIONNUM);
282 } else {
3685c2a1 283 spin_unlock(&mp->m_sb_lock);
1da177e4
LT
284 }
285 }
286 /* Caller must log the inode */
287}
288
289/*
290 * Increment the link count on an inode & log the change.
291 */
292int
293xfs_bumplink(
294 xfs_trans_t *tp,
295 xfs_inode_t *ip)
296{
dcd79a14 297 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1da177e4
LT
298
299 ASSERT(ip->i_d.di_nlink > 0);
300 ip->i_d.di_nlink++;
01651646 301 inc_nlink(VFS_I(ip));
51ce16d5 302 if ((ip->i_d.di_version == 1) &&
1da177e4
LT
303 (ip->i_d.di_nlink > XFS_MAXLINK_1)) {
304 /*
305 * The inode has increased its number of links beyond
306 * what can fit in an old format inode. It now needs
307 * to be converted to a version 2 inode with a 32 bit
308 * link count. If this is the first inode in the file
309 * system to do this, then we need to bump the superblock
310 * version number as well.
311 */
312 xfs_bump_ino_vers2(tp, ip);
313 }
314
315 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
316 return 0;
317}