inode.c 31 KB

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