inode.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2011 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/slab.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/completion.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/namei.h>
  14. #include <linux/mm.h>
  15. #include <linux/xattr.h>
  16. #include <linux/posix_acl.h>
  17. #include <linux/gfs2_ondisk.h>
  18. #include <linux/crc32.h>
  19. #include <linux/fiemap.h>
  20. #include <linux/security.h>
  21. #include <asm/uaccess.h>
  22. #include "gfs2.h"
  23. #include "incore.h"
  24. #include "acl.h"
  25. #include "bmap.h"
  26. #include "dir.h"
  27. #include "xattr.h"
  28. #include "glock.h"
  29. #include "inode.h"
  30. #include "meta_io.h"
  31. #include "quota.h"
  32. #include "rgrp.h"
  33. #include "trans.h"
  34. #include "util.h"
  35. #include "super.h"
  36. #include "glops.h"
  37. static int iget_test(struct inode *inode, void *opaque)
  38. {
  39. u64 no_addr = *(u64 *)opaque;
  40. return GFS2_I(inode)->i_no_addr == no_addr;
  41. }
  42. static int iget_set(struct inode *inode, void *opaque)
  43. {
  44. u64 no_addr = *(u64 *)opaque;
  45. GFS2_I(inode)->i_no_addr = no_addr;
  46. inode->i_ino = no_addr;
  47. return 0;
  48. }
  49. static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr)
  50. {
  51. struct inode *inode;
  52. repeat:
  53. inode = iget5_locked(sb, no_addr, iget_test, iget_set, &no_addr);
  54. if (!inode)
  55. return inode;
  56. if (is_bad_inode(inode)) {
  57. iput(inode);
  58. goto repeat;
  59. }
  60. return inode;
  61. }
  62. /**
  63. * gfs2_set_iop - Sets inode operations
  64. * @inode: The inode with correct i_mode filled in
  65. *
  66. * GFS2 lookup code fills in vfs inode contents based on info obtained
  67. * from directory entry inside gfs2_inode_lookup().
  68. */
  69. static void gfs2_set_iop(struct inode *inode)
  70. {
  71. struct gfs2_sbd *sdp = GFS2_SB(inode);
  72. umode_t mode = inode->i_mode;
  73. if (S_ISREG(mode)) {
  74. inode->i_op = &gfs2_file_iops;
  75. if (gfs2_localflocks(sdp))
  76. inode->i_fop = &gfs2_file_fops_nolock;
  77. else
  78. inode->i_fop = &gfs2_file_fops;
  79. } else if (S_ISDIR(mode)) {
  80. inode->i_op = &gfs2_dir_iops;
  81. if (gfs2_localflocks(sdp))
  82. inode->i_fop = &gfs2_dir_fops_nolock;
  83. else
  84. inode->i_fop = &gfs2_dir_fops;
  85. } else if (S_ISLNK(mode)) {
  86. inode->i_op = &gfs2_symlink_iops;
  87. } else {
  88. inode->i_op = &gfs2_file_iops;
  89. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  90. }
  91. }
  92. /**
  93. * gfs2_inode_lookup - Lookup an inode
  94. * @sb: The super block
  95. * @type: The type of the inode
  96. * @no_addr: The inode number
  97. * @no_formal_ino: The inode generation number
  98. * @blktype: Requested block type (GFS2_BLKST_DINODE or GFS2_BLKST_UNLINKED;
  99. * GFS2_BLKST_FREE do indicate not to verify)
  100. *
  101. * If @type is DT_UNKNOWN, the inode type is fetched from disk.
  102. *
  103. * If @blktype is anything other than GFS2_BLKST_FREE (which is used as a
  104. * placeholder because it doesn't otherwise make sense), the on-disk block type
  105. * is verified to be @blktype.
  106. *
  107. * Returns: A VFS inode, or an error
  108. */
  109. struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
  110. u64 no_addr, u64 no_formal_ino,
  111. unsigned int blktype)
  112. {
  113. struct inode *inode;
  114. struct gfs2_inode *ip;
  115. struct gfs2_glock *io_gl = NULL;
  116. struct gfs2_holder i_gh;
  117. int error;
  118. gfs2_holder_mark_uninitialized(&i_gh);
  119. inode = gfs2_iget(sb, no_addr);
  120. if (!inode)
  121. return ERR_PTR(-ENOMEM);
  122. ip = GFS2_I(inode);
  123. if (inode->i_state & I_NEW) {
  124. struct gfs2_sbd *sdp = GFS2_SB(inode);
  125. ip->i_no_formal_ino = no_formal_ino;
  126. error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  127. if (unlikely(error))
  128. goto fail;
  129. ip->i_gl->gl_object = ip;
  130. error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  131. if (unlikely(error))
  132. goto fail_put;
  133. if (type == DT_UNKNOWN || blktype != GFS2_BLKST_FREE) {
  134. /*
  135. * The GL_SKIP flag indicates to skip reading the inode
  136. * block. We read the inode with gfs2_inode_refresh
  137. * after possibly checking the block type.
  138. */
  139. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE,
  140. GL_SKIP, &i_gh);
  141. if (error)
  142. goto fail_put;
  143. if (blktype != GFS2_BLKST_FREE) {
  144. error = gfs2_check_blk_type(sdp, no_addr,
  145. blktype);
  146. if (error)
  147. goto fail_put;
  148. }
  149. }
  150. set_bit(GIF_INVALID, &ip->i_flags);
  151. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
  152. if (unlikely(error))
  153. goto fail_put;
  154. ip->i_iopen_gh.gh_gl->gl_object = ip;
  155. gfs2_glock_put(io_gl);
  156. io_gl = NULL;
  157. if (type == DT_UNKNOWN) {
  158. /* Inode glock must be locked already */
  159. error = gfs2_inode_refresh(GFS2_I(inode));
  160. if (error)
  161. goto fail_refresh;
  162. } else {
  163. inode->i_mode = DT2IF(type);
  164. }
  165. gfs2_set_iop(inode);
  166. unlock_new_inode(inode);
  167. }
  168. if (gfs2_holder_initialized(&i_gh))
  169. gfs2_glock_dq_uninit(&i_gh);
  170. return inode;
  171. fail_refresh:
  172. ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
  173. ip->i_iopen_gh.gh_gl->gl_object = NULL;
  174. gfs2_glock_dq_wait(&ip->i_iopen_gh);
  175. gfs2_holder_uninit(&ip->i_iopen_gh);
  176. fail_put:
  177. if (io_gl)
  178. gfs2_glock_put(io_gl);
  179. if (gfs2_holder_initialized(&i_gh))
  180. gfs2_glock_dq_uninit(&i_gh);
  181. ip->i_gl->gl_object = NULL;
  182. fail:
  183. iget_failed(inode);
  184. return ERR_PTR(error);
  185. }
  186. struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
  187. u64 *no_formal_ino, unsigned int blktype)
  188. {
  189. struct super_block *sb = sdp->sd_vfs;
  190. struct inode *inode;
  191. int error;
  192. inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0, blktype);
  193. if (IS_ERR(inode))
  194. return inode;
  195. /* Two extra checks for NFS only */
  196. if (no_formal_ino) {
  197. error = -ESTALE;
  198. if (GFS2_I(inode)->i_no_formal_ino != *no_formal_ino)
  199. goto fail_iput;
  200. error = -EIO;
  201. if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
  202. goto fail_iput;
  203. }
  204. return inode;
  205. fail_iput:
  206. iput(inode);
  207. return ERR_PTR(error);
  208. }
  209. struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
  210. {
  211. struct qstr qstr;
  212. struct inode *inode;
  213. gfs2_str2qstr(&qstr, name);
  214. inode = gfs2_lookupi(dip, &qstr, 1);
  215. /* gfs2_lookupi has inconsistent callers: vfs
  216. * related routines expect NULL for no entry found,
  217. * gfs2_lookup_simple callers expect ENOENT
  218. * and do not check for NULL.
  219. */
  220. if (inode == NULL)
  221. return ERR_PTR(-ENOENT);
  222. else
  223. return inode;
  224. }
  225. /**
  226. * gfs2_lookupi - Look up a filename in a directory and return its inode
  227. * @d_gh: An initialized holder for the directory glock
  228. * @name: The name of the inode to look for
  229. * @is_root: If 1, ignore the caller's permissions
  230. * @i_gh: An uninitialized holder for the new inode glock
  231. *
  232. * This can be called via the VFS filldir function when NFS is doing
  233. * a readdirplus and the inode which its intending to stat isn't
  234. * already in cache. In this case we must not take the directory glock
  235. * again, since the readdir call will have already taken that lock.
  236. *
  237. * Returns: errno
  238. */
  239. struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
  240. int is_root)
  241. {
  242. struct super_block *sb = dir->i_sb;
  243. struct gfs2_inode *dip = GFS2_I(dir);
  244. struct gfs2_holder d_gh;
  245. int error = 0;
  246. struct inode *inode = NULL;
  247. gfs2_holder_mark_uninitialized(&d_gh);
  248. if (!name->len || name->len > GFS2_FNAMESIZE)
  249. return ERR_PTR(-ENAMETOOLONG);
  250. if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
  251. (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
  252. dir == d_inode(sb->s_root))) {
  253. igrab(dir);
  254. return dir;
  255. }
  256. if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
  257. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  258. if (error)
  259. return ERR_PTR(error);
  260. }
  261. if (!is_root) {
  262. error = gfs2_permission(dir, MAY_EXEC);
  263. if (error)
  264. goto out;
  265. }
  266. inode = gfs2_dir_search(dir, name, false);
  267. if (IS_ERR(inode))
  268. error = PTR_ERR(inode);
  269. out:
  270. if (gfs2_holder_initialized(&d_gh))
  271. gfs2_glock_dq_uninit(&d_gh);
  272. if (error == -ENOENT)
  273. return NULL;
  274. return inode ? inode : ERR_PTR(error);
  275. }
  276. /**
  277. * create_ok - OK to create a new on-disk inode here?
  278. * @dip: Directory in which dinode is to be created
  279. * @name: Name of new dinode
  280. * @mode:
  281. *
  282. * Returns: errno
  283. */
  284. static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
  285. umode_t mode)
  286. {
  287. int error;
  288. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  289. if (error)
  290. return error;
  291. /* Don't create entries in an unlinked directory */
  292. if (!dip->i_inode.i_nlink)
  293. return -ENOENT;
  294. if (dip->i_entries == (u32)-1)
  295. return -EFBIG;
  296. if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
  297. return -EMLINK;
  298. return 0;
  299. }
  300. static void munge_mode_uid_gid(const struct gfs2_inode *dip,
  301. struct inode *inode)
  302. {
  303. if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
  304. (dip->i_inode.i_mode & S_ISUID) &&
  305. !uid_eq(dip->i_inode.i_uid, GLOBAL_ROOT_UID)) {
  306. if (S_ISDIR(inode->i_mode))
  307. inode->i_mode |= S_ISUID;
  308. else if (!uid_eq(dip->i_inode.i_uid, current_fsuid()))
  309. inode->i_mode &= ~07111;
  310. inode->i_uid = dip->i_inode.i_uid;
  311. } else
  312. inode->i_uid = current_fsuid();
  313. if (dip->i_inode.i_mode & S_ISGID) {
  314. if (S_ISDIR(inode->i_mode))
  315. inode->i_mode |= S_ISGID;
  316. inode->i_gid = dip->i_inode.i_gid;
  317. } else
  318. inode->i_gid = current_fsgid();
  319. }
  320. static int alloc_dinode(struct gfs2_inode *ip, u32 flags, unsigned *dblocks)
  321. {
  322. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  323. struct gfs2_alloc_parms ap = { .target = *dblocks, .aflags = flags, };
  324. int error;
  325. error = gfs2_quota_lock_check(ip, &ap);
  326. if (error)
  327. goto out;
  328. error = gfs2_inplace_reserve(ip, &ap);
  329. if (error)
  330. goto out_quota;
  331. error = gfs2_trans_begin(sdp, (*dblocks * RES_RG_BIT) + RES_STATFS + RES_QUOTA, 0);
  332. if (error)
  333. goto out_ipreserv;
  334. error = gfs2_alloc_blocks(ip, &ip->i_no_addr, dblocks, 1, &ip->i_generation);
  335. ip->i_no_formal_ino = ip->i_generation;
  336. ip->i_inode.i_ino = ip->i_no_addr;
  337. ip->i_goal = ip->i_no_addr;
  338. gfs2_trans_end(sdp);
  339. out_ipreserv:
  340. gfs2_inplace_release(ip);
  341. out_quota:
  342. gfs2_quota_unlock(ip);
  343. out:
  344. return error;
  345. }
  346. static void gfs2_init_dir(struct buffer_head *dibh,
  347. const struct gfs2_inode *parent)
  348. {
  349. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  350. struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
  351. gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
  352. dent->de_inum = di->di_num; /* already GFS2 endian */
  353. dent->de_type = cpu_to_be16(DT_DIR);
  354. dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
  355. gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
  356. gfs2_inum_out(parent, dent);
  357. dent->de_type = cpu_to_be16(DT_DIR);
  358. }
  359. /**
  360. * gfs2_init_xattr - Initialise an xattr block for a new inode
  361. * @ip: The inode in question
  362. *
  363. * This sets up an empty xattr block for a new inode, ready to
  364. * take any ACLs, LSM xattrs, etc.
  365. */
  366. static void gfs2_init_xattr(struct gfs2_inode *ip)
  367. {
  368. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  369. struct buffer_head *bh;
  370. struct gfs2_ea_header *ea;
  371. bh = gfs2_meta_new(ip->i_gl, ip->i_eattr);
  372. gfs2_trans_add_meta(ip->i_gl, bh);
  373. gfs2_metatype_set(bh, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
  374. gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
  375. ea = GFS2_EA_BH2FIRST(bh);
  376. ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
  377. ea->ea_type = GFS2_EATYPE_UNUSED;
  378. ea->ea_flags = GFS2_EAFLAG_LAST;
  379. brelse(bh);
  380. }
  381. /**
  382. * init_dinode - Fill in a new dinode structure
  383. * @dip: The directory this inode is being created in
  384. * @ip: The inode
  385. * @symname: The symlink destination (if a symlink)
  386. * @bhp: The buffer head (returned to caller)
  387. *
  388. */
  389. static void init_dinode(struct gfs2_inode *dip, struct gfs2_inode *ip,
  390. const char *symname)
  391. {
  392. struct gfs2_dinode *di;
  393. struct buffer_head *dibh;
  394. dibh = gfs2_meta_new(ip->i_gl, ip->i_no_addr);
  395. gfs2_trans_add_meta(ip->i_gl, dibh);
  396. di = (struct gfs2_dinode *)dibh->b_data;
  397. gfs2_dinode_out(ip, di);
  398. di->di_major = cpu_to_be32(MAJOR(ip->i_inode.i_rdev));
  399. di->di_minor = cpu_to_be32(MINOR(ip->i_inode.i_rdev));
  400. di->__pad1 = 0;
  401. di->__pad2 = 0;
  402. di->__pad3 = 0;
  403. memset(&di->__pad4, 0, sizeof(di->__pad4));
  404. memset(&di->di_reserved, 0, sizeof(di->di_reserved));
  405. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  406. switch(ip->i_inode.i_mode & S_IFMT) {
  407. case S_IFDIR:
  408. gfs2_init_dir(dibh, dip);
  409. break;
  410. case S_IFLNK:
  411. memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, ip->i_inode.i_size);
  412. break;
  413. }
  414. set_buffer_uptodate(dibh);
  415. brelse(dibh);
  416. }
  417. /**
  418. * gfs2_trans_da_blocks - Calculate number of blocks to link inode
  419. * @dip: The directory we are linking into
  420. * @da: The dir add information
  421. * @nr_inodes: The number of inodes involved
  422. *
  423. * This calculate the number of blocks we need to reserve in a
  424. * transaction to link @nr_inodes into a directory. In most cases
  425. * @nr_inodes will be 2 (the directory plus the inode being linked in)
  426. * but in case of rename, 4 may be required.
  427. *
  428. * Returns: Number of blocks
  429. */
  430. static unsigned gfs2_trans_da_blks(const struct gfs2_inode *dip,
  431. const struct gfs2_diradd *da,
  432. unsigned nr_inodes)
  433. {
  434. return da->nr_blocks + gfs2_rg_blocks(dip, da->nr_blocks) +
  435. (nr_inodes * RES_DINODE) + RES_QUOTA + RES_STATFS;
  436. }
  437. static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
  438. struct gfs2_inode *ip, struct gfs2_diradd *da)
  439. {
  440. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  441. struct gfs2_alloc_parms ap = { .target = da->nr_blocks, };
  442. int error;
  443. if (da->nr_blocks) {
  444. error = gfs2_quota_lock_check(dip, &ap);
  445. if (error)
  446. goto fail_quota_locks;
  447. error = gfs2_inplace_reserve(dip, &ap);
  448. if (error)
  449. goto fail_quota_locks;
  450. error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, da, 2), 0);
  451. if (error)
  452. goto fail_ipreserv;
  453. } else {
  454. error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
  455. if (error)
  456. goto fail_quota_locks;
  457. }
  458. error = gfs2_dir_add(&dip->i_inode, name, ip, da);
  459. gfs2_trans_end(sdp);
  460. fail_ipreserv:
  461. gfs2_inplace_release(dip);
  462. fail_quota_locks:
  463. gfs2_quota_unlock(dip);
  464. return error;
  465. }
  466. static int gfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
  467. void *fs_info)
  468. {
  469. const struct xattr *xattr;
  470. int err = 0;
  471. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  472. err = __gfs2_xattr_set(inode, xattr->name, xattr->value,
  473. xattr->value_len, 0,
  474. GFS2_EATYPE_SECURITY);
  475. if (err < 0)
  476. break;
  477. }
  478. return err;
  479. }
  480. /**
  481. * gfs2_create_inode - Create a new inode
  482. * @dir: The parent directory
  483. * @dentry: The new dentry
  484. * @file: If non-NULL, the file which is being opened
  485. * @mode: The permissions on the new inode
  486. * @dev: For device nodes, this is the device number
  487. * @symname: For symlinks, this is the link destination
  488. * @size: The initial size of the inode (ignored for directories)
  489. *
  490. * Returns: 0 on success, or error code
  491. */
  492. static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
  493. struct file *file,
  494. umode_t mode, dev_t dev, const char *symname,
  495. unsigned int size, int excl, int *opened)
  496. {
  497. const struct qstr *name = &dentry->d_name;
  498. struct posix_acl *default_acl, *acl;
  499. struct gfs2_holder ghs[2];
  500. struct inode *inode = NULL;
  501. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  502. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  503. struct gfs2_glock *io_gl = NULL;
  504. int error, free_vfs_inode = 1;
  505. u32 aflags = 0;
  506. unsigned blocks = 1;
  507. struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
  508. if (!name->len || name->len > GFS2_FNAMESIZE)
  509. return -ENAMETOOLONG;
  510. error = gfs2_rsqa_alloc(dip);
  511. if (error)
  512. return error;
  513. error = gfs2_rindex_update(sdp);
  514. if (error)
  515. return error;
  516. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  517. if (error)
  518. goto fail;
  519. error = create_ok(dip, name, mode);
  520. if (error)
  521. goto fail_gunlock;
  522. inode = gfs2_dir_search(dir, &dentry->d_name, !S_ISREG(mode) || excl);
  523. error = PTR_ERR(inode);
  524. if (!IS_ERR(inode)) {
  525. if (S_ISDIR(inode->i_mode)) {
  526. iput(inode);
  527. inode = ERR_PTR(-EISDIR);
  528. goto fail_gunlock;
  529. }
  530. d_instantiate(dentry, inode);
  531. error = 0;
  532. if (file) {
  533. if (S_ISREG(inode->i_mode))
  534. error = finish_open(file, dentry, gfs2_open_common, opened);
  535. else
  536. error = finish_no_open(file, NULL);
  537. }
  538. gfs2_glock_dq_uninit(ghs);
  539. return error;
  540. } else if (error != -ENOENT) {
  541. goto fail_gunlock;
  542. }
  543. error = gfs2_diradd_alloc_required(dir, name, &da);
  544. if (error < 0)
  545. goto fail_gunlock;
  546. inode = new_inode(sdp->sd_vfs);
  547. error = -ENOMEM;
  548. if (!inode)
  549. goto fail_gunlock;
  550. error = posix_acl_create(dir, &mode, &default_acl, &acl);
  551. if (error)
  552. goto fail_gunlock;
  553. ip = GFS2_I(inode);
  554. error = gfs2_rsqa_alloc(ip);
  555. if (error)
  556. goto fail_free_acls;
  557. inode->i_mode = mode;
  558. set_nlink(inode, S_ISDIR(mode) ? 2 : 1);
  559. inode->i_rdev = dev;
  560. inode->i_size = size;
  561. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  562. gfs2_set_inode_blocks(inode, 1);
  563. munge_mode_uid_gid(dip, inode);
  564. check_and_update_goal(dip);
  565. ip->i_goal = dip->i_goal;
  566. ip->i_diskflags = 0;
  567. ip->i_eattr = 0;
  568. ip->i_height = 0;
  569. ip->i_depth = 0;
  570. ip->i_entries = 0;
  571. switch(mode & S_IFMT) {
  572. case S_IFREG:
  573. if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
  574. gfs2_tune_get(sdp, gt_new_files_jdata))
  575. ip->i_diskflags |= GFS2_DIF_JDATA;
  576. gfs2_set_aops(inode);
  577. break;
  578. case S_IFDIR:
  579. ip->i_diskflags |= (dip->i_diskflags & GFS2_DIF_INHERIT_JDATA);
  580. ip->i_diskflags |= GFS2_DIF_JDATA;
  581. ip->i_entries = 2;
  582. break;
  583. }
  584. /* Force SYSTEM flag on all files and subdirs of a SYSTEM directory */
  585. if (dip->i_diskflags & GFS2_DIF_SYSTEM)
  586. ip->i_diskflags |= GFS2_DIF_SYSTEM;
  587. gfs2_set_inode_flags(inode);
  588. if ((GFS2_I(d_inode(sdp->sd_root_dir)) == dip) ||
  589. (dip->i_diskflags & GFS2_DIF_TOPDIR))
  590. aflags |= GFS2_AF_ORLOV;
  591. if (default_acl || acl)
  592. blocks++;
  593. error = alloc_dinode(ip, aflags, &blocks);
  594. if (error)
  595. goto fail_free_inode;
  596. gfs2_set_inode_blocks(inode, blocks);
  597. error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  598. if (error)
  599. goto fail_free_inode;
  600. ip->i_gl->gl_object = ip;
  601. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
  602. if (error)
  603. goto fail_free_inode;
  604. error = gfs2_trans_begin(sdp, blocks, 0);
  605. if (error)
  606. goto fail_gunlock2;
  607. if (blocks > 1) {
  608. ip->i_eattr = ip->i_no_addr + 1;
  609. gfs2_init_xattr(ip);
  610. }
  611. init_dinode(dip, ip, symname);
  612. gfs2_trans_end(sdp);
  613. error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  614. if (error)
  615. goto fail_gunlock2;
  616. BUG_ON(test_and_set_bit(GLF_INODE_CREATING, &io_gl->gl_flags));
  617. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
  618. if (error)
  619. goto fail_gunlock2;
  620. ip->i_iopen_gh.gh_gl->gl_object = ip;
  621. gfs2_glock_put(io_gl);
  622. gfs2_set_iop(inode);
  623. insert_inode_hash(inode);
  624. free_vfs_inode = 0; /* After this point, the inode is no longer
  625. considered free. Any failures need to undo
  626. the gfs2 structures. */
  627. if (default_acl) {
  628. error = __gfs2_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
  629. posix_acl_release(default_acl);
  630. }
  631. if (acl) {
  632. if (!error)
  633. error = __gfs2_set_acl(inode, acl, ACL_TYPE_ACCESS);
  634. posix_acl_release(acl);
  635. }
  636. if (error)
  637. goto fail_gunlock3;
  638. error = security_inode_init_security(&ip->i_inode, &dip->i_inode, name,
  639. &gfs2_initxattrs, NULL);
  640. if (error)
  641. goto fail_gunlock3;
  642. error = link_dinode(dip, name, ip, &da);
  643. if (error)
  644. goto fail_gunlock3;
  645. mark_inode_dirty(inode);
  646. d_instantiate(dentry, inode);
  647. if (file) {
  648. *opened |= FILE_CREATED;
  649. error = finish_open(file, dentry, gfs2_open_common, opened);
  650. }
  651. gfs2_glock_dq_uninit(ghs);
  652. gfs2_glock_dq_uninit(ghs + 1);
  653. clear_bit(GLF_INODE_CREATING, &io_gl->gl_flags);
  654. return error;
  655. fail_gunlock3:
  656. gfs2_glock_dq_uninit(&ip->i_iopen_gh);
  657. gfs2_glock_put(io_gl);
  658. fail_gunlock2:
  659. if (io_gl)
  660. clear_bit(GLF_INODE_CREATING, &io_gl->gl_flags);
  661. gfs2_glock_dq_uninit(ghs + 1);
  662. fail_free_inode:
  663. if (ip->i_gl)
  664. gfs2_glock_put(ip->i_gl);
  665. gfs2_rsqa_delete(ip, NULL);
  666. fail_free_acls:
  667. if (default_acl)
  668. posix_acl_release(default_acl);
  669. if (acl)
  670. posix_acl_release(acl);
  671. fail_gunlock:
  672. gfs2_dir_no_add(&da);
  673. gfs2_glock_dq_uninit(ghs);
  674. if (inode && !IS_ERR(inode)) {
  675. clear_nlink(inode);
  676. if (!free_vfs_inode)
  677. mark_inode_dirty(inode);
  678. set_bit(free_vfs_inode ? GIF_FREE_VFS_INODE : GIF_ALLOC_FAILED,
  679. &GFS2_I(inode)->i_flags);
  680. iput(inode);
  681. }
  682. fail:
  683. return error;
  684. }
  685. /**
  686. * gfs2_create - Create a file
  687. * @dir: The directory in which to create the file
  688. * @dentry: The dentry of the new file
  689. * @mode: The mode of the new file
  690. *
  691. * Returns: errno
  692. */
  693. static int gfs2_create(struct inode *dir, struct dentry *dentry,
  694. umode_t mode, bool excl)
  695. {
  696. return gfs2_create_inode(dir, dentry, NULL, S_IFREG | mode, 0, NULL, 0, excl, NULL);
  697. }
  698. /**
  699. * __gfs2_lookup - Look up a filename in a directory and return its inode
  700. * @dir: The directory inode
  701. * @dentry: The dentry of the new inode
  702. * @file: File to be opened
  703. * @opened: atomic_open flags
  704. *
  705. *
  706. * Returns: errno
  707. */
  708. static struct dentry *__gfs2_lookup(struct inode *dir, struct dentry *dentry,
  709. struct file *file, int *opened)
  710. {
  711. struct inode *inode;
  712. struct dentry *d;
  713. struct gfs2_holder gh;
  714. struct gfs2_glock *gl;
  715. int error;
  716. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  717. if (inode == NULL) {
  718. d_add(dentry, NULL);
  719. return NULL;
  720. }
  721. if (IS_ERR(inode))
  722. return ERR_CAST(inode);
  723. gl = GFS2_I(inode)->i_gl;
  724. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  725. if (error) {
  726. iput(inode);
  727. return ERR_PTR(error);
  728. }
  729. d = d_splice_alias(inode, dentry);
  730. if (IS_ERR(d)) {
  731. gfs2_glock_dq_uninit(&gh);
  732. return d;
  733. }
  734. if (file && S_ISREG(inode->i_mode))
  735. error = finish_open(file, dentry, gfs2_open_common, opened);
  736. gfs2_glock_dq_uninit(&gh);
  737. if (error) {
  738. dput(d);
  739. return ERR_PTR(error);
  740. }
  741. return d;
  742. }
  743. static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
  744. unsigned flags)
  745. {
  746. return __gfs2_lookup(dir, dentry, NULL, NULL);
  747. }
  748. /**
  749. * gfs2_link - Link to a file
  750. * @old_dentry: The inode to link
  751. * @dir: Add link to this directory
  752. * @dentry: The name of the link
  753. *
  754. * Link the inode in "old_dentry" into the directory "dir" with the
  755. * name in "dentry".
  756. *
  757. * Returns: errno
  758. */
  759. static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
  760. struct dentry *dentry)
  761. {
  762. struct gfs2_inode *dip = GFS2_I(dir);
  763. struct gfs2_sbd *sdp = GFS2_SB(dir);
  764. struct inode *inode = d_inode(old_dentry);
  765. struct gfs2_inode *ip = GFS2_I(inode);
  766. struct gfs2_holder ghs[2];
  767. struct buffer_head *dibh;
  768. struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
  769. int error;
  770. if (S_ISDIR(inode->i_mode))
  771. return -EPERM;
  772. error = gfs2_rsqa_alloc(dip);
  773. if (error)
  774. return error;
  775. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  776. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  777. error = gfs2_glock_nq(ghs); /* parent */
  778. if (error)
  779. goto out_parent;
  780. error = gfs2_glock_nq(ghs + 1); /* child */
  781. if (error)
  782. goto out_child;
  783. error = -ENOENT;
  784. if (inode->i_nlink == 0)
  785. goto out_gunlock;
  786. error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
  787. if (error)
  788. goto out_gunlock;
  789. error = gfs2_dir_check(dir, &dentry->d_name, NULL);
  790. switch (error) {
  791. case -ENOENT:
  792. break;
  793. case 0:
  794. error = -EEXIST;
  795. default:
  796. goto out_gunlock;
  797. }
  798. error = -EINVAL;
  799. if (!dip->i_inode.i_nlink)
  800. goto out_gunlock;
  801. error = -EFBIG;
  802. if (dip->i_entries == (u32)-1)
  803. goto out_gunlock;
  804. error = -EPERM;
  805. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  806. goto out_gunlock;
  807. error = -EINVAL;
  808. if (!ip->i_inode.i_nlink)
  809. goto out_gunlock;
  810. error = -EMLINK;
  811. if (ip->i_inode.i_nlink == (u32)-1)
  812. goto out_gunlock;
  813. error = gfs2_diradd_alloc_required(dir, &dentry->d_name, &da);
  814. if (error < 0)
  815. goto out_gunlock;
  816. if (da.nr_blocks) {
  817. struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
  818. error = gfs2_quota_lock_check(dip, &ap);
  819. if (error)
  820. goto out_gunlock;
  821. error = gfs2_inplace_reserve(dip, &ap);
  822. if (error)
  823. goto out_gunlock_q;
  824. error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, &da, 2), 0);
  825. if (error)
  826. goto out_ipres;
  827. } else {
  828. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
  829. if (error)
  830. goto out_ipres;
  831. }
  832. error = gfs2_meta_inode_buffer(ip, &dibh);
  833. if (error)
  834. goto out_end_trans;
  835. error = gfs2_dir_add(dir, &dentry->d_name, ip, &da);
  836. if (error)
  837. goto out_brelse;
  838. gfs2_trans_add_meta(ip->i_gl, dibh);
  839. inc_nlink(&ip->i_inode);
  840. ip->i_inode.i_ctime = CURRENT_TIME;
  841. ihold(inode);
  842. d_instantiate(dentry, inode);
  843. mark_inode_dirty(inode);
  844. out_brelse:
  845. brelse(dibh);
  846. out_end_trans:
  847. gfs2_trans_end(sdp);
  848. out_ipres:
  849. if (da.nr_blocks)
  850. gfs2_inplace_release(dip);
  851. out_gunlock_q:
  852. if (da.nr_blocks)
  853. gfs2_quota_unlock(dip);
  854. out_gunlock:
  855. gfs2_dir_no_add(&da);
  856. gfs2_glock_dq(ghs + 1);
  857. out_child:
  858. gfs2_glock_dq(ghs);
  859. out_parent:
  860. gfs2_holder_uninit(ghs);
  861. gfs2_holder_uninit(ghs + 1);
  862. return error;
  863. }
  864. /*
  865. * gfs2_unlink_ok - check to see that a inode is still in a directory
  866. * @dip: the directory
  867. * @name: the name of the file
  868. * @ip: the inode
  869. *
  870. * Assumes that the lock on (at least) @dip is held.
  871. *
  872. * Returns: 0 if the parent/child relationship is correct, errno if it isn't
  873. */
  874. static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
  875. const struct gfs2_inode *ip)
  876. {
  877. int error;
  878. if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
  879. return -EPERM;
  880. if ((dip->i_inode.i_mode & S_ISVTX) &&
  881. !uid_eq(dip->i_inode.i_uid, current_fsuid()) &&
  882. !uid_eq(ip->i_inode.i_uid, current_fsuid()) && !capable(CAP_FOWNER))
  883. return -EPERM;
  884. if (IS_APPEND(&dip->i_inode))
  885. return -EPERM;
  886. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  887. if (error)
  888. return error;
  889. return gfs2_dir_check(&dip->i_inode, name, ip);
  890. }
  891. /**
  892. * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it
  893. * @dip: The parent directory
  894. * @name: The name of the entry in the parent directory
  895. * @inode: The inode to be removed
  896. *
  897. * Called with all the locks and in a transaction. This will only be
  898. * called for a directory after it has been checked to ensure it is empty.
  899. *
  900. * Returns: 0 on success, or an error
  901. */
  902. static int gfs2_unlink_inode(struct gfs2_inode *dip,
  903. const struct dentry *dentry)
  904. {
  905. struct inode *inode = d_inode(dentry);
  906. struct gfs2_inode *ip = GFS2_I(inode);
  907. int error;
  908. error = gfs2_dir_del(dip, dentry);
  909. if (error)
  910. return error;
  911. ip->i_entries = 0;
  912. inode->i_ctime = CURRENT_TIME;
  913. if (S_ISDIR(inode->i_mode))
  914. clear_nlink(inode);
  915. else
  916. drop_nlink(inode);
  917. mark_inode_dirty(inode);
  918. if (inode->i_nlink == 0)
  919. gfs2_unlink_di(inode);
  920. return 0;
  921. }
  922. /**
  923. * gfs2_unlink - Unlink an inode (this does rmdir as well)
  924. * @dir: The inode of the directory containing the inode to unlink
  925. * @dentry: The file itself
  926. *
  927. * This routine uses the type of the inode as a flag to figure out
  928. * whether this is an unlink or an rmdir.
  929. *
  930. * Returns: errno
  931. */
  932. static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
  933. {
  934. struct gfs2_inode *dip = GFS2_I(dir);
  935. struct gfs2_sbd *sdp = GFS2_SB(dir);
  936. struct inode *inode = d_inode(dentry);
  937. struct gfs2_inode *ip = GFS2_I(inode);
  938. struct gfs2_holder ghs[3];
  939. struct gfs2_rgrpd *rgd;
  940. int error;
  941. error = gfs2_rindex_update(sdp);
  942. if (error)
  943. return error;
  944. error = -EROFS;
  945. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  946. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  947. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
  948. if (!rgd)
  949. goto out_inodes;
  950. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  951. error = gfs2_glock_nq(ghs); /* parent */
  952. if (error)
  953. goto out_parent;
  954. error = gfs2_glock_nq(ghs + 1); /* child */
  955. if (error)
  956. goto out_child;
  957. error = -ENOENT;
  958. if (inode->i_nlink == 0)
  959. goto out_rgrp;
  960. if (S_ISDIR(inode->i_mode)) {
  961. error = -ENOTEMPTY;
  962. if (ip->i_entries > 2 || inode->i_nlink > 2)
  963. goto out_rgrp;
  964. }
  965. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  966. if (error)
  967. goto out_rgrp;
  968. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  969. if (error)
  970. goto out_gunlock;
  971. error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
  972. if (error)
  973. goto out_end_trans;
  974. error = gfs2_unlink_inode(dip, dentry);
  975. out_end_trans:
  976. gfs2_trans_end(sdp);
  977. out_gunlock:
  978. gfs2_glock_dq(ghs + 2);
  979. out_rgrp:
  980. gfs2_glock_dq(ghs + 1);
  981. out_child:
  982. gfs2_glock_dq(ghs);
  983. out_parent:
  984. gfs2_holder_uninit(ghs + 2);
  985. out_inodes:
  986. gfs2_holder_uninit(ghs + 1);
  987. gfs2_holder_uninit(ghs);
  988. return error;
  989. }
  990. /**
  991. * gfs2_symlink - Create a symlink
  992. * @dir: The directory to create the symlink in
  993. * @dentry: The dentry to put the symlink in
  994. * @symname: The thing which the link points to
  995. *
  996. * Returns: errno
  997. */
  998. static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
  999. const char *symname)
  1000. {
  1001. struct gfs2_sbd *sdp = GFS2_SB(dir);
  1002. unsigned int size;
  1003. size = strlen(symname);
  1004. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
  1005. return -ENAMETOOLONG;
  1006. return gfs2_create_inode(dir, dentry, NULL, S_IFLNK | S_IRWXUGO, 0, symname, size, 0, NULL);
  1007. }
  1008. /**
  1009. * gfs2_mkdir - Make a directory
  1010. * @dir: The parent directory of the new one
  1011. * @dentry: The dentry of the new directory
  1012. * @mode: The mode of the new directory
  1013. *
  1014. * Returns: errno
  1015. */
  1016. static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  1017. {
  1018. struct gfs2_sbd *sdp = GFS2_SB(dir);
  1019. unsigned dsize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
  1020. return gfs2_create_inode(dir, dentry, NULL, S_IFDIR | mode, 0, NULL, dsize, 0, NULL);
  1021. }
  1022. /**
  1023. * gfs2_mknod - Make a special file
  1024. * @dir: The directory in which the special file will reside
  1025. * @dentry: The dentry of the special file
  1026. * @mode: The mode of the special file
  1027. * @dev: The device specification of the special file
  1028. *
  1029. */
  1030. static int gfs2_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  1031. dev_t dev)
  1032. {
  1033. return gfs2_create_inode(dir, dentry, NULL, mode, dev, NULL, 0, 0, NULL);
  1034. }
  1035. /**
  1036. * gfs2_atomic_open - Atomically open a file
  1037. * @dir: The directory
  1038. * @dentry: The proposed new entry
  1039. * @file: The proposed new struct file
  1040. * @flags: open flags
  1041. * @mode: File mode
  1042. * @opened: Flag to say whether the file has been opened or not
  1043. *
  1044. * Returns: error code or 0 for success
  1045. */
  1046. static int gfs2_atomic_open(struct inode *dir, struct dentry *dentry,
  1047. struct file *file, unsigned flags,
  1048. umode_t mode, int *opened)
  1049. {
  1050. struct dentry *d;
  1051. bool excl = !!(flags & O_EXCL);
  1052. if (!d_in_lookup(dentry))
  1053. goto skip_lookup;
  1054. d = __gfs2_lookup(dir, dentry, file, opened);
  1055. if (IS_ERR(d))
  1056. return PTR_ERR(d);
  1057. if (d != NULL)
  1058. dentry = d;
  1059. if (d_really_is_positive(dentry)) {
  1060. if (!(*opened & FILE_OPENED))
  1061. return finish_no_open(file, d);
  1062. dput(d);
  1063. return 0;
  1064. }
  1065. BUG_ON(d != NULL);
  1066. skip_lookup:
  1067. if (!(flags & O_CREAT))
  1068. return -ENOENT;
  1069. return gfs2_create_inode(dir, dentry, file, S_IFREG | mode, 0, NULL, 0, excl, opened);
  1070. }
  1071. /*
  1072. * gfs2_ok_to_move - check if it's ok to move a directory to another directory
  1073. * @this: move this
  1074. * @to: to here
  1075. *
  1076. * Follow @to back to the root and make sure we don't encounter @this
  1077. * Assumes we already hold the rename lock.
  1078. *
  1079. * Returns: errno
  1080. */
  1081. static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
  1082. {
  1083. struct inode *dir = &to->i_inode;
  1084. struct super_block *sb = dir->i_sb;
  1085. struct inode *tmp;
  1086. int error = 0;
  1087. igrab(dir);
  1088. for (;;) {
  1089. if (dir == &this->i_inode) {
  1090. error = -EINVAL;
  1091. break;
  1092. }
  1093. if (dir == d_inode(sb->s_root)) {
  1094. error = 0;
  1095. break;
  1096. }
  1097. tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
  1098. if (!tmp) {
  1099. error = -ENOENT;
  1100. break;
  1101. }
  1102. if (IS_ERR(tmp)) {
  1103. error = PTR_ERR(tmp);
  1104. break;
  1105. }
  1106. iput(dir);
  1107. dir = tmp;
  1108. }
  1109. iput(dir);
  1110. return error;
  1111. }
  1112. /**
  1113. * update_moved_ino - Update an inode that's being moved
  1114. * @ip: The inode being moved
  1115. * @ndip: The parent directory of the new filename
  1116. * @dir_rename: True of ip is a directory
  1117. *
  1118. * Returns: errno
  1119. */
  1120. static int update_moved_ino(struct gfs2_inode *ip, struct gfs2_inode *ndip,
  1121. int dir_rename)
  1122. {
  1123. int error;
  1124. struct buffer_head *dibh;
  1125. if (dir_rename)
  1126. return gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
  1127. error = gfs2_meta_inode_buffer(ip, &dibh);
  1128. if (error)
  1129. return error;
  1130. ip->i_inode.i_ctime = CURRENT_TIME;
  1131. gfs2_trans_add_meta(ip->i_gl, dibh);
  1132. gfs2_dinode_out(ip, dibh->b_data);
  1133. brelse(dibh);
  1134. return 0;
  1135. }
  1136. /**
  1137. * gfs2_rename - Rename a file
  1138. * @odir: Parent directory of old file name
  1139. * @odentry: The old dentry of the file
  1140. * @ndir: Parent directory of new file name
  1141. * @ndentry: The new dentry of the file
  1142. *
  1143. * Returns: errno
  1144. */
  1145. static int gfs2_rename(struct inode *odir, struct dentry *odentry,
  1146. struct inode *ndir, struct dentry *ndentry)
  1147. {
  1148. struct gfs2_inode *odip = GFS2_I(odir);
  1149. struct gfs2_inode *ndip = GFS2_I(ndir);
  1150. struct gfs2_inode *ip = GFS2_I(d_inode(odentry));
  1151. struct gfs2_inode *nip = NULL;
  1152. struct gfs2_sbd *sdp = GFS2_SB(odir);
  1153. struct gfs2_holder ghs[5], r_gh;
  1154. struct gfs2_rgrpd *nrgd;
  1155. unsigned int num_gh;
  1156. int dir_rename = 0;
  1157. struct gfs2_diradd da = { .nr_blocks = 0, .save_loc = 0, };
  1158. unsigned int x;
  1159. int error;
  1160. gfs2_holder_mark_uninitialized(&r_gh);
  1161. if (d_really_is_positive(ndentry)) {
  1162. nip = GFS2_I(d_inode(ndentry));
  1163. if (ip == nip)
  1164. return 0;
  1165. }
  1166. error = gfs2_rindex_update(sdp);
  1167. if (error)
  1168. return error;
  1169. error = gfs2_rsqa_alloc(ndip);
  1170. if (error)
  1171. return error;
  1172. if (odip != ndip) {
  1173. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
  1174. 0, &r_gh);
  1175. if (error)
  1176. goto out;
  1177. if (S_ISDIR(ip->i_inode.i_mode)) {
  1178. dir_rename = 1;
  1179. /* don't move a directory into its subdir */
  1180. error = gfs2_ok_to_move(ip, ndip);
  1181. if (error)
  1182. goto out_gunlock_r;
  1183. }
  1184. }
  1185. num_gh = 1;
  1186. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  1187. if (odip != ndip) {
  1188. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1189. num_gh++;
  1190. }
  1191. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1192. num_gh++;
  1193. if (nip) {
  1194. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1195. num_gh++;
  1196. /* grab the resource lock for unlink flag twiddling
  1197. * this is the case of the target file already existing
  1198. * so we unlink before doing the rename
  1199. */
  1200. nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr, 1);
  1201. if (nrgd)
  1202. gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
  1203. }
  1204. for (x = 0; x < num_gh; x++) {
  1205. error = gfs2_glock_nq(ghs + x);
  1206. if (error)
  1207. goto out_gunlock;
  1208. }
  1209. error = -ENOENT;
  1210. if (ip->i_inode.i_nlink == 0)
  1211. goto out_gunlock;
  1212. /* Check out the old directory */
  1213. error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
  1214. if (error)
  1215. goto out_gunlock;
  1216. /* Check out the new directory */
  1217. if (nip) {
  1218. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  1219. if (error)
  1220. goto out_gunlock;
  1221. if (nip->i_inode.i_nlink == 0) {
  1222. error = -EAGAIN;
  1223. goto out_gunlock;
  1224. }
  1225. if (S_ISDIR(nip->i_inode.i_mode)) {
  1226. if (nip->i_entries < 2) {
  1227. gfs2_consist_inode(nip);
  1228. error = -EIO;
  1229. goto out_gunlock;
  1230. }
  1231. if (nip->i_entries > 2) {
  1232. error = -ENOTEMPTY;
  1233. goto out_gunlock;
  1234. }
  1235. }
  1236. } else {
  1237. error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
  1238. if (error)
  1239. goto out_gunlock;
  1240. error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
  1241. switch (error) {
  1242. case -ENOENT:
  1243. error = 0;
  1244. break;
  1245. case 0:
  1246. error = -EEXIST;
  1247. default:
  1248. goto out_gunlock;
  1249. };
  1250. if (odip != ndip) {
  1251. if (!ndip->i_inode.i_nlink) {
  1252. error = -ENOENT;
  1253. goto out_gunlock;
  1254. }
  1255. if (ndip->i_entries == (u32)-1) {
  1256. error = -EFBIG;
  1257. goto out_gunlock;
  1258. }
  1259. if (S_ISDIR(ip->i_inode.i_mode) &&
  1260. ndip->i_inode.i_nlink == (u32)-1) {
  1261. error = -EMLINK;
  1262. goto out_gunlock;
  1263. }
  1264. }
  1265. }
  1266. /* Check out the dir to be renamed */
  1267. if (dir_rename) {
  1268. error = gfs2_permission(d_inode(odentry), MAY_WRITE);
  1269. if (error)
  1270. goto out_gunlock;
  1271. }
  1272. if (nip == NULL) {
  1273. error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name, &da);
  1274. if (error)
  1275. goto out_gunlock;
  1276. }
  1277. if (da.nr_blocks) {
  1278. struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
  1279. error = gfs2_quota_lock_check(ndip, &ap);
  1280. if (error)
  1281. goto out_gunlock;
  1282. error = gfs2_inplace_reserve(ndip, &ap);
  1283. if (error)
  1284. goto out_gunlock_q;
  1285. error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(ndip, &da, 4) +
  1286. 4 * RES_LEAF + 4, 0);
  1287. if (error)
  1288. goto out_ipreserv;
  1289. } else {
  1290. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  1291. 5 * RES_LEAF + 4, 0);
  1292. if (error)
  1293. goto out_gunlock;
  1294. }
  1295. /* Remove the target file, if it exists */
  1296. if (nip)
  1297. error = gfs2_unlink_inode(ndip, ndentry);
  1298. error = update_moved_ino(ip, ndip, dir_rename);
  1299. if (error)
  1300. goto out_end_trans;
  1301. error = gfs2_dir_del(odip, odentry);
  1302. if (error)
  1303. goto out_end_trans;
  1304. error = gfs2_dir_add(ndir, &ndentry->d_name, ip, &da);
  1305. if (error)
  1306. goto out_end_trans;
  1307. out_end_trans:
  1308. gfs2_trans_end(sdp);
  1309. out_ipreserv:
  1310. if (da.nr_blocks)
  1311. gfs2_inplace_release(ndip);
  1312. out_gunlock_q:
  1313. if (da.nr_blocks)
  1314. gfs2_quota_unlock(ndip);
  1315. out_gunlock:
  1316. gfs2_dir_no_add(&da);
  1317. while (x--) {
  1318. gfs2_glock_dq(ghs + x);
  1319. gfs2_holder_uninit(ghs + x);
  1320. }
  1321. out_gunlock_r:
  1322. if (gfs2_holder_initialized(&r_gh))
  1323. gfs2_glock_dq_uninit(&r_gh);
  1324. out:
  1325. return error;
  1326. }
  1327. /**
  1328. * gfs2_exchange - exchange two files
  1329. * @odir: Parent directory of old file name
  1330. * @odentry: The old dentry of the file
  1331. * @ndir: Parent directory of new file name
  1332. * @ndentry: The new dentry of the file
  1333. * @flags: The rename flags
  1334. *
  1335. * Returns: errno
  1336. */
  1337. static int gfs2_exchange(struct inode *odir, struct dentry *odentry,
  1338. struct inode *ndir, struct dentry *ndentry,
  1339. unsigned int flags)
  1340. {
  1341. struct gfs2_inode *odip = GFS2_I(odir);
  1342. struct gfs2_inode *ndip = GFS2_I(ndir);
  1343. struct gfs2_inode *oip = GFS2_I(odentry->d_inode);
  1344. struct gfs2_inode *nip = GFS2_I(ndentry->d_inode);
  1345. struct gfs2_sbd *sdp = GFS2_SB(odir);
  1346. struct gfs2_holder ghs[5], r_gh;
  1347. unsigned int num_gh;
  1348. unsigned int x;
  1349. umode_t old_mode = oip->i_inode.i_mode;
  1350. umode_t new_mode = nip->i_inode.i_mode;
  1351. int error;
  1352. gfs2_holder_mark_uninitialized(&r_gh);
  1353. error = gfs2_rindex_update(sdp);
  1354. if (error)
  1355. return error;
  1356. if (odip != ndip) {
  1357. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
  1358. 0, &r_gh);
  1359. if (error)
  1360. goto out;
  1361. if (S_ISDIR(old_mode)) {
  1362. /* don't move a directory into its subdir */
  1363. error = gfs2_ok_to_move(oip, ndip);
  1364. if (error)
  1365. goto out_gunlock_r;
  1366. }
  1367. if (S_ISDIR(new_mode)) {
  1368. /* don't move a directory into its subdir */
  1369. error = gfs2_ok_to_move(nip, odip);
  1370. if (error)
  1371. goto out_gunlock_r;
  1372. }
  1373. }
  1374. num_gh = 1;
  1375. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  1376. if (odip != ndip) {
  1377. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1378. num_gh++;
  1379. }
  1380. gfs2_holder_init(oip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1381. num_gh++;
  1382. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1383. num_gh++;
  1384. for (x = 0; x < num_gh; x++) {
  1385. error = gfs2_glock_nq(ghs + x);
  1386. if (error)
  1387. goto out_gunlock;
  1388. }
  1389. error = -ENOENT;
  1390. if (oip->i_inode.i_nlink == 0 || nip->i_inode.i_nlink == 0)
  1391. goto out_gunlock;
  1392. error = gfs2_unlink_ok(odip, &odentry->d_name, oip);
  1393. if (error)
  1394. goto out_gunlock;
  1395. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  1396. if (error)
  1397. goto out_gunlock;
  1398. if (S_ISDIR(old_mode)) {
  1399. error = gfs2_permission(odentry->d_inode, MAY_WRITE);
  1400. if (error)
  1401. goto out_gunlock;
  1402. }
  1403. if (S_ISDIR(new_mode)) {
  1404. error = gfs2_permission(ndentry->d_inode, MAY_WRITE);
  1405. if (error)
  1406. goto out_gunlock;
  1407. }
  1408. error = gfs2_trans_begin(sdp, 4 * RES_DINODE + 4 * RES_LEAF, 0);
  1409. if (error)
  1410. goto out_gunlock;
  1411. error = update_moved_ino(oip, ndip, S_ISDIR(old_mode));
  1412. if (error)
  1413. goto out_end_trans;
  1414. error = update_moved_ino(nip, odip, S_ISDIR(new_mode));
  1415. if (error)
  1416. goto out_end_trans;
  1417. error = gfs2_dir_mvino(ndip, &ndentry->d_name, oip,
  1418. IF2DT(old_mode));
  1419. if (error)
  1420. goto out_end_trans;
  1421. error = gfs2_dir_mvino(odip, &odentry->d_name, nip,
  1422. IF2DT(new_mode));
  1423. if (error)
  1424. goto out_end_trans;
  1425. if (odip != ndip) {
  1426. if (S_ISDIR(new_mode) && !S_ISDIR(old_mode)) {
  1427. inc_nlink(&odip->i_inode);
  1428. drop_nlink(&ndip->i_inode);
  1429. } else if (S_ISDIR(old_mode) && !S_ISDIR(new_mode)) {
  1430. inc_nlink(&ndip->i_inode);
  1431. drop_nlink(&odip->i_inode);
  1432. }
  1433. }
  1434. mark_inode_dirty(&ndip->i_inode);
  1435. if (odip != ndip)
  1436. mark_inode_dirty(&odip->i_inode);
  1437. out_end_trans:
  1438. gfs2_trans_end(sdp);
  1439. out_gunlock:
  1440. while (x--) {
  1441. gfs2_glock_dq(ghs + x);
  1442. gfs2_holder_uninit(ghs + x);
  1443. }
  1444. out_gunlock_r:
  1445. if (gfs2_holder_initialized(&r_gh))
  1446. gfs2_glock_dq_uninit(&r_gh);
  1447. out:
  1448. return error;
  1449. }
  1450. static int gfs2_rename2(struct inode *odir, struct dentry *odentry,
  1451. struct inode *ndir, struct dentry *ndentry,
  1452. unsigned int flags)
  1453. {
  1454. flags &= ~RENAME_NOREPLACE;
  1455. if (flags & ~RENAME_EXCHANGE)
  1456. return -EINVAL;
  1457. if (flags & RENAME_EXCHANGE)
  1458. return gfs2_exchange(odir, odentry, ndir, ndentry, flags);
  1459. return gfs2_rename(odir, odentry, ndir, ndentry);
  1460. }
  1461. /**
  1462. * gfs2_get_link - Follow a symbolic link
  1463. * @dentry: The dentry of the link
  1464. * @inode: The inode of the link
  1465. * @done: destructor for return value
  1466. *
  1467. * This can handle symlinks of any size.
  1468. *
  1469. * Returns: 0 on success or error code
  1470. */
  1471. static const char *gfs2_get_link(struct dentry *dentry,
  1472. struct inode *inode,
  1473. struct delayed_call *done)
  1474. {
  1475. struct gfs2_inode *ip = GFS2_I(inode);
  1476. struct gfs2_holder i_gh;
  1477. struct buffer_head *dibh;
  1478. unsigned int size;
  1479. char *buf;
  1480. int error;
  1481. if (!dentry)
  1482. return ERR_PTR(-ECHILD);
  1483. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  1484. error = gfs2_glock_nq(&i_gh);
  1485. if (error) {
  1486. gfs2_holder_uninit(&i_gh);
  1487. return ERR_PTR(error);
  1488. }
  1489. size = (unsigned int)i_size_read(&ip->i_inode);
  1490. if (size == 0) {
  1491. gfs2_consist_inode(ip);
  1492. buf = ERR_PTR(-EIO);
  1493. goto out;
  1494. }
  1495. error = gfs2_meta_inode_buffer(ip, &dibh);
  1496. if (error) {
  1497. buf = ERR_PTR(error);
  1498. goto out;
  1499. }
  1500. buf = kzalloc(size + 1, GFP_NOFS);
  1501. if (!buf)
  1502. buf = ERR_PTR(-ENOMEM);
  1503. else
  1504. memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
  1505. brelse(dibh);
  1506. out:
  1507. gfs2_glock_dq_uninit(&i_gh);
  1508. if (!IS_ERR(buf))
  1509. set_delayed_call(done, kfree_link, buf);
  1510. return buf;
  1511. }
  1512. /**
  1513. * gfs2_permission -
  1514. * @inode: The inode
  1515. * @mask: The mask to be tested
  1516. * @flags: Indicates whether this is an RCU path walk or not
  1517. *
  1518. * This may be called from the VFS directly, or from within GFS2 with the
  1519. * inode locked, so we look to see if the glock is already locked and only
  1520. * lock the glock if its not already been done.
  1521. *
  1522. * Returns: errno
  1523. */
  1524. int gfs2_permission(struct inode *inode, int mask)
  1525. {
  1526. struct gfs2_inode *ip;
  1527. struct gfs2_holder i_gh;
  1528. int error;
  1529. gfs2_holder_mark_uninitialized(&i_gh);
  1530. ip = GFS2_I(inode);
  1531. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1532. if (mask & MAY_NOT_BLOCK)
  1533. return -ECHILD;
  1534. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  1535. if (error)
  1536. return error;
  1537. }
  1538. if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
  1539. error = -EPERM;
  1540. else
  1541. error = generic_permission(inode, mask);
  1542. if (gfs2_holder_initialized(&i_gh))
  1543. gfs2_glock_dq_uninit(&i_gh);
  1544. return error;
  1545. }
  1546. static int __gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
  1547. {
  1548. setattr_copy(inode, attr);
  1549. mark_inode_dirty(inode);
  1550. return 0;
  1551. }
  1552. /**
  1553. * gfs2_setattr_simple -
  1554. * @ip:
  1555. * @attr:
  1556. *
  1557. * Returns: errno
  1558. */
  1559. int gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
  1560. {
  1561. int error;
  1562. if (current->journal_info)
  1563. return __gfs2_setattr_simple(inode, attr);
  1564. error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0);
  1565. if (error)
  1566. return error;
  1567. error = __gfs2_setattr_simple(inode, attr);
  1568. gfs2_trans_end(GFS2_SB(inode));
  1569. return error;
  1570. }
  1571. static int setattr_chown(struct inode *inode, struct iattr *attr)
  1572. {
  1573. struct gfs2_inode *ip = GFS2_I(inode);
  1574. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1575. kuid_t ouid, nuid;
  1576. kgid_t ogid, ngid;
  1577. int error;
  1578. struct gfs2_alloc_parms ap;
  1579. ouid = inode->i_uid;
  1580. ogid = inode->i_gid;
  1581. nuid = attr->ia_uid;
  1582. ngid = attr->ia_gid;
  1583. if (!(attr->ia_valid & ATTR_UID) || uid_eq(ouid, nuid))
  1584. ouid = nuid = NO_UID_QUOTA_CHANGE;
  1585. if (!(attr->ia_valid & ATTR_GID) || gid_eq(ogid, ngid))
  1586. ogid = ngid = NO_GID_QUOTA_CHANGE;
  1587. error = gfs2_rsqa_alloc(ip);
  1588. if (error)
  1589. goto out;
  1590. error = gfs2_rindex_update(sdp);
  1591. if (error)
  1592. goto out;
  1593. error = gfs2_quota_lock(ip, nuid, ngid);
  1594. if (error)
  1595. goto out;
  1596. ap.target = gfs2_get_inode_blocks(&ip->i_inode);
  1597. if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
  1598. !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
  1599. error = gfs2_quota_check(ip, nuid, ngid, &ap);
  1600. if (error)
  1601. goto out_gunlock_q;
  1602. }
  1603. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  1604. if (error)
  1605. goto out_gunlock_q;
  1606. error = gfs2_setattr_simple(inode, attr);
  1607. if (error)
  1608. goto out_end_trans;
  1609. if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
  1610. !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
  1611. gfs2_quota_change(ip, -(s64)ap.target, ouid, ogid);
  1612. gfs2_quota_change(ip, ap.target, nuid, ngid);
  1613. }
  1614. out_end_trans:
  1615. gfs2_trans_end(sdp);
  1616. out_gunlock_q:
  1617. gfs2_quota_unlock(ip);
  1618. out:
  1619. return error;
  1620. }
  1621. /**
  1622. * gfs2_setattr - Change attributes on an inode
  1623. * @dentry: The dentry which is changing
  1624. * @attr: The structure describing the change
  1625. *
  1626. * The VFS layer wants to change one or more of an inodes attributes. Write
  1627. * that change out to disk.
  1628. *
  1629. * Returns: errno
  1630. */
  1631. static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
  1632. {
  1633. struct inode *inode = d_inode(dentry);
  1634. struct gfs2_inode *ip = GFS2_I(inode);
  1635. struct gfs2_holder i_gh;
  1636. int error;
  1637. error = gfs2_rsqa_alloc(ip);
  1638. if (error)
  1639. return error;
  1640. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  1641. if (error)
  1642. return error;
  1643. error = -EPERM;
  1644. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  1645. goto out;
  1646. error = inode_change_ok(inode, attr);
  1647. if (error)
  1648. goto out;
  1649. if (attr->ia_valid & ATTR_SIZE)
  1650. error = gfs2_setattr_size(inode, attr->ia_size);
  1651. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  1652. error = setattr_chown(inode, attr);
  1653. else {
  1654. error = gfs2_setattr_simple(inode, attr);
  1655. if (!error && attr->ia_valid & ATTR_MODE)
  1656. error = posix_acl_chmod(inode, inode->i_mode);
  1657. }
  1658. out:
  1659. if (!error)
  1660. mark_inode_dirty(inode);
  1661. gfs2_glock_dq_uninit(&i_gh);
  1662. return error;
  1663. }
  1664. /**
  1665. * gfs2_getattr - Read out an inode's attributes
  1666. * @mnt: The vfsmount the inode is being accessed from
  1667. * @dentry: The dentry to stat
  1668. * @stat: The inode's stats
  1669. *
  1670. * This may be called from the VFS directly, or from within GFS2 with the
  1671. * inode locked, so we look to see if the glock is already locked and only
  1672. * lock the glock if its not already been done. Note that its the NFS
  1673. * readdirplus operation which causes this to be called (from filldir)
  1674. * with the glock already held.
  1675. *
  1676. * Returns: errno
  1677. */
  1678. static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1679. struct kstat *stat)
  1680. {
  1681. struct inode *inode = d_inode(dentry);
  1682. struct gfs2_inode *ip = GFS2_I(inode);
  1683. struct gfs2_holder gh;
  1684. int error;
  1685. gfs2_holder_mark_uninitialized(&gh);
  1686. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1687. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1688. if (error)
  1689. return error;
  1690. }
  1691. generic_fillattr(inode, stat);
  1692. if (gfs2_holder_initialized(&gh))
  1693. gfs2_glock_dq_uninit(&gh);
  1694. return 0;
  1695. }
  1696. static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  1697. u64 start, u64 len)
  1698. {
  1699. struct gfs2_inode *ip = GFS2_I(inode);
  1700. struct gfs2_holder gh;
  1701. int ret;
  1702. ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
  1703. if (ret)
  1704. return ret;
  1705. inode_lock(inode);
  1706. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  1707. if (ret)
  1708. goto out;
  1709. if (gfs2_is_stuffed(ip)) {
  1710. u64 phys = ip->i_no_addr << inode->i_blkbits;
  1711. u64 size = i_size_read(inode);
  1712. u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
  1713. FIEMAP_EXTENT_DATA_INLINE;
  1714. phys += sizeof(struct gfs2_dinode);
  1715. phys += start;
  1716. if (start + len > size)
  1717. len = size - start;
  1718. if (start < size)
  1719. ret = fiemap_fill_next_extent(fieinfo, start, phys,
  1720. len, flags);
  1721. if (ret == 1)
  1722. ret = 0;
  1723. } else {
  1724. ret = __generic_block_fiemap(inode, fieinfo, start, len,
  1725. gfs2_block_map);
  1726. }
  1727. gfs2_glock_dq_uninit(&gh);
  1728. out:
  1729. inode_unlock(inode);
  1730. return ret;
  1731. }
  1732. const struct inode_operations gfs2_file_iops = {
  1733. .permission = gfs2_permission,
  1734. .setattr = gfs2_setattr,
  1735. .getattr = gfs2_getattr,
  1736. .setxattr = generic_setxattr,
  1737. .getxattr = generic_getxattr,
  1738. .listxattr = gfs2_listxattr,
  1739. .removexattr = generic_removexattr,
  1740. .fiemap = gfs2_fiemap,
  1741. .get_acl = gfs2_get_acl,
  1742. .set_acl = gfs2_set_acl,
  1743. };
  1744. const struct inode_operations gfs2_dir_iops = {
  1745. .create = gfs2_create,
  1746. .lookup = gfs2_lookup,
  1747. .link = gfs2_link,
  1748. .unlink = gfs2_unlink,
  1749. .symlink = gfs2_symlink,
  1750. .mkdir = gfs2_mkdir,
  1751. .rmdir = gfs2_unlink,
  1752. .mknod = gfs2_mknod,
  1753. .rename2 = gfs2_rename2,
  1754. .permission = gfs2_permission,
  1755. .setattr = gfs2_setattr,
  1756. .getattr = gfs2_getattr,
  1757. .setxattr = generic_setxattr,
  1758. .getxattr = generic_getxattr,
  1759. .listxattr = gfs2_listxattr,
  1760. .removexattr = generic_removexattr,
  1761. .fiemap = gfs2_fiemap,
  1762. .get_acl = gfs2_get_acl,
  1763. .set_acl = gfs2_set_acl,
  1764. .atomic_open = gfs2_atomic_open,
  1765. };
  1766. const struct inode_operations gfs2_symlink_iops = {
  1767. .readlink = generic_readlink,
  1768. .get_link = gfs2_get_link,
  1769. .permission = gfs2_permission,
  1770. .setattr = gfs2_setattr,
  1771. .getattr = gfs2_getattr,
  1772. .setxattr = generic_setxattr,
  1773. .getxattr = generic_getxattr,
  1774. .listxattr = gfs2_listxattr,
  1775. .removexattr = generic_removexattr,
  1776. .fiemap = gfs2_fiemap,
  1777. };