inode.c 30 KB

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