inode.c 51 KB

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