inode.c 31 KB

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