inode.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/posix_acl.h>
  15. #include <linux/sort.h>
  16. #include <linux/gfs2_ondisk.h>
  17. #include <linux/crc32.h>
  18. #include <linux/lm_interface.h>
  19. #include <linux/security.h>
  20. #include "gfs2.h"
  21. #include "incore.h"
  22. #include "acl.h"
  23. #include "bmap.h"
  24. #include "dir.h"
  25. #include "eattr.h"
  26. #include "glock.h"
  27. #include "glops.h"
  28. #include "inode.h"
  29. #include "log.h"
  30. #include "meta_io.h"
  31. #include "ops_address.h"
  32. #include "ops_file.h"
  33. #include "ops_inode.h"
  34. #include "quota.h"
  35. #include "rgrp.h"
  36. #include "trans.h"
  37. #include "util.h"
  38. static int iget_test(struct inode *inode, void *opaque)
  39. {
  40. struct gfs2_inode *ip = GFS2_I(inode);
  41. struct gfs2_inum_host *inum = opaque;
  42. if (ip->i_num.no_addr == inum->no_addr)
  43. return 1;
  44. return 0;
  45. }
  46. static int iget_set(struct inode *inode, void *opaque)
  47. {
  48. struct gfs2_inode *ip = GFS2_I(inode);
  49. struct gfs2_inum_host *inum = opaque;
  50. ip->i_num = *inum;
  51. inode->i_ino = inum->no_addr;
  52. return 0;
  53. }
  54. struct inode *gfs2_ilookup(struct super_block *sb, struct gfs2_inum_host *inum)
  55. {
  56. return ilookup5(sb, (unsigned long)inum->no_formal_ino,
  57. iget_test, inum);
  58. }
  59. static struct inode *gfs2_iget(struct super_block *sb, struct gfs2_inum_host *inum)
  60. {
  61. return iget5_locked(sb, (unsigned long)inum->no_formal_ino,
  62. iget_test, iget_set, inum);
  63. }
  64. /**
  65. * gfs2_inode_lookup - Lookup an inode
  66. * @sb: The super block
  67. * @inum: The inode number
  68. * @type: The type of the inode
  69. *
  70. * Returns: A VFS inode, or an error
  71. */
  72. struct inode *gfs2_inode_lookup(struct super_block *sb, struct gfs2_inum_host *inum, unsigned int type)
  73. {
  74. struct inode *inode = gfs2_iget(sb, inum);
  75. struct gfs2_inode *ip = GFS2_I(inode);
  76. struct gfs2_glock *io_gl;
  77. int error;
  78. if (!inode)
  79. return ERR_PTR(-ENOBUFS);
  80. if (inode->i_state & I_NEW) {
  81. struct gfs2_sbd *sdp = GFS2_SB(inode);
  82. umode_t mode = DT2IF(type);
  83. inode->i_private = ip;
  84. inode->i_mode = mode;
  85. if (S_ISREG(mode)) {
  86. inode->i_op = &gfs2_file_iops;
  87. inode->i_fop = &gfs2_file_fops;
  88. inode->i_mapping->a_ops = &gfs2_file_aops;
  89. } else if (S_ISDIR(mode)) {
  90. inode->i_op = &gfs2_dir_iops;
  91. inode->i_fop = &gfs2_dir_fops;
  92. } else if (S_ISLNK(mode)) {
  93. inode->i_op = &gfs2_symlink_iops;
  94. } else {
  95. inode->i_op = &gfs2_dev_iops;
  96. }
  97. error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  98. if (unlikely(error))
  99. goto fail;
  100. ip->i_gl->gl_object = ip;
  101. error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  102. if (unlikely(error))
  103. goto fail_put;
  104. set_bit(GIF_INVALID, &ip->i_flags);
  105. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
  106. if (unlikely(error))
  107. goto fail_iopen;
  108. gfs2_glock_put(io_gl);
  109. unlock_new_inode(inode);
  110. }
  111. return inode;
  112. fail_iopen:
  113. gfs2_glock_put(io_gl);
  114. fail_put:
  115. ip->i_gl->gl_object = NULL;
  116. gfs2_glock_put(ip->i_gl);
  117. fail:
  118. iput(inode);
  119. return ERR_PTR(error);
  120. }
  121. static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
  122. {
  123. struct gfs2_dinode_host *di = &ip->i_di;
  124. const struct gfs2_dinode *str = buf;
  125. if (ip->i_num.no_addr != be64_to_cpu(str->di_num.no_addr)) {
  126. if (gfs2_consist_inode(ip))
  127. gfs2_dinode_print(ip);
  128. return -EIO;
  129. }
  130. if (ip->i_num.no_formal_ino != be64_to_cpu(str->di_num.no_formal_ino))
  131. return -ESTALE;
  132. ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
  133. ip->i_inode.i_rdev = 0;
  134. switch (ip->i_inode.i_mode & S_IFMT) {
  135. case S_IFBLK:
  136. case S_IFCHR:
  137. ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
  138. be32_to_cpu(str->di_minor));
  139. break;
  140. };
  141. ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
  142. ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
  143. /*
  144. * We will need to review setting the nlink count here in the
  145. * light of the forthcoming ro bind mount work. This is a reminder
  146. * to do that.
  147. */
  148. ip->i_inode.i_nlink = be32_to_cpu(str->di_nlink);
  149. di->di_size = be64_to_cpu(str->di_size);
  150. i_size_write(&ip->i_inode, di->di_size);
  151. di->di_blocks = be64_to_cpu(str->di_blocks);
  152. gfs2_set_inode_blocks(&ip->i_inode);
  153. ip->i_inode.i_atime.tv_sec = be64_to_cpu(str->di_atime);
  154. ip->i_inode.i_atime.tv_nsec = 0;
  155. ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
  156. ip->i_inode.i_mtime.tv_nsec = 0;
  157. ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
  158. ip->i_inode.i_ctime.tv_nsec = 0;
  159. di->di_goal_meta = be64_to_cpu(str->di_goal_meta);
  160. di->di_goal_data = be64_to_cpu(str->di_goal_data);
  161. di->di_generation = be64_to_cpu(str->di_generation);
  162. di->di_flags = be32_to_cpu(str->di_flags);
  163. gfs2_set_inode_flags(&ip->i_inode);
  164. di->di_height = be16_to_cpu(str->di_height);
  165. di->di_depth = be16_to_cpu(str->di_depth);
  166. di->di_entries = be32_to_cpu(str->di_entries);
  167. di->di_eattr = be64_to_cpu(str->di_eattr);
  168. return 0;
  169. }
  170. /**
  171. * gfs2_inode_refresh - Refresh the incore copy of the dinode
  172. * @ip: The GFS2 inode
  173. *
  174. * Returns: errno
  175. */
  176. int gfs2_inode_refresh(struct gfs2_inode *ip)
  177. {
  178. struct buffer_head *dibh;
  179. int error;
  180. error = gfs2_meta_inode_buffer(ip, &dibh);
  181. if (error)
  182. return error;
  183. if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
  184. brelse(dibh);
  185. return -EIO;
  186. }
  187. error = gfs2_dinode_in(ip, dibh->b_data);
  188. brelse(dibh);
  189. clear_bit(GIF_INVALID, &ip->i_flags);
  190. return error;
  191. }
  192. int gfs2_dinode_dealloc(struct gfs2_inode *ip)
  193. {
  194. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  195. struct gfs2_alloc *al;
  196. struct gfs2_rgrpd *rgd;
  197. int error;
  198. if (ip->i_di.di_blocks != 1) {
  199. if (gfs2_consist_inode(ip))
  200. gfs2_dinode_print(ip);
  201. return -EIO;
  202. }
  203. al = gfs2_alloc_get(ip);
  204. error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  205. if (error)
  206. goto out;
  207. error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
  208. if (error)
  209. goto out_qs;
  210. rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
  211. if (!rgd) {
  212. gfs2_consist_inode(ip);
  213. error = -EIO;
  214. goto out_rindex_relse;
  215. }
  216. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
  217. &al->al_rgd_gh);
  218. if (error)
  219. goto out_rindex_relse;
  220. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
  221. if (error)
  222. goto out_rg_gunlock;
  223. gfs2_trans_add_gl(ip->i_gl);
  224. gfs2_free_di(rgd, ip);
  225. gfs2_trans_end(sdp);
  226. clear_bit(GLF_STICKY, &ip->i_gl->gl_flags);
  227. out_rg_gunlock:
  228. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  229. out_rindex_relse:
  230. gfs2_glock_dq_uninit(&al->al_ri_gh);
  231. out_qs:
  232. gfs2_quota_unhold(ip);
  233. out:
  234. gfs2_alloc_put(ip);
  235. return error;
  236. }
  237. /**
  238. * gfs2_change_nlink - Change nlink count on inode
  239. * @ip: The GFS2 inode
  240. * @diff: The change in the nlink count required
  241. *
  242. * Returns: errno
  243. */
  244. int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
  245. {
  246. struct gfs2_sbd *sdp = ip->i_inode.i_sb->s_fs_info;
  247. struct buffer_head *dibh;
  248. u32 nlink;
  249. int error;
  250. BUG_ON(diff != 1 && diff != -1);
  251. nlink = ip->i_inode.i_nlink + diff;
  252. /* If we are reducing the nlink count, but the new value ends up being
  253. bigger than the old one, we must have underflowed. */
  254. if (diff < 0 && nlink > ip->i_inode.i_nlink) {
  255. if (gfs2_consist_inode(ip))
  256. gfs2_dinode_print(ip);
  257. return -EIO;
  258. }
  259. error = gfs2_meta_inode_buffer(ip, &dibh);
  260. if (error)
  261. return error;
  262. if (diff > 0)
  263. inc_nlink(&ip->i_inode);
  264. else
  265. drop_nlink(&ip->i_inode);
  266. ip->i_inode.i_ctime.tv_sec = get_seconds();
  267. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  268. gfs2_dinode_out(ip, dibh->b_data);
  269. brelse(dibh);
  270. mark_inode_dirty(&ip->i_inode);
  271. if (ip->i_inode.i_nlink == 0) {
  272. struct gfs2_rgrpd *rgd;
  273. struct gfs2_holder ri_gh, rg_gh;
  274. error = gfs2_rindex_hold(sdp, &ri_gh);
  275. if (error)
  276. goto out;
  277. error = -EIO;
  278. rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
  279. if (!rgd)
  280. goto out_norgrp;
  281. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh);
  282. if (error)
  283. goto out_norgrp;
  284. gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
  285. gfs2_glock_dq_uninit(&rg_gh);
  286. out_norgrp:
  287. gfs2_glock_dq_uninit(&ri_gh);
  288. }
  289. out:
  290. return error;
  291. }
  292. struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
  293. {
  294. struct qstr qstr;
  295. gfs2_str2qstr(&qstr, name);
  296. return gfs2_lookupi(dip, &qstr, 1, NULL);
  297. }
  298. /**
  299. * gfs2_lookupi - Look up a filename in a directory and return its inode
  300. * @d_gh: An initialized holder for the directory glock
  301. * @name: The name of the inode to look for
  302. * @is_root: If 1, ignore the caller's permissions
  303. * @i_gh: An uninitialized holder for the new inode glock
  304. *
  305. * There will always be a vnode (Linux VFS inode) for the d_gh inode unless
  306. * @is_root is true.
  307. *
  308. * Returns: errno
  309. */
  310. struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
  311. int is_root, struct nameidata *nd)
  312. {
  313. struct super_block *sb = dir->i_sb;
  314. struct gfs2_inode *dip = GFS2_I(dir);
  315. struct gfs2_holder d_gh;
  316. struct gfs2_inum_host inum;
  317. unsigned int type;
  318. int error = 0;
  319. struct inode *inode = NULL;
  320. if (!name->len || name->len > GFS2_FNAMESIZE)
  321. return ERR_PTR(-ENAMETOOLONG);
  322. if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
  323. (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
  324. dir == sb->s_root->d_inode)) {
  325. igrab(dir);
  326. return dir;
  327. }
  328. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  329. if (error)
  330. return ERR_PTR(error);
  331. if (!is_root) {
  332. error = permission(dir, MAY_EXEC, NULL);
  333. if (error)
  334. goto out;
  335. }
  336. error = gfs2_dir_search(dir, name, &inum, &type);
  337. if (error)
  338. goto out;
  339. inode = gfs2_inode_lookup(sb, &inum, type);
  340. out:
  341. gfs2_glock_dq_uninit(&d_gh);
  342. if (error == -ENOENT)
  343. return NULL;
  344. return inode;
  345. }
  346. static int pick_formal_ino_1(struct gfs2_sbd *sdp, u64 *formal_ino)
  347. {
  348. struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
  349. struct buffer_head *bh;
  350. struct gfs2_inum_range_host ir;
  351. int error;
  352. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  353. if (error)
  354. return error;
  355. mutex_lock(&sdp->sd_inum_mutex);
  356. error = gfs2_meta_inode_buffer(ip, &bh);
  357. if (error) {
  358. mutex_unlock(&sdp->sd_inum_mutex);
  359. gfs2_trans_end(sdp);
  360. return error;
  361. }
  362. gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
  363. if (ir.ir_length) {
  364. *formal_ino = ir.ir_start++;
  365. ir.ir_length--;
  366. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  367. gfs2_inum_range_out(&ir,
  368. bh->b_data + sizeof(struct gfs2_dinode));
  369. brelse(bh);
  370. mutex_unlock(&sdp->sd_inum_mutex);
  371. gfs2_trans_end(sdp);
  372. return 0;
  373. }
  374. brelse(bh);
  375. mutex_unlock(&sdp->sd_inum_mutex);
  376. gfs2_trans_end(sdp);
  377. return 1;
  378. }
  379. static int pick_formal_ino_2(struct gfs2_sbd *sdp, u64 *formal_ino)
  380. {
  381. struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
  382. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_inum_inode);
  383. struct gfs2_holder gh;
  384. struct buffer_head *bh;
  385. struct gfs2_inum_range_host ir;
  386. int error;
  387. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  388. if (error)
  389. return error;
  390. error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
  391. if (error)
  392. goto out;
  393. mutex_lock(&sdp->sd_inum_mutex);
  394. error = gfs2_meta_inode_buffer(ip, &bh);
  395. if (error)
  396. goto out_end_trans;
  397. gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
  398. if (!ir.ir_length) {
  399. struct buffer_head *m_bh;
  400. u64 x, y;
  401. __be64 z;
  402. error = gfs2_meta_inode_buffer(m_ip, &m_bh);
  403. if (error)
  404. goto out_brelse;
  405. z = *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode));
  406. x = y = be64_to_cpu(z);
  407. ir.ir_start = x;
  408. ir.ir_length = GFS2_INUM_QUANTUM;
  409. x += GFS2_INUM_QUANTUM;
  410. if (x < y)
  411. gfs2_consist_inode(m_ip);
  412. z = cpu_to_be64(x);
  413. gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
  414. *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = z;
  415. brelse(m_bh);
  416. }
  417. *formal_ino = ir.ir_start++;
  418. ir.ir_length--;
  419. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  420. gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode));
  421. out_brelse:
  422. brelse(bh);
  423. out_end_trans:
  424. mutex_unlock(&sdp->sd_inum_mutex);
  425. gfs2_trans_end(sdp);
  426. out:
  427. gfs2_glock_dq_uninit(&gh);
  428. return error;
  429. }
  430. static int pick_formal_ino(struct gfs2_sbd *sdp, u64 *inum)
  431. {
  432. int error;
  433. error = pick_formal_ino_1(sdp, inum);
  434. if (error <= 0)
  435. return error;
  436. error = pick_formal_ino_2(sdp, inum);
  437. return error;
  438. }
  439. /**
  440. * create_ok - OK to create a new on-disk inode here?
  441. * @dip: Directory in which dinode is to be created
  442. * @name: Name of new dinode
  443. * @mode:
  444. *
  445. * Returns: errno
  446. */
  447. static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
  448. unsigned int mode)
  449. {
  450. int error;
  451. error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
  452. if (error)
  453. return error;
  454. /* Don't create entries in an unlinked directory */
  455. if (!dip->i_inode.i_nlink)
  456. return -EPERM;
  457. error = gfs2_dir_search(&dip->i_inode, name, NULL, NULL);
  458. switch (error) {
  459. case -ENOENT:
  460. error = 0;
  461. break;
  462. case 0:
  463. return -EEXIST;
  464. default:
  465. return error;
  466. }
  467. if (dip->i_di.di_entries == (u32)-1)
  468. return -EFBIG;
  469. if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
  470. return -EMLINK;
  471. return 0;
  472. }
  473. static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
  474. unsigned int *uid, unsigned int *gid)
  475. {
  476. if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
  477. (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
  478. if (S_ISDIR(*mode))
  479. *mode |= S_ISUID;
  480. else if (dip->i_inode.i_uid != current->fsuid)
  481. *mode &= ~07111;
  482. *uid = dip->i_inode.i_uid;
  483. } else
  484. *uid = current->fsuid;
  485. if (dip->i_inode.i_mode & S_ISGID) {
  486. if (S_ISDIR(*mode))
  487. *mode |= S_ISGID;
  488. *gid = dip->i_inode.i_gid;
  489. } else
  490. *gid = current->fsgid;
  491. }
  492. static int alloc_dinode(struct gfs2_inode *dip, struct gfs2_inum_host *inum,
  493. u64 *generation)
  494. {
  495. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  496. int error;
  497. gfs2_alloc_get(dip);
  498. dip->i_alloc.al_requested = RES_DINODE;
  499. error = gfs2_inplace_reserve(dip);
  500. if (error)
  501. goto out;
  502. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
  503. if (error)
  504. goto out_ipreserv;
  505. inum->no_addr = gfs2_alloc_di(dip, generation);
  506. gfs2_trans_end(sdp);
  507. out_ipreserv:
  508. gfs2_inplace_release(dip);
  509. out:
  510. gfs2_alloc_put(dip);
  511. return error;
  512. }
  513. /**
  514. * init_dinode - Fill in a new dinode structure
  515. * @dip: the directory this inode is being created in
  516. * @gl: The glock covering the new inode
  517. * @inum: the inode number
  518. * @mode: the file permissions
  519. * @uid:
  520. * @gid:
  521. *
  522. */
  523. static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  524. const struct gfs2_inum_host *inum, unsigned int mode,
  525. unsigned int uid, unsigned int gid,
  526. const u64 *generation, dev_t dev)
  527. {
  528. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  529. struct gfs2_dinode *di;
  530. struct buffer_head *dibh;
  531. dibh = gfs2_meta_new(gl, inum->no_addr);
  532. gfs2_trans_add_bh(gl, dibh, 1);
  533. gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
  534. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  535. di = (struct gfs2_dinode *)dibh->b_data;
  536. di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
  537. di->di_num.no_addr = cpu_to_be64(inum->no_addr);
  538. di->di_mode = cpu_to_be32(mode);
  539. di->di_uid = cpu_to_be32(uid);
  540. di->di_gid = cpu_to_be32(gid);
  541. di->di_nlink = 0;
  542. di->di_size = 0;
  543. di->di_blocks = cpu_to_be64(1);
  544. di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(get_seconds());
  545. di->di_major = cpu_to_be32(MAJOR(dev));
  546. di->di_minor = cpu_to_be32(MINOR(dev));
  547. di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
  548. di->di_generation = cpu_to_be64(*generation);
  549. di->di_flags = 0;
  550. if (S_ISREG(mode)) {
  551. if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA) ||
  552. gfs2_tune_get(sdp, gt_new_files_jdata))
  553. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  554. if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_DIRECTIO) ||
  555. gfs2_tune_get(sdp, gt_new_files_directio))
  556. di->di_flags |= cpu_to_be32(GFS2_DIF_DIRECTIO);
  557. } else if (S_ISDIR(mode)) {
  558. di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
  559. GFS2_DIF_INHERIT_DIRECTIO);
  560. di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
  561. GFS2_DIF_INHERIT_JDATA);
  562. }
  563. di->__pad1 = 0;
  564. di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
  565. di->di_height = 0;
  566. di->__pad2 = 0;
  567. di->__pad3 = 0;
  568. di->di_depth = 0;
  569. di->di_entries = 0;
  570. memset(&di->__pad4, 0, sizeof(di->__pad4));
  571. di->di_eattr = 0;
  572. memset(&di->di_reserved, 0, sizeof(di->di_reserved));
  573. brelse(dibh);
  574. }
  575. static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  576. unsigned int mode, const struct gfs2_inum_host *inum,
  577. const u64 *generation, dev_t dev)
  578. {
  579. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  580. unsigned int uid, gid;
  581. int error;
  582. munge_mode_uid_gid(dip, &mode, &uid, &gid);
  583. gfs2_alloc_get(dip);
  584. error = gfs2_quota_lock(dip, uid, gid);
  585. if (error)
  586. goto out;
  587. error = gfs2_quota_check(dip, uid, gid);
  588. if (error)
  589. goto out_quota;
  590. error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
  591. if (error)
  592. goto out_quota;
  593. init_dinode(dip, gl, inum, mode, uid, gid, generation, dev);
  594. gfs2_quota_change(dip, +1, uid, gid);
  595. gfs2_trans_end(sdp);
  596. out_quota:
  597. gfs2_quota_unlock(dip);
  598. out:
  599. gfs2_alloc_put(dip);
  600. return error;
  601. }
  602. static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
  603. struct gfs2_inode *ip)
  604. {
  605. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  606. struct gfs2_alloc *al;
  607. int alloc_required;
  608. struct buffer_head *dibh;
  609. int error;
  610. al = gfs2_alloc_get(dip);
  611. error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  612. if (error)
  613. goto fail;
  614. error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
  615. if (alloc_required < 0)
  616. goto fail;
  617. if (alloc_required) {
  618. error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
  619. if (error)
  620. goto fail_quota_locks;
  621. al->al_requested = sdp->sd_max_dirres;
  622. error = gfs2_inplace_reserve(dip);
  623. if (error)
  624. goto fail_quota_locks;
  625. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  626. al->al_rgd->rd_ri.ri_length +
  627. 2 * RES_DINODE +
  628. RES_STATFS + RES_QUOTA, 0);
  629. if (error)
  630. goto fail_ipreserv;
  631. } else {
  632. error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
  633. if (error)
  634. goto fail_quota_locks;
  635. }
  636. error = gfs2_dir_add(&dip->i_inode, name, &ip->i_num, IF2DT(ip->i_inode.i_mode));
  637. if (error)
  638. goto fail_end_trans;
  639. error = gfs2_meta_inode_buffer(ip, &dibh);
  640. if (error)
  641. goto fail_end_trans;
  642. ip->i_inode.i_nlink = 1;
  643. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  644. gfs2_dinode_out(ip, dibh->b_data);
  645. brelse(dibh);
  646. return 0;
  647. fail_end_trans:
  648. gfs2_trans_end(sdp);
  649. fail_ipreserv:
  650. if (dip->i_alloc.al_rgd)
  651. gfs2_inplace_release(dip);
  652. fail_quota_locks:
  653. gfs2_quota_unlock(dip);
  654. fail:
  655. gfs2_alloc_put(dip);
  656. return error;
  657. }
  658. static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip)
  659. {
  660. int err;
  661. size_t len;
  662. void *value;
  663. char *name;
  664. struct gfs2_ea_request er;
  665. err = security_inode_init_security(&ip->i_inode, &dip->i_inode,
  666. &name, &value, &len);
  667. if (err) {
  668. if (err == -EOPNOTSUPP)
  669. return 0;
  670. return err;
  671. }
  672. memset(&er, 0, sizeof(struct gfs2_ea_request));
  673. er.er_type = GFS2_EATYPE_SECURITY;
  674. er.er_name = name;
  675. er.er_data = value;
  676. er.er_name_len = strlen(name);
  677. er.er_data_len = len;
  678. err = gfs2_ea_set_i(ip, &er);
  679. kfree(value);
  680. kfree(name);
  681. return err;
  682. }
  683. /**
  684. * gfs2_createi - Create a new inode
  685. * @ghs: An array of two holders
  686. * @name: The name of the new file
  687. * @mode: the permissions on the new inode
  688. *
  689. * @ghs[0] is an initialized holder for the directory
  690. * @ghs[1] is the holder for the inode lock
  691. *
  692. * If the return value is not NULL, the glocks on both the directory and the new
  693. * file are held. A transaction has been started and an inplace reservation
  694. * is held, as well.
  695. *
  696. * Returns: An inode
  697. */
  698. struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
  699. unsigned int mode, dev_t dev)
  700. {
  701. struct inode *inode;
  702. struct gfs2_inode *dip = ghs->gh_gl->gl_object;
  703. struct inode *dir = &dip->i_inode;
  704. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  705. struct gfs2_inum_host inum;
  706. int error;
  707. u64 generation;
  708. if (!name->len || name->len > GFS2_FNAMESIZE)
  709. return ERR_PTR(-ENAMETOOLONG);
  710. gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
  711. error = gfs2_glock_nq(ghs);
  712. if (error)
  713. goto fail;
  714. error = create_ok(dip, name, mode);
  715. if (error)
  716. goto fail_gunlock;
  717. error = pick_formal_ino(sdp, &inum.no_formal_ino);
  718. if (error)
  719. goto fail_gunlock;
  720. error = alloc_dinode(dip, &inum, &generation);
  721. if (error)
  722. goto fail_gunlock;
  723. if (inum.no_addr < dip->i_num.no_addr) {
  724. gfs2_glock_dq(ghs);
  725. error = gfs2_glock_nq_num(sdp, inum.no_addr,
  726. &gfs2_inode_glops, LM_ST_EXCLUSIVE,
  727. GL_SKIP, ghs + 1);
  728. if (error) {
  729. return ERR_PTR(error);
  730. }
  731. gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
  732. error = gfs2_glock_nq(ghs);
  733. if (error) {
  734. gfs2_glock_dq_uninit(ghs + 1);
  735. return ERR_PTR(error);
  736. }
  737. error = create_ok(dip, name, mode);
  738. if (error)
  739. goto fail_gunlock2;
  740. } else {
  741. error = gfs2_glock_nq_num(sdp, inum.no_addr,
  742. &gfs2_inode_glops, LM_ST_EXCLUSIVE,
  743. GL_SKIP, ghs + 1);
  744. if (error)
  745. goto fail_gunlock;
  746. }
  747. error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev);
  748. if (error)
  749. goto fail_gunlock2;
  750. inode = gfs2_inode_lookup(dir->i_sb, &inum, IF2DT(mode));
  751. if (IS_ERR(inode))
  752. goto fail_gunlock2;
  753. error = gfs2_inode_refresh(GFS2_I(inode));
  754. if (error)
  755. goto fail_iput;
  756. error = gfs2_acl_create(dip, GFS2_I(inode));
  757. if (error)
  758. goto fail_iput;
  759. error = gfs2_security_init(dip, GFS2_I(inode));
  760. if (error)
  761. goto fail_iput;
  762. error = link_dinode(dip, name, GFS2_I(inode));
  763. if (error)
  764. goto fail_iput;
  765. if (!inode)
  766. return ERR_PTR(-ENOMEM);
  767. return inode;
  768. fail_iput:
  769. iput(inode);
  770. fail_gunlock2:
  771. gfs2_glock_dq_uninit(ghs + 1);
  772. fail_gunlock:
  773. gfs2_glock_dq(ghs);
  774. fail:
  775. return ERR_PTR(error);
  776. }
  777. /**
  778. * gfs2_rmdiri - Remove a directory
  779. * @dip: The parent directory of the directory to be removed
  780. * @name: The name of the directory to be removed
  781. * @ip: The GFS2 inode of the directory to be removed
  782. *
  783. * Assumes Glocks on dip and ip are held
  784. *
  785. * Returns: errno
  786. */
  787. int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
  788. struct gfs2_inode *ip)
  789. {
  790. struct qstr dotname;
  791. int error;
  792. if (ip->i_di.di_entries != 2) {
  793. if (gfs2_consist_inode(ip))
  794. gfs2_dinode_print(ip);
  795. return -EIO;
  796. }
  797. error = gfs2_dir_del(dip, name);
  798. if (error)
  799. return error;
  800. error = gfs2_change_nlink(dip, -1);
  801. if (error)
  802. return error;
  803. gfs2_str2qstr(&dotname, ".");
  804. error = gfs2_dir_del(ip, &dotname);
  805. if (error)
  806. return error;
  807. gfs2_str2qstr(&dotname, "..");
  808. error = gfs2_dir_del(ip, &dotname);
  809. if (error)
  810. return error;
  811. /* It looks odd, but it really should be done twice */
  812. error = gfs2_change_nlink(ip, -1);
  813. if (error)
  814. return error;
  815. error = gfs2_change_nlink(ip, -1);
  816. if (error)
  817. return error;
  818. return error;
  819. }
  820. /*
  821. * gfs2_unlink_ok - check to see that a inode is still in a directory
  822. * @dip: the directory
  823. * @name: the name of the file
  824. * @ip: the inode
  825. *
  826. * Assumes that the lock on (at least) @dip is held.
  827. *
  828. * Returns: 0 if the parent/child relationship is correct, errno if it isn't
  829. */
  830. int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
  831. struct gfs2_inode *ip)
  832. {
  833. struct gfs2_inum_host inum;
  834. unsigned int type;
  835. int error;
  836. if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
  837. return -EPERM;
  838. if ((dip->i_inode.i_mode & S_ISVTX) &&
  839. dip->i_inode.i_uid != current->fsuid &&
  840. ip->i_inode.i_uid != current->fsuid && !capable(CAP_FOWNER))
  841. return -EPERM;
  842. if (IS_APPEND(&dip->i_inode))
  843. return -EPERM;
  844. error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
  845. if (error)
  846. return error;
  847. error = gfs2_dir_search(&dip->i_inode, name, &inum, &type);
  848. if (error)
  849. return error;
  850. if (!gfs2_inum_equal(&inum, &ip->i_num))
  851. return -ENOENT;
  852. if (IF2DT(ip->i_inode.i_mode) != type) {
  853. gfs2_consist_inode(dip);
  854. return -EIO;
  855. }
  856. return 0;
  857. }
  858. /*
  859. * gfs2_ok_to_move - check if it's ok to move a directory to another directory
  860. * @this: move this
  861. * @to: to here
  862. *
  863. * Follow @to back to the root and make sure we don't encounter @this
  864. * Assumes we already hold the rename lock.
  865. *
  866. * Returns: errno
  867. */
  868. int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
  869. {
  870. struct inode *dir = &to->i_inode;
  871. struct super_block *sb = dir->i_sb;
  872. struct inode *tmp;
  873. struct qstr dotdot;
  874. int error = 0;
  875. gfs2_str2qstr(&dotdot, "..");
  876. igrab(dir);
  877. for (;;) {
  878. if (dir == &this->i_inode) {
  879. error = -EINVAL;
  880. break;
  881. }
  882. if (dir == sb->s_root->d_inode) {
  883. error = 0;
  884. break;
  885. }
  886. tmp = gfs2_lookupi(dir, &dotdot, 1, NULL);
  887. if (IS_ERR(tmp)) {
  888. error = PTR_ERR(tmp);
  889. break;
  890. }
  891. iput(dir);
  892. dir = tmp;
  893. }
  894. iput(dir);
  895. return error;
  896. }
  897. /**
  898. * gfs2_readlinki - return the contents of a symlink
  899. * @ip: the symlink's inode
  900. * @buf: a pointer to the buffer to be filled
  901. * @len: a pointer to the length of @buf
  902. *
  903. * If @buf is too small, a piece of memory is kmalloc()ed and needs
  904. * to be freed by the caller.
  905. *
  906. * Returns: errno
  907. */
  908. int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
  909. {
  910. struct gfs2_holder i_gh;
  911. struct buffer_head *dibh;
  912. unsigned int x;
  913. int error;
  914. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
  915. error = gfs2_glock_nq_atime(&i_gh);
  916. if (error) {
  917. gfs2_holder_uninit(&i_gh);
  918. return error;
  919. }
  920. if (!ip->i_di.di_size) {
  921. gfs2_consist_inode(ip);
  922. error = -EIO;
  923. goto out;
  924. }
  925. error = gfs2_meta_inode_buffer(ip, &dibh);
  926. if (error)
  927. goto out;
  928. x = ip->i_di.di_size + 1;
  929. if (x > *len) {
  930. *buf = kmalloc(x, GFP_KERNEL);
  931. if (!*buf) {
  932. error = -ENOMEM;
  933. goto out_brelse;
  934. }
  935. }
  936. memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
  937. *len = x;
  938. out_brelse:
  939. brelse(dibh);
  940. out:
  941. gfs2_glock_dq_uninit(&i_gh);
  942. return error;
  943. }
  944. /**
  945. * gfs2_glock_nq_atime - Acquire a hold on an inode's glock, and
  946. * conditionally update the inode's atime
  947. * @gh: the holder to acquire
  948. *
  949. * Tests atime (access time) for gfs2_read, gfs2_readdir and gfs2_mmap
  950. * Update if the difference between the current time and the inode's current
  951. * atime is greater than an interval specified at mount.
  952. *
  953. * Returns: errno
  954. */
  955. int gfs2_glock_nq_atime(struct gfs2_holder *gh)
  956. {
  957. struct gfs2_glock *gl = gh->gh_gl;
  958. struct gfs2_sbd *sdp = gl->gl_sbd;
  959. struct gfs2_inode *ip = gl->gl_object;
  960. s64 curtime, quantum = gfs2_tune_get(sdp, gt_atime_quantum);
  961. unsigned int state;
  962. int flags;
  963. int error;
  964. if (gfs2_assert_warn(sdp, gh->gh_flags & GL_ATIME) ||
  965. gfs2_assert_warn(sdp, !(gh->gh_flags & GL_ASYNC)) ||
  966. gfs2_assert_warn(sdp, gl->gl_ops == &gfs2_inode_glops))
  967. return -EINVAL;
  968. state = gh->gh_state;
  969. flags = gh->gh_flags;
  970. error = gfs2_glock_nq(gh);
  971. if (error)
  972. return error;
  973. if (test_bit(SDF_NOATIME, &sdp->sd_flags) ||
  974. (sdp->sd_vfs->s_flags & MS_RDONLY))
  975. return 0;
  976. curtime = get_seconds();
  977. if (curtime - ip->i_inode.i_atime.tv_sec >= quantum) {
  978. gfs2_glock_dq(gh);
  979. gfs2_holder_reinit(LM_ST_EXCLUSIVE, gh->gh_flags & ~LM_FLAG_ANY,
  980. gh);
  981. error = gfs2_glock_nq(gh);
  982. if (error)
  983. return error;
  984. /* Verify that atime hasn't been updated while we were
  985. trying to get exclusive lock. */
  986. curtime = get_seconds();
  987. if (curtime - ip->i_inode.i_atime.tv_sec >= quantum) {
  988. struct buffer_head *dibh;
  989. struct gfs2_dinode *di;
  990. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  991. if (error == -EROFS)
  992. return 0;
  993. if (error)
  994. goto fail;
  995. error = gfs2_meta_inode_buffer(ip, &dibh);
  996. if (error)
  997. goto fail_end_trans;
  998. ip->i_inode.i_atime.tv_sec = curtime;
  999. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1000. di = (struct gfs2_dinode *)dibh->b_data;
  1001. di->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
  1002. brelse(dibh);
  1003. gfs2_trans_end(sdp);
  1004. }
  1005. /* If someone else has asked for the glock,
  1006. unlock and let them have it. Then reacquire
  1007. in the original state. */
  1008. if (gfs2_glock_is_blocking(gl)) {
  1009. gfs2_glock_dq(gh);
  1010. gfs2_holder_reinit(state, flags, gh);
  1011. return gfs2_glock_nq(gh);
  1012. }
  1013. }
  1014. return 0;
  1015. fail_end_trans:
  1016. gfs2_trans_end(sdp);
  1017. fail:
  1018. gfs2_glock_dq(gh);
  1019. return error;
  1020. }
  1021. /**
  1022. * glock_compare_atime - Compare two struct gfs2_glock structures for sort
  1023. * @arg_a: the first structure
  1024. * @arg_b: the second structure
  1025. *
  1026. * Returns: 1 if A > B
  1027. * -1 if A < B
  1028. * 0 if A == B
  1029. */
  1030. static int glock_compare_atime(const void *arg_a, const void *arg_b)
  1031. {
  1032. const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
  1033. const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
  1034. const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
  1035. const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
  1036. if (a->ln_number > b->ln_number)
  1037. return 1;
  1038. if (a->ln_number < b->ln_number)
  1039. return -1;
  1040. if (gh_a->gh_state == LM_ST_SHARED && gh_b->gh_state == LM_ST_EXCLUSIVE)
  1041. return 1;
  1042. if (gh_a->gh_state == LM_ST_SHARED && (gh_b->gh_flags & GL_ATIME))
  1043. return 1;
  1044. return 0;
  1045. }
  1046. /**
  1047. * gfs2_glock_nq_m_atime - acquire multiple glocks where one may need an
  1048. * atime update
  1049. * @num_gh: the number of structures
  1050. * @ghs: an array of struct gfs2_holder structures
  1051. *
  1052. * Returns: 0 on success (all glocks acquired),
  1053. * errno on failure (no glocks acquired)
  1054. */
  1055. int gfs2_glock_nq_m_atime(unsigned int num_gh, struct gfs2_holder *ghs)
  1056. {
  1057. struct gfs2_holder **p;
  1058. unsigned int x;
  1059. int error = 0;
  1060. if (!num_gh)
  1061. return 0;
  1062. if (num_gh == 1) {
  1063. ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
  1064. if (ghs->gh_flags & GL_ATIME)
  1065. error = gfs2_glock_nq_atime(ghs);
  1066. else
  1067. error = gfs2_glock_nq(ghs);
  1068. return error;
  1069. }
  1070. p = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
  1071. if (!p)
  1072. return -ENOMEM;
  1073. for (x = 0; x < num_gh; x++)
  1074. p[x] = &ghs[x];
  1075. sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare_atime,NULL);
  1076. for (x = 0; x < num_gh; x++) {
  1077. p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
  1078. if (p[x]->gh_flags & GL_ATIME)
  1079. error = gfs2_glock_nq_atime(p[x]);
  1080. else
  1081. error = gfs2_glock_nq(p[x]);
  1082. if (error) {
  1083. while (x--)
  1084. gfs2_glock_dq(p[x]);
  1085. break;
  1086. }
  1087. }
  1088. kfree(p);
  1089. return error;
  1090. }
  1091. static int
  1092. __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  1093. {
  1094. struct buffer_head *dibh;
  1095. int error;
  1096. error = gfs2_meta_inode_buffer(ip, &dibh);
  1097. if (!error) {
  1098. error = inode_setattr(&ip->i_inode, attr);
  1099. gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
  1100. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1101. gfs2_dinode_out(ip, dibh->b_data);
  1102. brelse(dibh);
  1103. }
  1104. return error;
  1105. }
  1106. /**
  1107. * gfs2_setattr_simple -
  1108. * @ip:
  1109. * @attr:
  1110. *
  1111. * Called with a reference on the vnode.
  1112. *
  1113. * Returns: errno
  1114. */
  1115. int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  1116. {
  1117. int error;
  1118. if (current->journal_info)
  1119. return __gfs2_setattr_simple(ip, attr);
  1120. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
  1121. if (error)
  1122. return error;
  1123. error = __gfs2_setattr_simple(ip, attr);
  1124. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  1125. return error;
  1126. }