inode.c 31 KB

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