inode.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849
  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. struct gfs2_skip_data {
  38. u64 no_addr;
  39. int skipped;
  40. int non_block;
  41. };
  42. static int iget_test(struct inode *inode, void *opaque)
  43. {
  44. struct gfs2_inode *ip = GFS2_I(inode);
  45. struct gfs2_skip_data *data = opaque;
  46. if (ip->i_no_addr == data->no_addr) {
  47. if (data->non_block &&
  48. inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) {
  49. data->skipped = 1;
  50. return 0;
  51. }
  52. return 1;
  53. }
  54. return 0;
  55. }
  56. static int iget_set(struct inode *inode, void *opaque)
  57. {
  58. struct gfs2_inode *ip = GFS2_I(inode);
  59. struct gfs2_skip_data *data = opaque;
  60. if (data->skipped)
  61. return -ENOENT;
  62. inode->i_ino = (unsigned long)(data->no_addr);
  63. ip->i_no_addr = data->no_addr;
  64. return 0;
  65. }
  66. struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr, int non_block)
  67. {
  68. unsigned long hash = (unsigned long)no_addr;
  69. struct gfs2_skip_data data;
  70. data.no_addr = no_addr;
  71. data.skipped = 0;
  72. data.non_block = non_block;
  73. return ilookup5(sb, hash, iget_test, &data);
  74. }
  75. static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr,
  76. int non_block)
  77. {
  78. struct gfs2_skip_data data;
  79. unsigned long hash = (unsigned long)no_addr;
  80. data.no_addr = no_addr;
  81. data.skipped = 0;
  82. data.non_block = non_block;
  83. return iget5_locked(sb, hash, iget_test, iget_set, &data);
  84. }
  85. /**
  86. * gfs2_set_iop - Sets inode operations
  87. * @inode: The inode with correct i_mode filled in
  88. *
  89. * GFS2 lookup code fills in vfs inode contents based on info obtained
  90. * from directory entry inside gfs2_inode_lookup().
  91. */
  92. static void gfs2_set_iop(struct inode *inode)
  93. {
  94. struct gfs2_sbd *sdp = GFS2_SB(inode);
  95. umode_t mode = inode->i_mode;
  96. if (S_ISREG(mode)) {
  97. inode->i_op = &gfs2_file_iops;
  98. if (gfs2_localflocks(sdp))
  99. inode->i_fop = &gfs2_file_fops_nolock;
  100. else
  101. inode->i_fop = &gfs2_file_fops;
  102. } else if (S_ISDIR(mode)) {
  103. inode->i_op = &gfs2_dir_iops;
  104. if (gfs2_localflocks(sdp))
  105. inode->i_fop = &gfs2_dir_fops_nolock;
  106. else
  107. inode->i_fop = &gfs2_dir_fops;
  108. } else if (S_ISLNK(mode)) {
  109. inode->i_op = &gfs2_symlink_iops;
  110. } else {
  111. inode->i_op = &gfs2_file_iops;
  112. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  113. }
  114. }
  115. /**
  116. * gfs2_inode_lookup - Lookup an inode
  117. * @sb: The super block
  118. * @no_addr: The inode number
  119. * @type: The type of the inode
  120. * non_block: Can we block on inodes that are being freed?
  121. *
  122. * Returns: A VFS inode, or an error
  123. */
  124. struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
  125. u64 no_addr, u64 no_formal_ino, int non_block)
  126. {
  127. struct inode *inode;
  128. struct gfs2_inode *ip;
  129. struct gfs2_glock *io_gl = NULL;
  130. int error;
  131. inode = gfs2_iget(sb, no_addr, non_block);
  132. ip = GFS2_I(inode);
  133. if (!inode)
  134. return ERR_PTR(-ENOBUFS);
  135. if (inode->i_state & I_NEW) {
  136. struct gfs2_sbd *sdp = GFS2_SB(inode);
  137. ip->i_no_formal_ino = no_formal_ino;
  138. error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  139. if (unlikely(error))
  140. goto fail;
  141. ip->i_gl->gl_object = ip;
  142. error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  143. if (unlikely(error))
  144. goto fail_put;
  145. set_bit(GIF_INVALID, &ip->i_flags);
  146. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
  147. if (unlikely(error))
  148. goto fail_iopen;
  149. ip->i_iopen_gh.gh_gl->gl_object = ip;
  150. gfs2_glock_put(io_gl);
  151. io_gl = NULL;
  152. if (type == DT_UNKNOWN) {
  153. /* Inode glock must be locked already */
  154. error = gfs2_inode_refresh(GFS2_I(inode));
  155. if (error)
  156. goto fail_refresh;
  157. } else {
  158. inode->i_mode = DT2IF(type);
  159. }
  160. gfs2_set_iop(inode);
  161. unlock_new_inode(inode);
  162. }
  163. return inode;
  164. fail_refresh:
  165. ip->i_iopen_gh.gh_gl->gl_object = NULL;
  166. gfs2_glock_dq_uninit(&ip->i_iopen_gh);
  167. fail_iopen:
  168. if (io_gl)
  169. gfs2_glock_put(io_gl);
  170. fail_put:
  171. ip->i_gl->gl_object = NULL;
  172. gfs2_glock_put(ip->i_gl);
  173. fail:
  174. iget_failed(inode);
  175. return ERR_PTR(error);
  176. }
  177. struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
  178. u64 *no_formal_ino, unsigned int blktype)
  179. {
  180. struct super_block *sb = sdp->sd_vfs;
  181. struct gfs2_holder i_gh;
  182. struct inode *inode = NULL;
  183. int error;
  184. /* Must not read in block until block type is verified */
  185. error = gfs2_glock_nq_num(sdp, no_addr, &gfs2_inode_glops,
  186. LM_ST_EXCLUSIVE, GL_SKIP, &i_gh);
  187. if (error)
  188. return ERR_PTR(error);
  189. error = gfs2_check_blk_type(sdp, no_addr, blktype);
  190. if (error)
  191. goto fail;
  192. inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0, 1);
  193. if (IS_ERR(inode))
  194. goto fail;
  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. error = 0;
  204. }
  205. fail:
  206. gfs2_glock_dq_uninit(&i_gh);
  207. return error ? ERR_PTR(error) : inode;
  208. fail_iput:
  209. iput(inode);
  210. goto fail;
  211. }
  212. struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
  213. {
  214. struct qstr qstr;
  215. struct inode *inode;
  216. gfs2_str2qstr(&qstr, name);
  217. inode = gfs2_lookupi(dip, &qstr, 1);
  218. /* gfs2_lookupi has inconsistent callers: vfs
  219. * related routines expect NULL for no entry found,
  220. * gfs2_lookup_simple callers expect ENOENT
  221. * and do not check for NULL.
  222. */
  223. if (inode == NULL)
  224. return ERR_PTR(-ENOENT);
  225. else
  226. return inode;
  227. }
  228. /**
  229. * gfs2_lookupi - Look up a filename in a directory and return its inode
  230. * @d_gh: An initialized holder for the directory glock
  231. * @name: The name of the inode to look for
  232. * @is_root: If 1, ignore the caller's permissions
  233. * @i_gh: An uninitialized holder for the new inode glock
  234. *
  235. * This can be called via the VFS filldir function when NFS is doing
  236. * a readdirplus and the inode which its intending to stat isn't
  237. * already in cache. In this case we must not take the directory glock
  238. * again, since the readdir call will have already taken that lock.
  239. *
  240. * Returns: errno
  241. */
  242. struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
  243. int is_root)
  244. {
  245. struct super_block *sb = dir->i_sb;
  246. struct gfs2_inode *dip = GFS2_I(dir);
  247. struct gfs2_holder d_gh;
  248. int error = 0;
  249. struct inode *inode = NULL;
  250. int unlock = 0;
  251. if (!name->len || name->len > GFS2_FNAMESIZE)
  252. return ERR_PTR(-ENAMETOOLONG);
  253. if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
  254. (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
  255. dir == sb->s_root->d_inode)) {
  256. igrab(dir);
  257. return dir;
  258. }
  259. if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
  260. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  261. if (error)
  262. return ERR_PTR(error);
  263. unlock = 1;
  264. }
  265. if (!is_root) {
  266. error = gfs2_permission(dir, MAY_EXEC);
  267. if (error)
  268. goto out;
  269. }
  270. inode = gfs2_dir_search(dir, name);
  271. if (IS_ERR(inode))
  272. error = PTR_ERR(inode);
  273. out:
  274. if (unlock)
  275. gfs2_glock_dq_uninit(&d_gh);
  276. if (error == -ENOENT)
  277. return NULL;
  278. return inode ? inode : ERR_PTR(error);
  279. }
  280. /**
  281. * create_ok - OK to create a new on-disk inode here?
  282. * @dip: Directory in which dinode is to be created
  283. * @name: Name of new dinode
  284. * @mode:
  285. *
  286. * Returns: errno
  287. */
  288. static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
  289. unsigned int mode)
  290. {
  291. int error;
  292. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  293. if (error)
  294. return error;
  295. /* Don't create entries in an unlinked directory */
  296. if (!dip->i_inode.i_nlink)
  297. return -ENOENT;
  298. error = gfs2_dir_check(&dip->i_inode, name, NULL);
  299. switch (error) {
  300. case -ENOENT:
  301. error = 0;
  302. break;
  303. case 0:
  304. return -EEXIST;
  305. default:
  306. return error;
  307. }
  308. if (dip->i_entries == (u32)-1)
  309. return -EFBIG;
  310. if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
  311. return -EMLINK;
  312. return 0;
  313. }
  314. static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
  315. unsigned int *uid, unsigned int *gid)
  316. {
  317. if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
  318. (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
  319. if (S_ISDIR(*mode))
  320. *mode |= S_ISUID;
  321. else if (dip->i_inode.i_uid != current_fsuid())
  322. *mode &= ~07111;
  323. *uid = dip->i_inode.i_uid;
  324. } else
  325. *uid = current_fsuid();
  326. if (dip->i_inode.i_mode & S_ISGID) {
  327. if (S_ISDIR(*mode))
  328. *mode |= S_ISGID;
  329. *gid = dip->i_inode.i_gid;
  330. } else
  331. *gid = current_fsgid();
  332. }
  333. static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
  334. {
  335. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  336. int error;
  337. if (gfs2_alloc_get(dip) == NULL)
  338. return -ENOMEM;
  339. dip->i_alloc->al_requested = RES_DINODE;
  340. error = gfs2_inplace_reserve(dip);
  341. if (error)
  342. goto out;
  343. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
  344. if (error)
  345. goto out_ipreserv;
  346. error = gfs2_alloc_di(dip, no_addr, generation);
  347. gfs2_trans_end(sdp);
  348. out_ipreserv:
  349. gfs2_inplace_release(dip);
  350. out:
  351. gfs2_alloc_put(dip);
  352. return error;
  353. }
  354. static void gfs2_init_dir(struct buffer_head *dibh,
  355. const struct gfs2_inode *parent)
  356. {
  357. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  358. struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
  359. gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
  360. dent->de_inum = di->di_num; /* already GFS2 endian */
  361. dent->de_type = cpu_to_be16(DT_DIR);
  362. dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
  363. gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
  364. gfs2_inum_out(parent, dent);
  365. dent->de_type = cpu_to_be16(DT_DIR);
  366. }
  367. /**
  368. * init_dinode - Fill in a new dinode structure
  369. * @dip: The directory this inode is being created in
  370. * @gl: The glock covering the new inode
  371. * @inum: The inode number
  372. * @mode: The file permissions
  373. * @uid: The uid of the new inode
  374. * @gid: The gid of the new inode
  375. * @generation: The generation number of the new inode
  376. * @dev: The device number (if a device node)
  377. * @symname: The symlink destination (if a symlink)
  378. * @size: The inode size (ignored for directories)
  379. * @bhp: The buffer head (returned to caller)
  380. *
  381. */
  382. static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  383. const struct gfs2_inum_host *inum, unsigned int mode,
  384. unsigned int uid, unsigned int gid,
  385. const u64 *generation, dev_t dev, const char *symname,
  386. unsigned size, struct buffer_head **bhp)
  387. {
  388. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  389. struct gfs2_dinode *di;
  390. struct buffer_head *dibh;
  391. struct timespec tv = CURRENT_TIME;
  392. dibh = gfs2_meta_new(gl, inum->no_addr);
  393. gfs2_trans_add_bh(gl, dibh, 1);
  394. gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
  395. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  396. di = (struct gfs2_dinode *)dibh->b_data;
  397. di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
  398. di->di_num.no_addr = cpu_to_be64(inum->no_addr);
  399. di->di_mode = cpu_to_be32(mode);
  400. di->di_uid = cpu_to_be32(uid);
  401. di->di_gid = cpu_to_be32(gid);
  402. di->di_nlink = 0;
  403. di->di_size = cpu_to_be64(size);
  404. di->di_blocks = cpu_to_be64(1);
  405. di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
  406. di->di_major = cpu_to_be32(MAJOR(dev));
  407. di->di_minor = cpu_to_be32(MINOR(dev));
  408. di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
  409. di->di_generation = cpu_to_be64(*generation);
  410. di->di_flags = 0;
  411. di->__pad1 = 0;
  412. di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
  413. di->di_height = 0;
  414. di->__pad2 = 0;
  415. di->__pad3 = 0;
  416. di->di_depth = 0;
  417. di->di_entries = 0;
  418. memset(&di->__pad4, 0, sizeof(di->__pad4));
  419. di->di_eattr = 0;
  420. di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
  421. di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
  422. di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
  423. memset(&di->di_reserved, 0, sizeof(di->di_reserved));
  424. switch(mode & S_IFMT) {
  425. case S_IFREG:
  426. if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
  427. gfs2_tune_get(sdp, gt_new_files_jdata))
  428. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  429. break;
  430. case S_IFDIR:
  431. di->di_flags |= cpu_to_be32(dip->i_diskflags &
  432. GFS2_DIF_INHERIT_JDATA);
  433. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  434. di->di_size = cpu_to_be64(sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode));
  435. di->di_entries = cpu_to_be32(2);
  436. gfs2_init_dir(dibh, dip);
  437. break;
  438. case S_IFLNK:
  439. memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, size);
  440. break;
  441. }
  442. set_buffer_uptodate(dibh);
  443. *bhp = dibh;
  444. }
  445. static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  446. unsigned int mode, const struct gfs2_inum_host *inum,
  447. const u64 *generation, dev_t dev, const char *symname,
  448. unsigned int size, struct buffer_head **bhp)
  449. {
  450. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  451. unsigned int uid, gid;
  452. int error;
  453. munge_mode_uid_gid(dip, &mode, &uid, &gid);
  454. if (!gfs2_alloc_get(dip))
  455. return -ENOMEM;
  456. error = gfs2_quota_lock(dip, uid, gid);
  457. if (error)
  458. goto out;
  459. error = gfs2_quota_check(dip, uid, gid);
  460. if (error)
  461. goto out_quota;
  462. error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
  463. if (error)
  464. goto out_quota;
  465. init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, symname, size, bhp);
  466. gfs2_quota_change(dip, +1, uid, gid);
  467. gfs2_trans_end(sdp);
  468. out_quota:
  469. gfs2_quota_unlock(dip);
  470. out:
  471. gfs2_alloc_put(dip);
  472. return error;
  473. }
  474. static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
  475. struct gfs2_inode *ip)
  476. {
  477. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  478. struct gfs2_alloc *al;
  479. int alloc_required;
  480. struct buffer_head *dibh;
  481. int error;
  482. al = gfs2_alloc_get(dip);
  483. if (!al)
  484. return -ENOMEM;
  485. error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  486. if (error)
  487. goto fail;
  488. error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
  489. if (alloc_required < 0)
  490. goto fail_quota_locks;
  491. if (alloc_required) {
  492. error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
  493. if (error)
  494. goto fail_quota_locks;
  495. al->al_requested = sdp->sd_max_dirres;
  496. error = gfs2_inplace_reserve(dip);
  497. if (error)
  498. goto fail_quota_locks;
  499. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  500. dip->i_rgd->rd_length +
  501. 2 * RES_DINODE +
  502. RES_STATFS + RES_QUOTA, 0);
  503. if (error)
  504. goto fail_ipreserv;
  505. } else {
  506. error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
  507. if (error)
  508. goto fail_quota_locks;
  509. }
  510. error = gfs2_dir_add(&dip->i_inode, name, ip);
  511. if (error)
  512. goto fail_end_trans;
  513. error = gfs2_meta_inode_buffer(ip, &dibh);
  514. if (error)
  515. goto fail_end_trans;
  516. inc_nlink(&ip->i_inode);
  517. if (S_ISDIR(ip->i_inode.i_mode))
  518. inc_nlink(&ip->i_inode);
  519. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  520. gfs2_dinode_out(ip, dibh->b_data);
  521. brelse(dibh);
  522. return 0;
  523. fail_end_trans:
  524. gfs2_trans_end(sdp);
  525. fail_ipreserv:
  526. gfs2_inplace_release(dip);
  527. fail_quota_locks:
  528. gfs2_quota_unlock(dip);
  529. fail:
  530. gfs2_alloc_put(dip);
  531. return error;
  532. }
  533. int gfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
  534. void *fs_info)
  535. {
  536. const struct xattr *xattr;
  537. int err = 0;
  538. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  539. err = __gfs2_xattr_set(inode, xattr->name, xattr->value,
  540. xattr->value_len, 0,
  541. GFS2_EATYPE_SECURITY);
  542. if (err < 0)
  543. break;
  544. }
  545. return err;
  546. }
  547. static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip,
  548. const struct qstr *qstr)
  549. {
  550. return security_inode_init_security(&ip->i_inode, &dip->i_inode, qstr,
  551. &gfs2_initxattrs, NULL);
  552. }
  553. /**
  554. * gfs2_create_inode - Create a new inode
  555. * @dir: The parent directory
  556. * @dentry: The new dentry
  557. * @mode: The permissions on the new inode
  558. * @dev: For device nodes, this is the device number
  559. * @symname: For symlinks, this is the link destination
  560. * @size: The initial size of the inode (ignored for directories)
  561. *
  562. * Returns: 0 on success, or error code
  563. */
  564. static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
  565. unsigned int mode, dev_t dev, const char *symname,
  566. unsigned int size, int excl)
  567. {
  568. const struct qstr *name = &dentry->d_name;
  569. struct gfs2_holder ghs[2];
  570. struct inode *inode = NULL;
  571. struct gfs2_inode *dip = GFS2_I(dir);
  572. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  573. struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
  574. int error;
  575. u64 generation;
  576. struct buffer_head *bh = NULL;
  577. if (!name->len || name->len > GFS2_FNAMESIZE)
  578. return -ENAMETOOLONG;
  579. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  580. if (error)
  581. goto fail;
  582. error = create_ok(dip, name, mode);
  583. if ((error == -EEXIST) && S_ISREG(mode) && !excl) {
  584. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  585. gfs2_glock_dq_uninit(ghs);
  586. d_instantiate(dentry, inode);
  587. return IS_ERR(inode) ? PTR_ERR(inode) : 0;
  588. }
  589. if (error)
  590. goto fail_gunlock;
  591. error = alloc_dinode(dip, &inum.no_addr, &generation);
  592. if (error)
  593. goto fail_gunlock;
  594. inum.no_formal_ino = generation;
  595. error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
  596. LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
  597. if (error)
  598. goto fail_gunlock;
  599. error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, symname, size, &bh);
  600. if (error)
  601. goto fail_gunlock2;
  602. inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode), inum.no_addr,
  603. inum.no_formal_ino, 0);
  604. if (IS_ERR(inode))
  605. goto fail_gunlock2;
  606. error = gfs2_inode_refresh(GFS2_I(inode));
  607. if (error)
  608. goto fail_gunlock2;
  609. error = gfs2_acl_create(dip, inode);
  610. if (error)
  611. goto fail_gunlock2;
  612. error = gfs2_security_init(dip, GFS2_I(inode), name);
  613. if (error)
  614. goto fail_gunlock2;
  615. error = link_dinode(dip, name, GFS2_I(inode));
  616. if (error)
  617. goto fail_gunlock2;
  618. if (bh)
  619. brelse(bh);
  620. gfs2_trans_end(sdp);
  621. gfs2_inplace_release(dip);
  622. gfs2_quota_unlock(dip);
  623. gfs2_alloc_put(dip);
  624. mark_inode_dirty(inode);
  625. gfs2_glock_dq_uninit_m(2, ghs);
  626. d_instantiate(dentry, inode);
  627. return 0;
  628. fail_gunlock2:
  629. gfs2_glock_dq_uninit(ghs + 1);
  630. fail_gunlock:
  631. gfs2_glock_dq_uninit(ghs);
  632. if (inode && !IS_ERR(inode)) {
  633. set_bit(GIF_ALLOC_FAILED, &GFS2_I(inode)->i_flags);
  634. iput(inode);
  635. }
  636. fail:
  637. if (bh)
  638. brelse(bh);
  639. return error;
  640. }
  641. /**
  642. * gfs2_create - Create a file
  643. * @dir: The directory in which to create the file
  644. * @dentry: The dentry of the new file
  645. * @mode: The mode of the new file
  646. *
  647. * Returns: errno
  648. */
  649. static int gfs2_create(struct inode *dir, struct dentry *dentry,
  650. int mode, struct nameidata *nd)
  651. {
  652. int excl = 0;
  653. if (nd && (nd->flags & LOOKUP_EXCL))
  654. excl = 1;
  655. return gfs2_create_inode(dir, dentry, S_IFREG | mode, 0, NULL, 0, excl);
  656. }
  657. /**
  658. * gfs2_lookup - Look up a filename in a directory and return its inode
  659. * @dir: The directory inode
  660. * @dentry: The dentry of the new inode
  661. * @nd: passed from Linux VFS, ignored by us
  662. *
  663. * Called by the VFS layer. Lock dir and call gfs2_lookupi()
  664. *
  665. * Returns: errno
  666. */
  667. static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
  668. struct nameidata *nd)
  669. {
  670. struct inode *inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  671. if (inode && !IS_ERR(inode)) {
  672. struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
  673. struct gfs2_holder gh;
  674. int error;
  675. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  676. if (error) {
  677. iput(inode);
  678. return ERR_PTR(error);
  679. }
  680. gfs2_glock_dq_uninit(&gh);
  681. }
  682. return d_splice_alias(inode, dentry);
  683. }
  684. /**
  685. * gfs2_link - Link to a file
  686. * @old_dentry: The inode to link
  687. * @dir: Add link to this directory
  688. * @dentry: The name of the link
  689. *
  690. * Link the inode in "old_dentry" into the directory "dir" with the
  691. * name in "dentry".
  692. *
  693. * Returns: errno
  694. */
  695. static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
  696. struct dentry *dentry)
  697. {
  698. struct gfs2_inode *dip = GFS2_I(dir);
  699. struct gfs2_sbd *sdp = GFS2_SB(dir);
  700. struct inode *inode = old_dentry->d_inode;
  701. struct gfs2_inode *ip = GFS2_I(inode);
  702. struct gfs2_holder ghs[2];
  703. struct buffer_head *dibh;
  704. int alloc_required;
  705. int error;
  706. if (S_ISDIR(inode->i_mode))
  707. return -EPERM;
  708. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  709. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  710. error = gfs2_glock_nq(ghs); /* parent */
  711. if (error)
  712. goto out_parent;
  713. error = gfs2_glock_nq(ghs + 1); /* child */
  714. if (error)
  715. goto out_child;
  716. error = -ENOENT;
  717. if (inode->i_nlink == 0)
  718. goto out_gunlock;
  719. error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
  720. if (error)
  721. goto out_gunlock;
  722. error = gfs2_dir_check(dir, &dentry->d_name, NULL);
  723. switch (error) {
  724. case -ENOENT:
  725. break;
  726. case 0:
  727. error = -EEXIST;
  728. default:
  729. goto out_gunlock;
  730. }
  731. error = -EINVAL;
  732. if (!dip->i_inode.i_nlink)
  733. goto out_gunlock;
  734. error = -EFBIG;
  735. if (dip->i_entries == (u32)-1)
  736. goto out_gunlock;
  737. error = -EPERM;
  738. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  739. goto out_gunlock;
  740. error = -EINVAL;
  741. if (!ip->i_inode.i_nlink)
  742. goto out_gunlock;
  743. error = -EMLINK;
  744. if (ip->i_inode.i_nlink == (u32)-1)
  745. goto out_gunlock;
  746. alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
  747. if (error < 0)
  748. goto out_gunlock;
  749. error = 0;
  750. if (alloc_required) {
  751. struct gfs2_alloc *al = gfs2_alloc_get(dip);
  752. if (!al) {
  753. error = -ENOMEM;
  754. goto out_gunlock;
  755. }
  756. error = gfs2_quota_lock_check(dip);
  757. if (error)
  758. goto out_alloc;
  759. al->al_requested = sdp->sd_max_dirres;
  760. error = gfs2_inplace_reserve(dip);
  761. if (error)
  762. goto out_gunlock_q;
  763. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  764. gfs2_rg_blocks(dip) +
  765. 2 * RES_DINODE + RES_STATFS +
  766. RES_QUOTA, 0);
  767. if (error)
  768. goto out_ipres;
  769. } else {
  770. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
  771. if (error)
  772. goto out_ipres;
  773. }
  774. error = gfs2_meta_inode_buffer(ip, &dibh);
  775. if (error)
  776. goto out_end_trans;
  777. error = gfs2_dir_add(dir, &dentry->d_name, ip);
  778. if (error)
  779. goto out_brelse;
  780. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  781. inc_nlink(&ip->i_inode);
  782. ip->i_inode.i_ctime = CURRENT_TIME;
  783. ihold(inode);
  784. d_instantiate(dentry, inode);
  785. mark_inode_dirty(inode);
  786. out_brelse:
  787. brelse(dibh);
  788. out_end_trans:
  789. gfs2_trans_end(sdp);
  790. out_ipres:
  791. if (alloc_required)
  792. gfs2_inplace_release(dip);
  793. out_gunlock_q:
  794. if (alloc_required)
  795. gfs2_quota_unlock(dip);
  796. out_alloc:
  797. if (alloc_required)
  798. gfs2_alloc_put(dip);
  799. out_gunlock:
  800. gfs2_glock_dq(ghs + 1);
  801. out_child:
  802. gfs2_glock_dq(ghs);
  803. out_parent:
  804. gfs2_holder_uninit(ghs);
  805. gfs2_holder_uninit(ghs + 1);
  806. return error;
  807. }
  808. /*
  809. * gfs2_unlink_ok - check to see that a inode is still in a directory
  810. * @dip: the directory
  811. * @name: the name of the file
  812. * @ip: the inode
  813. *
  814. * Assumes that the lock on (at least) @dip is held.
  815. *
  816. * Returns: 0 if the parent/child relationship is correct, errno if it isn't
  817. */
  818. static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
  819. const struct gfs2_inode *ip)
  820. {
  821. int error;
  822. if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
  823. return -EPERM;
  824. if ((dip->i_inode.i_mode & S_ISVTX) &&
  825. dip->i_inode.i_uid != current_fsuid() &&
  826. ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER))
  827. return -EPERM;
  828. if (IS_APPEND(&dip->i_inode))
  829. return -EPERM;
  830. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  831. if (error)
  832. return error;
  833. error = gfs2_dir_check(&dip->i_inode, name, ip);
  834. if (error)
  835. return error;
  836. return 0;
  837. }
  838. /**
  839. * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it
  840. * @dip: The parent directory
  841. * @name: The name of the entry in the parent directory
  842. * @bh: The inode buffer for the inode to be removed
  843. * @inode: The inode to be removed
  844. *
  845. * Called with all the locks and in a transaction. This will only be
  846. * called for a directory after it has been checked to ensure it is empty.
  847. *
  848. * Returns: 0 on success, or an error
  849. */
  850. static int gfs2_unlink_inode(struct gfs2_inode *dip,
  851. const struct dentry *dentry,
  852. struct buffer_head *bh)
  853. {
  854. struct inode *inode = dentry->d_inode;
  855. struct gfs2_inode *ip = GFS2_I(inode);
  856. int error;
  857. error = gfs2_dir_del(dip, dentry);
  858. if (error)
  859. return error;
  860. ip->i_entries = 0;
  861. inode->i_ctime = CURRENT_TIME;
  862. if (S_ISDIR(inode->i_mode))
  863. clear_nlink(inode);
  864. else
  865. drop_nlink(inode);
  866. mark_inode_dirty(inode);
  867. if (inode->i_nlink == 0)
  868. gfs2_unlink_di(inode);
  869. return 0;
  870. }
  871. /**
  872. * gfs2_unlink - Unlink an inode (this does rmdir as well)
  873. * @dir: The inode of the directory containing the inode to unlink
  874. * @dentry: The file itself
  875. *
  876. * This routine uses the type of the inode as a flag to figure out
  877. * whether this is an unlink or an rmdir.
  878. *
  879. * Returns: errno
  880. */
  881. static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
  882. {
  883. struct gfs2_inode *dip = GFS2_I(dir);
  884. struct gfs2_sbd *sdp = GFS2_SB(dir);
  885. struct inode *inode = dentry->d_inode;
  886. struct gfs2_inode *ip = GFS2_I(inode);
  887. struct buffer_head *bh;
  888. struct gfs2_holder ghs[3];
  889. struct gfs2_rgrpd *rgd;
  890. int error;
  891. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  892. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  893. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  894. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  895. error = gfs2_glock_nq(ghs); /* parent */
  896. if (error)
  897. goto out_parent;
  898. error = gfs2_glock_nq(ghs + 1); /* child */
  899. if (error)
  900. goto out_child;
  901. error = -ENOENT;
  902. if (inode->i_nlink == 0)
  903. goto out_rgrp;
  904. if (S_ISDIR(inode->i_mode)) {
  905. error = -ENOTEMPTY;
  906. if (ip->i_entries > 2 || inode->i_nlink > 2)
  907. goto out_rgrp;
  908. }
  909. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  910. if (error)
  911. goto out_rgrp;
  912. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  913. if (error)
  914. goto out_gunlock;
  915. error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
  916. if (error)
  917. goto out_gunlock;
  918. error = gfs2_meta_inode_buffer(ip, &bh);
  919. if (error)
  920. goto out_end_trans;
  921. error = gfs2_unlink_inode(dip, dentry, bh);
  922. brelse(bh);
  923. out_end_trans:
  924. gfs2_trans_end(sdp);
  925. out_gunlock:
  926. gfs2_glock_dq(ghs + 2);
  927. out_rgrp:
  928. gfs2_holder_uninit(ghs + 2);
  929. gfs2_glock_dq(ghs + 1);
  930. out_child:
  931. gfs2_holder_uninit(ghs + 1);
  932. gfs2_glock_dq(ghs);
  933. out_parent:
  934. gfs2_holder_uninit(ghs);
  935. return error;
  936. }
  937. /**
  938. * gfs2_symlink - Create a symlink
  939. * @dir: The directory to create the symlink in
  940. * @dentry: The dentry to put the symlink in
  941. * @symname: The thing which the link points to
  942. *
  943. * Returns: errno
  944. */
  945. static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
  946. const char *symname)
  947. {
  948. struct gfs2_sbd *sdp = GFS2_SB(dir);
  949. unsigned int size;
  950. size = strlen(symname);
  951. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
  952. return -ENAMETOOLONG;
  953. return gfs2_create_inode(dir, dentry, S_IFLNK | S_IRWXUGO, 0, symname, size, 0);
  954. }
  955. /**
  956. * gfs2_mkdir - Make a directory
  957. * @dir: The parent directory of the new one
  958. * @dentry: The dentry of the new directory
  959. * @mode: The mode of the new directory
  960. *
  961. * Returns: errno
  962. */
  963. static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  964. {
  965. return gfs2_create_inode(dir, dentry, S_IFDIR | mode, 0, NULL, 0, 0);
  966. }
  967. /**
  968. * gfs2_mknod - Make a special file
  969. * @dir: The directory in which the special file will reside
  970. * @dentry: The dentry of the special file
  971. * @mode: The mode of the special file
  972. * @dev: The device specification of the special file
  973. *
  974. */
  975. static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
  976. dev_t dev)
  977. {
  978. return gfs2_create_inode(dir, dentry, mode, dev, NULL, 0, 0);
  979. }
  980. /*
  981. * gfs2_ok_to_move - check if it's ok to move a directory to another directory
  982. * @this: move this
  983. * @to: to here
  984. *
  985. * Follow @to back to the root and make sure we don't encounter @this
  986. * Assumes we already hold the rename lock.
  987. *
  988. * Returns: errno
  989. */
  990. static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
  991. {
  992. struct inode *dir = &to->i_inode;
  993. struct super_block *sb = dir->i_sb;
  994. struct inode *tmp;
  995. int error = 0;
  996. igrab(dir);
  997. for (;;) {
  998. if (dir == &this->i_inode) {
  999. error = -EINVAL;
  1000. break;
  1001. }
  1002. if (dir == sb->s_root->d_inode) {
  1003. error = 0;
  1004. break;
  1005. }
  1006. tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
  1007. if (IS_ERR(tmp)) {
  1008. error = PTR_ERR(tmp);
  1009. break;
  1010. }
  1011. iput(dir);
  1012. dir = tmp;
  1013. }
  1014. iput(dir);
  1015. return error;
  1016. }
  1017. /**
  1018. * gfs2_rename - Rename a file
  1019. * @odir: Parent directory of old file name
  1020. * @odentry: The old dentry of the file
  1021. * @ndir: Parent directory of new file name
  1022. * @ndentry: The new dentry of the file
  1023. *
  1024. * Returns: errno
  1025. */
  1026. static int gfs2_rename(struct inode *odir, struct dentry *odentry,
  1027. struct inode *ndir, struct dentry *ndentry)
  1028. {
  1029. struct gfs2_inode *odip = GFS2_I(odir);
  1030. struct gfs2_inode *ndip = GFS2_I(ndir);
  1031. struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
  1032. struct gfs2_inode *nip = NULL;
  1033. struct gfs2_sbd *sdp = GFS2_SB(odir);
  1034. struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
  1035. struct gfs2_rgrpd *nrgd;
  1036. unsigned int num_gh;
  1037. int dir_rename = 0;
  1038. int alloc_required = 0;
  1039. unsigned int x;
  1040. int error;
  1041. if (ndentry->d_inode) {
  1042. nip = GFS2_I(ndentry->d_inode);
  1043. if (ip == nip)
  1044. return 0;
  1045. }
  1046. if (odip != ndip) {
  1047. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
  1048. 0, &r_gh);
  1049. if (error)
  1050. goto out;
  1051. if (S_ISDIR(ip->i_inode.i_mode)) {
  1052. dir_rename = 1;
  1053. /* don't move a dirctory into it's subdir */
  1054. error = gfs2_ok_to_move(ip, ndip);
  1055. if (error)
  1056. goto out_gunlock_r;
  1057. }
  1058. }
  1059. num_gh = 1;
  1060. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  1061. if (odip != ndip) {
  1062. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1063. num_gh++;
  1064. }
  1065. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1066. num_gh++;
  1067. if (nip) {
  1068. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1069. num_gh++;
  1070. /* grab the resource lock for unlink flag twiddling
  1071. * this is the case of the target file already existing
  1072. * so we unlink before doing the rename
  1073. */
  1074. nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
  1075. if (nrgd)
  1076. gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
  1077. }
  1078. for (x = 0; x < num_gh; x++) {
  1079. error = gfs2_glock_nq(ghs + x);
  1080. if (error)
  1081. goto out_gunlock;
  1082. }
  1083. error = -ENOENT;
  1084. if (ip->i_inode.i_nlink == 0)
  1085. goto out_gunlock;
  1086. /* Check out the old directory */
  1087. error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
  1088. if (error)
  1089. goto out_gunlock;
  1090. /* Check out the new directory */
  1091. if (nip) {
  1092. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  1093. if (error)
  1094. goto out_gunlock;
  1095. if (nip->i_inode.i_nlink == 0) {
  1096. error = -EAGAIN;
  1097. goto out_gunlock;
  1098. }
  1099. if (S_ISDIR(nip->i_inode.i_mode)) {
  1100. if (nip->i_entries < 2) {
  1101. gfs2_consist_inode(nip);
  1102. error = -EIO;
  1103. goto out_gunlock;
  1104. }
  1105. if (nip->i_entries > 2) {
  1106. error = -ENOTEMPTY;
  1107. goto out_gunlock;
  1108. }
  1109. }
  1110. } else {
  1111. error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
  1112. if (error)
  1113. goto out_gunlock;
  1114. error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
  1115. switch (error) {
  1116. case -ENOENT:
  1117. error = 0;
  1118. break;
  1119. case 0:
  1120. error = -EEXIST;
  1121. default:
  1122. goto out_gunlock;
  1123. };
  1124. if (odip != ndip) {
  1125. if (!ndip->i_inode.i_nlink) {
  1126. error = -ENOENT;
  1127. goto out_gunlock;
  1128. }
  1129. if (ndip->i_entries == (u32)-1) {
  1130. error = -EFBIG;
  1131. goto out_gunlock;
  1132. }
  1133. if (S_ISDIR(ip->i_inode.i_mode) &&
  1134. ndip->i_inode.i_nlink == (u32)-1) {
  1135. error = -EMLINK;
  1136. goto out_gunlock;
  1137. }
  1138. }
  1139. }
  1140. /* Check out the dir to be renamed */
  1141. if (dir_rename) {
  1142. error = gfs2_permission(odentry->d_inode, MAY_WRITE);
  1143. if (error)
  1144. goto out_gunlock;
  1145. }
  1146. if (nip == NULL)
  1147. alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
  1148. error = alloc_required;
  1149. if (error < 0)
  1150. goto out_gunlock;
  1151. error = 0;
  1152. if (alloc_required) {
  1153. struct gfs2_alloc *al = gfs2_alloc_get(ndip);
  1154. if (!al) {
  1155. error = -ENOMEM;
  1156. goto out_gunlock;
  1157. }
  1158. error = gfs2_quota_lock_check(ndip);
  1159. if (error)
  1160. goto out_alloc;
  1161. al->al_requested = sdp->sd_max_dirres;
  1162. error = gfs2_inplace_reserve(ndip);
  1163. if (error)
  1164. goto out_gunlock_q;
  1165. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  1166. gfs2_rg_blocks(ndip) +
  1167. 4 * RES_DINODE + 4 * RES_LEAF +
  1168. RES_STATFS + RES_QUOTA + 4, 0);
  1169. if (error)
  1170. goto out_ipreserv;
  1171. } else {
  1172. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  1173. 5 * RES_LEAF + 4, 0);
  1174. if (error)
  1175. goto out_gunlock;
  1176. }
  1177. /* Remove the target file, if it exists */
  1178. if (nip) {
  1179. struct buffer_head *bh;
  1180. error = gfs2_meta_inode_buffer(nip, &bh);
  1181. if (error)
  1182. goto out_end_trans;
  1183. error = gfs2_unlink_inode(ndip, ndentry, bh);
  1184. brelse(bh);
  1185. }
  1186. if (dir_rename) {
  1187. error = gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
  1188. if (error)
  1189. goto out_end_trans;
  1190. } else {
  1191. struct buffer_head *dibh;
  1192. error = gfs2_meta_inode_buffer(ip, &dibh);
  1193. if (error)
  1194. goto out_end_trans;
  1195. ip->i_inode.i_ctime = CURRENT_TIME;
  1196. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1197. gfs2_dinode_out(ip, dibh->b_data);
  1198. brelse(dibh);
  1199. }
  1200. error = gfs2_dir_del(odip, odentry);
  1201. if (error)
  1202. goto out_end_trans;
  1203. error = gfs2_dir_add(ndir, &ndentry->d_name, ip);
  1204. if (error)
  1205. goto out_end_trans;
  1206. out_end_trans:
  1207. gfs2_trans_end(sdp);
  1208. out_ipreserv:
  1209. if (alloc_required)
  1210. gfs2_inplace_release(ndip);
  1211. out_gunlock_q:
  1212. if (alloc_required)
  1213. gfs2_quota_unlock(ndip);
  1214. out_alloc:
  1215. if (alloc_required)
  1216. gfs2_alloc_put(ndip);
  1217. out_gunlock:
  1218. while (x--) {
  1219. gfs2_glock_dq(ghs + x);
  1220. gfs2_holder_uninit(ghs + x);
  1221. }
  1222. out_gunlock_r:
  1223. if (r_gh.gh_gl)
  1224. gfs2_glock_dq_uninit(&r_gh);
  1225. out:
  1226. return error;
  1227. }
  1228. /**
  1229. * gfs2_follow_link - Follow a symbolic link
  1230. * @dentry: The dentry of the link
  1231. * @nd: Data that we pass to vfs_follow_link()
  1232. *
  1233. * This can handle symlinks of any size.
  1234. *
  1235. * Returns: 0 on success or error code
  1236. */
  1237. static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
  1238. {
  1239. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  1240. struct gfs2_holder i_gh;
  1241. struct buffer_head *dibh;
  1242. unsigned int size;
  1243. char *buf;
  1244. int error;
  1245. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  1246. error = gfs2_glock_nq(&i_gh);
  1247. if (error) {
  1248. gfs2_holder_uninit(&i_gh);
  1249. nd_set_link(nd, ERR_PTR(error));
  1250. return NULL;
  1251. }
  1252. size = (unsigned int)i_size_read(&ip->i_inode);
  1253. if (size == 0) {
  1254. gfs2_consist_inode(ip);
  1255. buf = ERR_PTR(-EIO);
  1256. goto out;
  1257. }
  1258. error = gfs2_meta_inode_buffer(ip, &dibh);
  1259. if (error) {
  1260. buf = ERR_PTR(error);
  1261. goto out;
  1262. }
  1263. buf = kzalloc(size + 1, GFP_NOFS);
  1264. if (!buf)
  1265. buf = ERR_PTR(-ENOMEM);
  1266. else
  1267. memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
  1268. brelse(dibh);
  1269. out:
  1270. gfs2_glock_dq_uninit(&i_gh);
  1271. nd_set_link(nd, buf);
  1272. return NULL;
  1273. }
  1274. static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
  1275. {
  1276. char *s = nd_get_link(nd);
  1277. if (!IS_ERR(s))
  1278. kfree(s);
  1279. }
  1280. /**
  1281. * gfs2_permission -
  1282. * @inode: The inode
  1283. * @mask: The mask to be tested
  1284. * @flags: Indicates whether this is an RCU path walk or not
  1285. *
  1286. * This may be called from the VFS directly, or from within GFS2 with the
  1287. * inode locked, so we look to see if the glock is already locked and only
  1288. * lock the glock if its not already been done.
  1289. *
  1290. * Returns: errno
  1291. */
  1292. int gfs2_permission(struct inode *inode, int mask)
  1293. {
  1294. struct gfs2_inode *ip;
  1295. struct gfs2_holder i_gh;
  1296. int error;
  1297. int unlock = 0;
  1298. ip = GFS2_I(inode);
  1299. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1300. if (mask & MAY_NOT_BLOCK)
  1301. return -ECHILD;
  1302. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  1303. if (error)
  1304. return error;
  1305. unlock = 1;
  1306. }
  1307. if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
  1308. error = -EACCES;
  1309. else
  1310. error = generic_permission(inode, mask);
  1311. if (unlock)
  1312. gfs2_glock_dq_uninit(&i_gh);
  1313. return error;
  1314. }
  1315. static int __gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
  1316. {
  1317. setattr_copy(inode, attr);
  1318. mark_inode_dirty(inode);
  1319. return 0;
  1320. }
  1321. /**
  1322. * gfs2_setattr_simple -
  1323. * @ip:
  1324. * @attr:
  1325. *
  1326. * Returns: errno
  1327. */
  1328. int gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
  1329. {
  1330. int error;
  1331. if (current->journal_info)
  1332. return __gfs2_setattr_simple(inode, attr);
  1333. error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0);
  1334. if (error)
  1335. return error;
  1336. error = __gfs2_setattr_simple(inode, attr);
  1337. gfs2_trans_end(GFS2_SB(inode));
  1338. return error;
  1339. }
  1340. static int setattr_chown(struct inode *inode, struct iattr *attr)
  1341. {
  1342. struct gfs2_inode *ip = GFS2_I(inode);
  1343. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1344. u32 ouid, ogid, nuid, ngid;
  1345. int error;
  1346. ouid = inode->i_uid;
  1347. ogid = inode->i_gid;
  1348. nuid = attr->ia_uid;
  1349. ngid = attr->ia_gid;
  1350. if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
  1351. ouid = nuid = NO_QUOTA_CHANGE;
  1352. if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
  1353. ogid = ngid = NO_QUOTA_CHANGE;
  1354. if (!gfs2_alloc_get(ip))
  1355. return -ENOMEM;
  1356. error = gfs2_quota_lock(ip, nuid, ngid);
  1357. if (error)
  1358. goto out_alloc;
  1359. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  1360. error = gfs2_quota_check(ip, nuid, ngid);
  1361. if (error)
  1362. goto out_gunlock_q;
  1363. }
  1364. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  1365. if (error)
  1366. goto out_gunlock_q;
  1367. error = gfs2_setattr_simple(inode, attr);
  1368. if (error)
  1369. goto out_end_trans;
  1370. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  1371. u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
  1372. gfs2_quota_change(ip, -blocks, ouid, ogid);
  1373. gfs2_quota_change(ip, blocks, nuid, ngid);
  1374. }
  1375. out_end_trans:
  1376. gfs2_trans_end(sdp);
  1377. out_gunlock_q:
  1378. gfs2_quota_unlock(ip);
  1379. out_alloc:
  1380. gfs2_alloc_put(ip);
  1381. return error;
  1382. }
  1383. /**
  1384. * gfs2_setattr - Change attributes on an inode
  1385. * @dentry: The dentry which is changing
  1386. * @attr: The structure describing the change
  1387. *
  1388. * The VFS layer wants to change one or more of an inodes attributes. Write
  1389. * that change out to disk.
  1390. *
  1391. * Returns: errno
  1392. */
  1393. static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
  1394. {
  1395. struct inode *inode = dentry->d_inode;
  1396. struct gfs2_inode *ip = GFS2_I(inode);
  1397. struct gfs2_holder i_gh;
  1398. int error;
  1399. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  1400. if (error)
  1401. return error;
  1402. error = -EPERM;
  1403. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  1404. goto out;
  1405. error = inode_change_ok(inode, attr);
  1406. if (error)
  1407. goto out;
  1408. if (attr->ia_valid & ATTR_SIZE)
  1409. error = gfs2_setattr_size(inode, attr->ia_size);
  1410. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  1411. error = setattr_chown(inode, attr);
  1412. else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
  1413. error = gfs2_acl_chmod(ip, attr);
  1414. else
  1415. error = gfs2_setattr_simple(inode, attr);
  1416. out:
  1417. if (!error)
  1418. mark_inode_dirty(inode);
  1419. gfs2_glock_dq_uninit(&i_gh);
  1420. return error;
  1421. }
  1422. /**
  1423. * gfs2_getattr - Read out an inode's attributes
  1424. * @mnt: The vfsmount the inode is being accessed from
  1425. * @dentry: The dentry to stat
  1426. * @stat: The inode's stats
  1427. *
  1428. * This may be called from the VFS directly, or from within GFS2 with the
  1429. * inode locked, so we look to see if the glock is already locked and only
  1430. * lock the glock if its not already been done. Note that its the NFS
  1431. * readdirplus operation which causes this to be called (from filldir)
  1432. * with the glock already held.
  1433. *
  1434. * Returns: errno
  1435. */
  1436. static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1437. struct kstat *stat)
  1438. {
  1439. struct inode *inode = dentry->d_inode;
  1440. struct gfs2_inode *ip = GFS2_I(inode);
  1441. struct gfs2_holder gh;
  1442. int error;
  1443. int unlock = 0;
  1444. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1445. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1446. if (error)
  1447. return error;
  1448. unlock = 1;
  1449. }
  1450. generic_fillattr(inode, stat);
  1451. if (unlock)
  1452. gfs2_glock_dq_uninit(&gh);
  1453. return 0;
  1454. }
  1455. static int gfs2_setxattr(struct dentry *dentry, const char *name,
  1456. const void *data, size_t size, int flags)
  1457. {
  1458. struct inode *inode = dentry->d_inode;
  1459. struct gfs2_inode *ip = GFS2_I(inode);
  1460. struct gfs2_holder gh;
  1461. int ret;
  1462. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1463. ret = gfs2_glock_nq(&gh);
  1464. if (ret == 0) {
  1465. ret = generic_setxattr(dentry, name, data, size, flags);
  1466. gfs2_glock_dq(&gh);
  1467. }
  1468. gfs2_holder_uninit(&gh);
  1469. return ret;
  1470. }
  1471. static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
  1472. void *data, size_t size)
  1473. {
  1474. struct inode *inode = dentry->d_inode;
  1475. struct gfs2_inode *ip = GFS2_I(inode);
  1476. struct gfs2_holder gh;
  1477. int ret;
  1478. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1479. ret = gfs2_glock_nq(&gh);
  1480. if (ret == 0) {
  1481. ret = generic_getxattr(dentry, name, data, size);
  1482. gfs2_glock_dq(&gh);
  1483. }
  1484. gfs2_holder_uninit(&gh);
  1485. return ret;
  1486. }
  1487. static int gfs2_removexattr(struct dentry *dentry, const char *name)
  1488. {
  1489. struct inode *inode = dentry->d_inode;
  1490. struct gfs2_inode *ip = GFS2_I(inode);
  1491. struct gfs2_holder gh;
  1492. int ret;
  1493. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1494. ret = gfs2_glock_nq(&gh);
  1495. if (ret == 0) {
  1496. ret = generic_removexattr(dentry, name);
  1497. gfs2_glock_dq(&gh);
  1498. }
  1499. gfs2_holder_uninit(&gh);
  1500. return ret;
  1501. }
  1502. static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  1503. u64 start, u64 len)
  1504. {
  1505. struct gfs2_inode *ip = GFS2_I(inode);
  1506. struct gfs2_holder gh;
  1507. int ret;
  1508. ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
  1509. if (ret)
  1510. return ret;
  1511. mutex_lock(&inode->i_mutex);
  1512. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  1513. if (ret)
  1514. goto out;
  1515. if (gfs2_is_stuffed(ip)) {
  1516. u64 phys = ip->i_no_addr << inode->i_blkbits;
  1517. u64 size = i_size_read(inode);
  1518. u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
  1519. FIEMAP_EXTENT_DATA_INLINE;
  1520. phys += sizeof(struct gfs2_dinode);
  1521. phys += start;
  1522. if (start + len > size)
  1523. len = size - start;
  1524. if (start < size)
  1525. ret = fiemap_fill_next_extent(fieinfo, start, phys,
  1526. len, flags);
  1527. if (ret == 1)
  1528. ret = 0;
  1529. } else {
  1530. ret = __generic_block_fiemap(inode, fieinfo, start, len,
  1531. gfs2_block_map);
  1532. }
  1533. gfs2_glock_dq_uninit(&gh);
  1534. out:
  1535. mutex_unlock(&inode->i_mutex);
  1536. return ret;
  1537. }
  1538. const struct inode_operations gfs2_file_iops = {
  1539. .permission = gfs2_permission,
  1540. .setattr = gfs2_setattr,
  1541. .getattr = gfs2_getattr,
  1542. .setxattr = gfs2_setxattr,
  1543. .getxattr = gfs2_getxattr,
  1544. .listxattr = gfs2_listxattr,
  1545. .removexattr = gfs2_removexattr,
  1546. .fiemap = gfs2_fiemap,
  1547. .get_acl = gfs2_get_acl,
  1548. };
  1549. const struct inode_operations gfs2_dir_iops = {
  1550. .create = gfs2_create,
  1551. .lookup = gfs2_lookup,
  1552. .link = gfs2_link,
  1553. .unlink = gfs2_unlink,
  1554. .symlink = gfs2_symlink,
  1555. .mkdir = gfs2_mkdir,
  1556. .rmdir = gfs2_unlink,
  1557. .mknod = gfs2_mknod,
  1558. .rename = gfs2_rename,
  1559. .permission = gfs2_permission,
  1560. .setattr = gfs2_setattr,
  1561. .getattr = gfs2_getattr,
  1562. .setxattr = gfs2_setxattr,
  1563. .getxattr = gfs2_getxattr,
  1564. .listxattr = gfs2_listxattr,
  1565. .removexattr = gfs2_removexattr,
  1566. .fiemap = gfs2_fiemap,
  1567. .get_acl = gfs2_get_acl,
  1568. };
  1569. const struct inode_operations gfs2_symlink_iops = {
  1570. .readlink = generic_readlink,
  1571. .follow_link = gfs2_follow_link,
  1572. .put_link = gfs2_put_link,
  1573. .permission = gfs2_permission,
  1574. .setattr = gfs2_setattr,
  1575. .getattr = gfs2_getattr,
  1576. .setxattr = gfs2_setxattr,
  1577. .getxattr = gfs2_getxattr,
  1578. .listxattr = gfs2_listxattr,
  1579. .removexattr = gfs2_removexattr,
  1580. .fiemap = gfs2_fiemap,
  1581. .get_acl = gfs2_get_acl,
  1582. };