inode.c 31 KB

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