inode.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561
  1. /*
  2. * fs/cifs/inode.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2010
  5. * Author(s): Steve French (sfrench@us.ibm.com)
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published
  9. * by the Free Software Foundation; either version 2.1 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  15. * the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/stat.h>
  23. #include <linux/slab.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/freezer.h>
  26. #include <linux/sched/signal.h>
  27. #include <linux/wait_bit.h>
  28. #include <asm/div64.h>
  29. #include "cifsfs.h"
  30. #include "cifspdu.h"
  31. #include "cifsglob.h"
  32. #include "cifsproto.h"
  33. #include "cifs_debug.h"
  34. #include "cifs_fs_sb.h"
  35. #include "cifs_unicode.h"
  36. #include "fscache.h"
  37. static void cifs_set_ops(struct inode *inode)
  38. {
  39. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  40. switch (inode->i_mode & S_IFMT) {
  41. case S_IFREG:
  42. inode->i_op = &cifs_file_inode_ops;
  43. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  44. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  45. inode->i_fop = &cifs_file_direct_nobrl_ops;
  46. else
  47. inode->i_fop = &cifs_file_direct_ops;
  48. } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
  49. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  50. inode->i_fop = &cifs_file_strict_nobrl_ops;
  51. else
  52. inode->i_fop = &cifs_file_strict_ops;
  53. } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  54. inode->i_fop = &cifs_file_nobrl_ops;
  55. else { /* not direct, send byte range locks */
  56. inode->i_fop = &cifs_file_ops;
  57. }
  58. /* check if server can support readpages */
  59. if (cifs_sb_master_tcon(cifs_sb)->ses->server->maxBuf <
  60. PAGE_SIZE + MAX_CIFS_HDR_SIZE)
  61. inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
  62. else
  63. inode->i_data.a_ops = &cifs_addr_ops;
  64. break;
  65. case S_IFDIR:
  66. #ifdef CONFIG_CIFS_DFS_UPCALL
  67. if (IS_AUTOMOUNT(inode)) {
  68. inode->i_op = &cifs_dfs_referral_inode_operations;
  69. } else {
  70. #else /* NO DFS support, treat as a directory */
  71. {
  72. #endif
  73. inode->i_op = &cifs_dir_inode_ops;
  74. inode->i_fop = &cifs_dir_ops;
  75. }
  76. break;
  77. case S_IFLNK:
  78. inode->i_op = &cifs_symlink_inode_ops;
  79. break;
  80. default:
  81. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  82. break;
  83. }
  84. }
  85. /* check inode attributes against fattr. If they don't match, tag the
  86. * inode for cache invalidation
  87. */
  88. static void
  89. cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
  90. {
  91. struct cifsInodeInfo *cifs_i = CIFS_I(inode);
  92. cifs_dbg(FYI, "%s: revalidating inode %llu\n",
  93. __func__, cifs_i->uniqueid);
  94. if (inode->i_state & I_NEW) {
  95. cifs_dbg(FYI, "%s: inode %llu is new\n",
  96. __func__, cifs_i->uniqueid);
  97. return;
  98. }
  99. /* don't bother with revalidation if we have an oplock */
  100. if (CIFS_CACHE_READ(cifs_i)) {
  101. cifs_dbg(FYI, "%s: inode %llu is oplocked\n",
  102. __func__, cifs_i->uniqueid);
  103. return;
  104. }
  105. /* revalidate if mtime or size have changed */
  106. if (timespec64_equal(&inode->i_mtime, &fattr->cf_mtime) &&
  107. cifs_i->server_eof == fattr->cf_eof) {
  108. cifs_dbg(FYI, "%s: inode %llu is unchanged\n",
  109. __func__, cifs_i->uniqueid);
  110. return;
  111. }
  112. cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n",
  113. __func__, cifs_i->uniqueid);
  114. set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
  115. }
  116. /*
  117. * copy nlink to the inode, unless it wasn't provided. Provide
  118. * sane values if we don't have an existing one and none was provided
  119. */
  120. static void
  121. cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
  122. {
  123. /*
  124. * if we're in a situation where we can't trust what we
  125. * got from the server (readdir, some non-unix cases)
  126. * fake reasonable values
  127. */
  128. if (fattr->cf_flags & CIFS_FATTR_UNKNOWN_NLINK) {
  129. /* only provide fake values on a new inode */
  130. if (inode->i_state & I_NEW) {
  131. if (fattr->cf_cifsattrs & ATTR_DIRECTORY)
  132. set_nlink(inode, 2);
  133. else
  134. set_nlink(inode, 1);
  135. }
  136. return;
  137. }
  138. /* we trust the server, so update it */
  139. set_nlink(inode, fattr->cf_nlink);
  140. }
  141. /* populate an inode with info from a cifs_fattr struct */
  142. void
  143. cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
  144. {
  145. struct cifsInodeInfo *cifs_i = CIFS_I(inode);
  146. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  147. cifs_revalidate_cache(inode, fattr);
  148. spin_lock(&inode->i_lock);
  149. inode->i_atime = fattr->cf_atime;
  150. inode->i_mtime = fattr->cf_mtime;
  151. inode->i_ctime = fattr->cf_ctime;
  152. inode->i_rdev = fattr->cf_rdev;
  153. cifs_nlink_fattr_to_inode(inode, fattr);
  154. inode->i_uid = fattr->cf_uid;
  155. inode->i_gid = fattr->cf_gid;
  156. /* if dynperm is set, don't clobber existing mode */
  157. if (inode->i_state & I_NEW ||
  158. !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
  159. inode->i_mode = fattr->cf_mode;
  160. cifs_i->cifsAttrs = fattr->cf_cifsattrs;
  161. if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
  162. cifs_i->time = 0;
  163. else
  164. cifs_i->time = jiffies;
  165. if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING)
  166. set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
  167. else
  168. clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
  169. cifs_i->server_eof = fattr->cf_eof;
  170. /*
  171. * Can't safely change the file size here if the client is writing to
  172. * it due to potential races.
  173. */
  174. if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
  175. i_size_write(inode, fattr->cf_eof);
  176. /*
  177. * i_blocks is not related to (i_size / i_blksize),
  178. * but instead 512 byte (2**9) size is required for
  179. * calculating num blocks.
  180. */
  181. inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
  182. }
  183. spin_unlock(&inode->i_lock);
  184. if (fattr->cf_flags & CIFS_FATTR_DFS_REFERRAL)
  185. inode->i_flags |= S_AUTOMOUNT;
  186. if (inode->i_state & I_NEW)
  187. cifs_set_ops(inode);
  188. }
  189. void
  190. cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr)
  191. {
  192. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  193. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
  194. return;
  195. fattr->cf_uniqueid = iunique(sb, ROOT_I);
  196. }
  197. /* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
  198. void
  199. cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
  200. struct cifs_sb_info *cifs_sb)
  201. {
  202. memset(fattr, 0, sizeof(*fattr));
  203. fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
  204. fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
  205. fattr->cf_eof = le64_to_cpu(info->EndOfFile);
  206. fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
  207. fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
  208. fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
  209. /* old POSIX extensions don't get create time */
  210. fattr->cf_mode = le64_to_cpu(info->Permissions);
  211. /*
  212. * Since we set the inode type below we need to mask off
  213. * to avoid strange results if bits set above.
  214. */
  215. fattr->cf_mode &= ~S_IFMT;
  216. switch (le32_to_cpu(info->Type)) {
  217. case UNIX_FILE:
  218. fattr->cf_mode |= S_IFREG;
  219. fattr->cf_dtype = DT_REG;
  220. break;
  221. case UNIX_SYMLINK:
  222. fattr->cf_mode |= S_IFLNK;
  223. fattr->cf_dtype = DT_LNK;
  224. break;
  225. case UNIX_DIR:
  226. fattr->cf_mode |= S_IFDIR;
  227. fattr->cf_dtype = DT_DIR;
  228. break;
  229. case UNIX_CHARDEV:
  230. fattr->cf_mode |= S_IFCHR;
  231. fattr->cf_dtype = DT_CHR;
  232. fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
  233. le64_to_cpu(info->DevMinor) & MINORMASK);
  234. break;
  235. case UNIX_BLOCKDEV:
  236. fattr->cf_mode |= S_IFBLK;
  237. fattr->cf_dtype = DT_BLK;
  238. fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
  239. le64_to_cpu(info->DevMinor) & MINORMASK);
  240. break;
  241. case UNIX_FIFO:
  242. fattr->cf_mode |= S_IFIFO;
  243. fattr->cf_dtype = DT_FIFO;
  244. break;
  245. case UNIX_SOCKET:
  246. fattr->cf_mode |= S_IFSOCK;
  247. fattr->cf_dtype = DT_SOCK;
  248. break;
  249. default:
  250. /* safest to call it a file if we do not know */
  251. fattr->cf_mode |= S_IFREG;
  252. fattr->cf_dtype = DT_REG;
  253. cifs_dbg(FYI, "unknown type %d\n", le32_to_cpu(info->Type));
  254. break;
  255. }
  256. fattr->cf_uid = cifs_sb->mnt_uid;
  257. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)) {
  258. u64 id = le64_to_cpu(info->Uid);
  259. if (id < ((uid_t)-1)) {
  260. kuid_t uid = make_kuid(&init_user_ns, id);
  261. if (uid_valid(uid))
  262. fattr->cf_uid = uid;
  263. }
  264. }
  265. fattr->cf_gid = cifs_sb->mnt_gid;
  266. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)) {
  267. u64 id = le64_to_cpu(info->Gid);
  268. if (id < ((gid_t)-1)) {
  269. kgid_t gid = make_kgid(&init_user_ns, id);
  270. if (gid_valid(gid))
  271. fattr->cf_gid = gid;
  272. }
  273. }
  274. fattr->cf_nlink = le64_to_cpu(info->Nlinks);
  275. }
  276. /*
  277. * Fill a cifs_fattr struct with fake inode info.
  278. *
  279. * Needed to setup cifs_fattr data for the directory which is the
  280. * junction to the new submount (ie to setup the fake directory
  281. * which represents a DFS referral).
  282. */
  283. static void
  284. cifs_create_dfs_fattr(struct cifs_fattr *fattr, struct super_block *sb)
  285. {
  286. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  287. cifs_dbg(FYI, "creating fake fattr for DFS referral\n");
  288. memset(fattr, 0, sizeof(*fattr));
  289. fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
  290. fattr->cf_uid = cifs_sb->mnt_uid;
  291. fattr->cf_gid = cifs_sb->mnt_gid;
  292. ktime_get_real_ts64(&fattr->cf_mtime);
  293. fattr->cf_mtime = timespec64_trunc(fattr->cf_mtime, sb->s_time_gran);
  294. fattr->cf_atime = fattr->cf_ctime = fattr->cf_mtime;
  295. fattr->cf_nlink = 2;
  296. fattr->cf_flags |= CIFS_FATTR_DFS_REFERRAL;
  297. }
  298. static int
  299. cifs_get_file_info_unix(struct file *filp)
  300. {
  301. int rc;
  302. unsigned int xid;
  303. FILE_UNIX_BASIC_INFO find_data;
  304. struct cifs_fattr fattr;
  305. struct inode *inode = file_inode(filp);
  306. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  307. struct cifsFileInfo *cfile = filp->private_data;
  308. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  309. xid = get_xid();
  310. rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data);
  311. if (!rc) {
  312. cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
  313. } else if (rc == -EREMOTE) {
  314. cifs_create_dfs_fattr(&fattr, inode->i_sb);
  315. rc = 0;
  316. }
  317. cifs_fattr_to_inode(inode, &fattr);
  318. free_xid(xid);
  319. return rc;
  320. }
  321. int cifs_get_inode_info_unix(struct inode **pinode,
  322. const unsigned char *full_path,
  323. struct super_block *sb, unsigned int xid)
  324. {
  325. int rc;
  326. FILE_UNIX_BASIC_INFO find_data;
  327. struct cifs_fattr fattr;
  328. struct cifs_tcon *tcon;
  329. struct tcon_link *tlink;
  330. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  331. cifs_dbg(FYI, "Getting info on %s\n", full_path);
  332. tlink = cifs_sb_tlink(cifs_sb);
  333. if (IS_ERR(tlink))
  334. return PTR_ERR(tlink);
  335. tcon = tlink_tcon(tlink);
  336. /* could have done a find first instead but this returns more info */
  337. rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
  338. cifs_sb->local_nls, cifs_remap(cifs_sb));
  339. cifs_put_tlink(tlink);
  340. if (!rc) {
  341. cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
  342. } else if (rc == -EREMOTE) {
  343. cifs_create_dfs_fattr(&fattr, sb);
  344. rc = 0;
  345. } else {
  346. return rc;
  347. }
  348. /* check for Minshall+French symlinks */
  349. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
  350. int tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
  351. full_path);
  352. if (tmprc)
  353. cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
  354. }
  355. if (*pinode == NULL) {
  356. /* get new inode */
  357. cifs_fill_uniqueid(sb, &fattr);
  358. *pinode = cifs_iget(sb, &fattr);
  359. if (!*pinode)
  360. rc = -ENOMEM;
  361. } else {
  362. /* we already have inode, update it */
  363. /* if uniqueid is different, return error */
  364. if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
  365. CIFS_I(*pinode)->uniqueid != fattr.cf_uniqueid)) {
  366. CIFS_I(*pinode)->time = 0; /* force reval */
  367. rc = -ESTALE;
  368. goto cgiiu_exit;
  369. }
  370. /* if filetype is different, return error */
  371. if (unlikely(((*pinode)->i_mode & S_IFMT) !=
  372. (fattr.cf_mode & S_IFMT))) {
  373. CIFS_I(*pinode)->time = 0; /* force reval */
  374. rc = -ESTALE;
  375. goto cgiiu_exit;
  376. }
  377. cifs_fattr_to_inode(*pinode, &fattr);
  378. }
  379. cgiiu_exit:
  380. return rc;
  381. }
  382. static int
  383. cifs_sfu_type(struct cifs_fattr *fattr, const char *path,
  384. struct cifs_sb_info *cifs_sb, unsigned int xid)
  385. {
  386. int rc;
  387. __u32 oplock;
  388. struct tcon_link *tlink;
  389. struct cifs_tcon *tcon;
  390. struct cifs_fid fid;
  391. struct cifs_open_parms oparms;
  392. struct cifs_io_parms io_parms;
  393. char buf[24];
  394. unsigned int bytes_read;
  395. char *pbuf;
  396. int buf_type = CIFS_NO_BUFFER;
  397. pbuf = buf;
  398. fattr->cf_mode &= ~S_IFMT;
  399. if (fattr->cf_eof == 0) {
  400. fattr->cf_mode |= S_IFIFO;
  401. fattr->cf_dtype = DT_FIFO;
  402. return 0;
  403. } else if (fattr->cf_eof < 8) {
  404. fattr->cf_mode |= S_IFREG;
  405. fattr->cf_dtype = DT_REG;
  406. return -EINVAL; /* EOPNOTSUPP? */
  407. }
  408. tlink = cifs_sb_tlink(cifs_sb);
  409. if (IS_ERR(tlink))
  410. return PTR_ERR(tlink);
  411. tcon = tlink_tcon(tlink);
  412. oparms.tcon = tcon;
  413. oparms.cifs_sb = cifs_sb;
  414. oparms.desired_access = GENERIC_READ;
  415. oparms.create_options = CREATE_NOT_DIR;
  416. if (backup_cred(cifs_sb))
  417. oparms.create_options |= CREATE_OPEN_BACKUP_INTENT;
  418. oparms.disposition = FILE_OPEN;
  419. oparms.path = path;
  420. oparms.fid = &fid;
  421. oparms.reconnect = false;
  422. if (tcon->ses->server->oplocks)
  423. oplock = REQ_OPLOCK;
  424. else
  425. oplock = 0;
  426. rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
  427. if (rc) {
  428. cifs_dbg(FYI, "check sfu type of %s, open rc = %d\n", path, rc);
  429. cifs_put_tlink(tlink);
  430. return rc;
  431. }
  432. /* Read header */
  433. io_parms.netfid = fid.netfid;
  434. io_parms.pid = current->tgid;
  435. io_parms.tcon = tcon;
  436. io_parms.offset = 0;
  437. io_parms.length = 24;
  438. rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms,
  439. &bytes_read, &pbuf, &buf_type);
  440. if ((rc == 0) && (bytes_read >= 8)) {
  441. if (memcmp("IntxBLK", pbuf, 8) == 0) {
  442. cifs_dbg(FYI, "Block device\n");
  443. fattr->cf_mode |= S_IFBLK;
  444. fattr->cf_dtype = DT_BLK;
  445. if (bytes_read == 24) {
  446. /* we have enough to decode dev num */
  447. __u64 mjr; /* major */
  448. __u64 mnr; /* minor */
  449. mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
  450. mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
  451. fattr->cf_rdev = MKDEV(mjr, mnr);
  452. }
  453. } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
  454. cifs_dbg(FYI, "Char device\n");
  455. fattr->cf_mode |= S_IFCHR;
  456. fattr->cf_dtype = DT_CHR;
  457. if (bytes_read == 24) {
  458. /* we have enough to decode dev num */
  459. __u64 mjr; /* major */
  460. __u64 mnr; /* minor */
  461. mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
  462. mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
  463. fattr->cf_rdev = MKDEV(mjr, mnr);
  464. }
  465. } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
  466. cifs_dbg(FYI, "Symlink\n");
  467. fattr->cf_mode |= S_IFLNK;
  468. fattr->cf_dtype = DT_LNK;
  469. } else {
  470. fattr->cf_mode |= S_IFREG; /* file? */
  471. fattr->cf_dtype = DT_REG;
  472. rc = -EOPNOTSUPP;
  473. }
  474. } else {
  475. fattr->cf_mode |= S_IFREG; /* then it is a file */
  476. fattr->cf_dtype = DT_REG;
  477. rc = -EOPNOTSUPP; /* or some unknown SFU type */
  478. }
  479. tcon->ses->server->ops->close(xid, tcon, &fid);
  480. cifs_put_tlink(tlink);
  481. return rc;
  482. }
  483. #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
  484. /*
  485. * Fetch mode bits as provided by SFU.
  486. *
  487. * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
  488. */
  489. static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
  490. struct cifs_sb_info *cifs_sb, unsigned int xid)
  491. {
  492. #ifdef CONFIG_CIFS_XATTR
  493. ssize_t rc;
  494. char ea_value[4];
  495. __u32 mode;
  496. struct tcon_link *tlink;
  497. struct cifs_tcon *tcon;
  498. tlink = cifs_sb_tlink(cifs_sb);
  499. if (IS_ERR(tlink))
  500. return PTR_ERR(tlink);
  501. tcon = tlink_tcon(tlink);
  502. if (tcon->ses->server->ops->query_all_EAs == NULL) {
  503. cifs_put_tlink(tlink);
  504. return -EOPNOTSUPP;
  505. }
  506. rc = tcon->ses->server->ops->query_all_EAs(xid, tcon, path,
  507. "SETFILEBITS", ea_value, 4 /* size of buf */,
  508. cifs_sb);
  509. cifs_put_tlink(tlink);
  510. if (rc < 0)
  511. return (int)rc;
  512. else if (rc > 3) {
  513. mode = le32_to_cpu(*((__le32 *)ea_value));
  514. fattr->cf_mode &= ~SFBITS_MASK;
  515. cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
  516. mode, fattr->cf_mode);
  517. fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
  518. cifs_dbg(FYI, "special mode bits 0%o\n", mode);
  519. }
  520. return 0;
  521. #else
  522. return -EOPNOTSUPP;
  523. #endif
  524. }
  525. /* Fill a cifs_fattr struct with info from FILE_ALL_INFO */
  526. static void
  527. cifs_all_info_to_fattr(struct cifs_fattr *fattr, FILE_ALL_INFO *info,
  528. struct super_block *sb, bool adjust_tz,
  529. bool symlink)
  530. {
  531. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  532. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  533. memset(fattr, 0, sizeof(*fattr));
  534. fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
  535. if (info->DeletePending)
  536. fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
  537. if (info->LastAccessTime)
  538. fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
  539. else {
  540. ktime_get_real_ts64(&fattr->cf_atime);
  541. fattr->cf_atime = timespec64_trunc(fattr->cf_atime, sb->s_time_gran);
  542. }
  543. fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
  544. fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
  545. if (adjust_tz) {
  546. fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
  547. fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
  548. }
  549. fattr->cf_eof = le64_to_cpu(info->EndOfFile);
  550. fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
  551. fattr->cf_createtime = le64_to_cpu(info->CreationTime);
  552. fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
  553. if (symlink) {
  554. fattr->cf_mode = S_IFLNK;
  555. fattr->cf_dtype = DT_LNK;
  556. } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
  557. fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
  558. fattr->cf_dtype = DT_DIR;
  559. /*
  560. * Server can return wrong NumberOfLinks value for directories
  561. * when Unix extensions are disabled - fake it.
  562. */
  563. if (!tcon->unix_ext)
  564. fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
  565. } else {
  566. fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
  567. fattr->cf_dtype = DT_REG;
  568. /* clear write bits if ATTR_READONLY is set */
  569. if (fattr->cf_cifsattrs & ATTR_READONLY)
  570. fattr->cf_mode &= ~(S_IWUGO);
  571. /*
  572. * Don't accept zero nlink from non-unix servers unless
  573. * delete is pending. Instead mark it as unknown.
  574. */
  575. if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
  576. !info->DeletePending) {
  577. cifs_dbg(1, "bogus file nlink value %u\n",
  578. fattr->cf_nlink);
  579. fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
  580. }
  581. }
  582. fattr->cf_uid = cifs_sb->mnt_uid;
  583. fattr->cf_gid = cifs_sb->mnt_gid;
  584. }
  585. static int
  586. cifs_get_file_info(struct file *filp)
  587. {
  588. int rc;
  589. unsigned int xid;
  590. FILE_ALL_INFO find_data;
  591. struct cifs_fattr fattr;
  592. struct inode *inode = file_inode(filp);
  593. struct cifsFileInfo *cfile = filp->private_data;
  594. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  595. struct TCP_Server_Info *server = tcon->ses->server;
  596. if (!server->ops->query_file_info)
  597. return -ENOSYS;
  598. xid = get_xid();
  599. rc = server->ops->query_file_info(xid, tcon, &cfile->fid, &find_data);
  600. switch (rc) {
  601. case 0:
  602. cifs_all_info_to_fattr(&fattr, &find_data, inode->i_sb, false,
  603. false);
  604. break;
  605. case -EREMOTE:
  606. cifs_create_dfs_fattr(&fattr, inode->i_sb);
  607. rc = 0;
  608. break;
  609. case -EOPNOTSUPP:
  610. case -EINVAL:
  611. /*
  612. * FIXME: legacy server -- fall back to path-based call?
  613. * for now, just skip revalidating and mark inode for
  614. * immediate reval.
  615. */
  616. rc = 0;
  617. CIFS_I(inode)->time = 0;
  618. default:
  619. goto cgfi_exit;
  620. }
  621. /*
  622. * don't bother with SFU junk here -- just mark inode as needing
  623. * revalidation.
  624. */
  625. fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
  626. fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
  627. cifs_fattr_to_inode(inode, &fattr);
  628. cgfi_exit:
  629. free_xid(xid);
  630. return rc;
  631. }
  632. /* Simple function to return a 64 bit hash of string. Rarely called */
  633. static __u64 simple_hashstr(const char *str)
  634. {
  635. const __u64 hash_mult = 1125899906842597ULL; /* a big enough prime */
  636. __u64 hash = 0;
  637. while (*str)
  638. hash = (hash + (__u64) *str++) * hash_mult;
  639. return hash;
  640. }
  641. int
  642. cifs_get_inode_info(struct inode **inode, const char *full_path,
  643. FILE_ALL_INFO *data, struct super_block *sb, int xid,
  644. const struct cifs_fid *fid)
  645. {
  646. bool validinum = false;
  647. __u16 srchflgs;
  648. int rc = 0, tmprc = ENOSYS;
  649. struct cifs_tcon *tcon;
  650. struct TCP_Server_Info *server;
  651. struct tcon_link *tlink;
  652. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  653. char *buf = NULL;
  654. bool adjust_tz = false;
  655. struct cifs_fattr fattr;
  656. struct cifs_search_info *srchinf = NULL;
  657. bool symlink = false;
  658. tlink = cifs_sb_tlink(cifs_sb);
  659. if (IS_ERR(tlink))
  660. return PTR_ERR(tlink);
  661. tcon = tlink_tcon(tlink);
  662. server = tcon->ses->server;
  663. cifs_dbg(FYI, "Getting info on %s\n", full_path);
  664. if ((data == NULL) && (*inode != NULL)) {
  665. if (CIFS_CACHE_READ(CIFS_I(*inode)) &&
  666. CIFS_I(*inode)->time != 0) {
  667. cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
  668. goto cgii_exit;
  669. }
  670. }
  671. /* if inode info is not passed, get it from server */
  672. if (data == NULL) {
  673. if (!server->ops->query_path_info) {
  674. rc = -ENOSYS;
  675. goto cgii_exit;
  676. }
  677. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  678. if (buf == NULL) {
  679. rc = -ENOMEM;
  680. goto cgii_exit;
  681. }
  682. data = (FILE_ALL_INFO *)buf;
  683. rc = server->ops->query_path_info(xid, tcon, cifs_sb, full_path,
  684. data, &adjust_tz, &symlink);
  685. }
  686. if (!rc) {
  687. cifs_all_info_to_fattr(&fattr, data, sb, adjust_tz,
  688. symlink);
  689. } else if (rc == -EREMOTE) {
  690. cifs_create_dfs_fattr(&fattr, sb);
  691. rc = 0;
  692. } else if ((rc == -EACCES) && backup_cred(cifs_sb) &&
  693. (strcmp(server->vals->version_string, SMB1_VERSION_STRING)
  694. == 0)) {
  695. /*
  696. * For SMB2 and later the backup intent flag is already
  697. * sent if needed on open and there is no path based
  698. * FindFirst operation to use to retry with
  699. */
  700. srchinf = kzalloc(sizeof(struct cifs_search_info),
  701. GFP_KERNEL);
  702. if (srchinf == NULL) {
  703. rc = -ENOMEM;
  704. goto cgii_exit;
  705. }
  706. srchinf->endOfSearch = false;
  707. if (tcon->unix_ext)
  708. srchinf->info_level = SMB_FIND_FILE_UNIX;
  709. else if ((tcon->ses->capabilities &
  710. tcon->ses->server->vals->cap_nt_find) == 0)
  711. srchinf->info_level = SMB_FIND_FILE_INFO_STANDARD;
  712. else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
  713. srchinf->info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
  714. else /* no srvino useful for fallback to some netapp */
  715. srchinf->info_level = SMB_FIND_FILE_DIRECTORY_INFO;
  716. srchflgs = CIFS_SEARCH_CLOSE_ALWAYS |
  717. CIFS_SEARCH_CLOSE_AT_END |
  718. CIFS_SEARCH_BACKUP_SEARCH;
  719. rc = CIFSFindFirst(xid, tcon, full_path,
  720. cifs_sb, NULL, srchflgs, srchinf, false);
  721. if (!rc) {
  722. data = (FILE_ALL_INFO *)srchinf->srch_entries_start;
  723. cifs_dir_info_to_fattr(&fattr,
  724. (FILE_DIRECTORY_INFO *)data, cifs_sb);
  725. fattr.cf_uniqueid = le64_to_cpu(
  726. ((SEARCH_ID_FULL_DIR_INFO *)data)->UniqueId);
  727. validinum = true;
  728. cifs_buf_release(srchinf->ntwrk_buf_start);
  729. }
  730. kfree(srchinf);
  731. if (rc)
  732. goto cgii_exit;
  733. } else
  734. goto cgii_exit;
  735. /*
  736. * If an inode wasn't passed in, then get the inode number
  737. *
  738. * Is an i_ino of zero legal? Can we use that to check if the server
  739. * supports returning inode numbers? Are there other sanity checks we
  740. * can use to ensure that the server is really filling in that field?
  741. */
  742. if (*inode == NULL) {
  743. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
  744. if (validinum == false) {
  745. if (server->ops->get_srv_inum)
  746. tmprc = server->ops->get_srv_inum(xid,
  747. tcon, cifs_sb, full_path,
  748. &fattr.cf_uniqueid, data);
  749. if (tmprc) {
  750. cifs_dbg(FYI, "GetSrvInodeNum rc %d\n",
  751. tmprc);
  752. fattr.cf_uniqueid = iunique(sb, ROOT_I);
  753. cifs_autodisable_serverino(cifs_sb);
  754. } else if ((fattr.cf_uniqueid == 0) &&
  755. strlen(full_path) == 0) {
  756. /* some servers ret bad root ino ie 0 */
  757. cifs_dbg(FYI, "Invalid (0) inodenum\n");
  758. fattr.cf_flags |=
  759. CIFS_FATTR_FAKE_ROOT_INO;
  760. fattr.cf_uniqueid =
  761. simple_hashstr(tcon->treeName);
  762. }
  763. }
  764. } else
  765. fattr.cf_uniqueid = iunique(sb, ROOT_I);
  766. } else {
  767. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
  768. validinum == false && server->ops->get_srv_inum) {
  769. /*
  770. * Pass a NULL tcon to ensure we don't make a round
  771. * trip to the server. This only works for SMB2+.
  772. */
  773. tmprc = server->ops->get_srv_inum(xid,
  774. NULL, cifs_sb, full_path,
  775. &fattr.cf_uniqueid, data);
  776. if (tmprc)
  777. fattr.cf_uniqueid = CIFS_I(*inode)->uniqueid;
  778. else if ((fattr.cf_uniqueid == 0) &&
  779. strlen(full_path) == 0) {
  780. /*
  781. * Reuse existing root inode num since
  782. * inum zero for root causes ls of . and .. to
  783. * not be returned
  784. */
  785. cifs_dbg(FYI, "Srv ret 0 inode num for root\n");
  786. fattr.cf_uniqueid = CIFS_I(*inode)->uniqueid;
  787. }
  788. } else
  789. fattr.cf_uniqueid = CIFS_I(*inode)->uniqueid;
  790. }
  791. /* query for SFU type info if supported and needed */
  792. if (fattr.cf_cifsattrs & ATTR_SYSTEM &&
  793. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
  794. tmprc = cifs_sfu_type(&fattr, full_path, cifs_sb, xid);
  795. if (tmprc)
  796. cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
  797. }
  798. #ifdef CONFIG_CIFS_ACL
  799. /* fill in 0777 bits from ACL */
  800. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
  801. rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, full_path, fid);
  802. if (rc) {
  803. cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
  804. __func__, rc);
  805. goto cgii_exit;
  806. }
  807. }
  808. #endif /* CONFIG_CIFS_ACL */
  809. /* fill in remaining high mode bits e.g. SUID, VTX */
  810. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
  811. cifs_sfu_mode(&fattr, full_path, cifs_sb, xid);
  812. /* check for Minshall+French symlinks */
  813. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
  814. tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
  815. full_path);
  816. if (tmprc)
  817. cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
  818. }
  819. if (!*inode) {
  820. *inode = cifs_iget(sb, &fattr);
  821. if (!*inode)
  822. rc = -ENOMEM;
  823. } else {
  824. /* we already have inode, update it */
  825. /* if uniqueid is different, return error */
  826. if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
  827. CIFS_I(*inode)->uniqueid != fattr.cf_uniqueid)) {
  828. CIFS_I(*inode)->time = 0; /* force reval */
  829. rc = -ESTALE;
  830. goto cgii_exit;
  831. }
  832. /* if filetype is different, return error */
  833. if (unlikely(((*inode)->i_mode & S_IFMT) !=
  834. (fattr.cf_mode & S_IFMT))) {
  835. CIFS_I(*inode)->time = 0; /* force reval */
  836. rc = -ESTALE;
  837. goto cgii_exit;
  838. }
  839. cifs_fattr_to_inode(*inode, &fattr);
  840. }
  841. cgii_exit:
  842. if ((*inode) && ((*inode)->i_ino == 0))
  843. cifs_dbg(FYI, "inode number of zero returned\n");
  844. kfree(buf);
  845. cifs_put_tlink(tlink);
  846. return rc;
  847. }
  848. static const struct inode_operations cifs_ipc_inode_ops = {
  849. .lookup = cifs_lookup,
  850. };
  851. static int
  852. cifs_find_inode(struct inode *inode, void *opaque)
  853. {
  854. struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
  855. /* don't match inode with different uniqueid */
  856. if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
  857. return 0;
  858. /* use createtime like an i_generation field */
  859. if (CIFS_I(inode)->createtime != fattr->cf_createtime)
  860. return 0;
  861. /* don't match inode of different type */
  862. if ((inode->i_mode & S_IFMT) != (fattr->cf_mode & S_IFMT))
  863. return 0;
  864. /* if it's not a directory or has no dentries, then flag it */
  865. if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
  866. fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
  867. return 1;
  868. }
  869. static int
  870. cifs_init_inode(struct inode *inode, void *opaque)
  871. {
  872. struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
  873. CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
  874. CIFS_I(inode)->createtime = fattr->cf_createtime;
  875. return 0;
  876. }
  877. /*
  878. * walk dentry list for an inode and report whether it has aliases that
  879. * are hashed. We use this to determine if a directory inode can actually
  880. * be used.
  881. */
  882. static bool
  883. inode_has_hashed_dentries(struct inode *inode)
  884. {
  885. struct dentry *dentry;
  886. spin_lock(&inode->i_lock);
  887. hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
  888. if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
  889. spin_unlock(&inode->i_lock);
  890. return true;
  891. }
  892. }
  893. spin_unlock(&inode->i_lock);
  894. return false;
  895. }
  896. /* Given fattrs, get a corresponding inode */
  897. struct inode *
  898. cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
  899. {
  900. unsigned long hash;
  901. struct inode *inode;
  902. retry_iget5_locked:
  903. cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
  904. /* hash down to 32-bits on 32-bit arch */
  905. hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
  906. inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
  907. if (inode) {
  908. /* was there a potentially problematic inode collision? */
  909. if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
  910. fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
  911. if (inode_has_hashed_dentries(inode)) {
  912. cifs_autodisable_serverino(CIFS_SB(sb));
  913. iput(inode);
  914. fattr->cf_uniqueid = iunique(sb, ROOT_I);
  915. goto retry_iget5_locked;
  916. }
  917. }
  918. cifs_fattr_to_inode(inode, fattr);
  919. if (sb->s_flags & SB_NOATIME)
  920. inode->i_flags |= S_NOATIME | S_NOCMTIME;
  921. if (inode->i_state & I_NEW) {
  922. inode->i_ino = hash;
  923. #ifdef CONFIG_CIFS_FSCACHE
  924. /* initialize per-inode cache cookie pointer */
  925. CIFS_I(inode)->fscache = NULL;
  926. #endif
  927. unlock_new_inode(inode);
  928. }
  929. }
  930. return inode;
  931. }
  932. /* gets root inode */
  933. struct inode *cifs_root_iget(struct super_block *sb)
  934. {
  935. unsigned int xid;
  936. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  937. struct inode *inode = NULL;
  938. long rc;
  939. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  940. char *path = NULL;
  941. int len;
  942. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
  943. && cifs_sb->prepath) {
  944. len = strlen(cifs_sb->prepath);
  945. path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL);
  946. if (path == NULL)
  947. return ERR_PTR(-ENOMEM);
  948. path[0] = '/';
  949. memcpy(path+1, cifs_sb->prepath, len);
  950. } else {
  951. path = kstrdup("", GFP_KERNEL);
  952. if (path == NULL)
  953. return ERR_PTR(-ENOMEM);
  954. }
  955. xid = get_xid();
  956. if (tcon->unix_ext) {
  957. rc = cifs_get_inode_info_unix(&inode, path, sb, xid);
  958. /* some servers mistakenly claim POSIX support */
  959. if (rc != -EOPNOTSUPP)
  960. goto iget_no_retry;
  961. cifs_dbg(VFS, "server does not support POSIX extensions");
  962. tcon->unix_ext = false;
  963. }
  964. convert_delimiter(path, CIFS_DIR_SEP(cifs_sb));
  965. rc = cifs_get_inode_info(&inode, path, NULL, sb, xid, NULL);
  966. iget_no_retry:
  967. if (!inode) {
  968. inode = ERR_PTR(rc);
  969. goto out;
  970. }
  971. #ifdef CONFIG_CIFS_FSCACHE
  972. /* populate tcon->resource_id */
  973. tcon->resource_id = CIFS_I(inode)->uniqueid;
  974. #endif
  975. if (rc && tcon->pipe) {
  976. cifs_dbg(FYI, "ipc connection - fake read inode\n");
  977. spin_lock(&inode->i_lock);
  978. inode->i_mode |= S_IFDIR;
  979. set_nlink(inode, 2);
  980. inode->i_op = &cifs_ipc_inode_ops;
  981. inode->i_fop = &simple_dir_operations;
  982. inode->i_uid = cifs_sb->mnt_uid;
  983. inode->i_gid = cifs_sb->mnt_gid;
  984. spin_unlock(&inode->i_lock);
  985. } else if (rc) {
  986. iget_failed(inode);
  987. inode = ERR_PTR(rc);
  988. }
  989. out:
  990. kfree(path);
  991. free_xid(xid);
  992. return inode;
  993. }
  994. int
  995. cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
  996. char *full_path, __u32 dosattr)
  997. {
  998. bool set_time = false;
  999. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1000. struct TCP_Server_Info *server;
  1001. FILE_BASIC_INFO info_buf;
  1002. if (attrs == NULL)
  1003. return -EINVAL;
  1004. server = cifs_sb_master_tcon(cifs_sb)->ses->server;
  1005. if (!server->ops->set_file_info)
  1006. return -ENOSYS;
  1007. info_buf.Pad = 0;
  1008. if (attrs->ia_valid & ATTR_ATIME) {
  1009. set_time = true;
  1010. info_buf.LastAccessTime =
  1011. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
  1012. } else
  1013. info_buf.LastAccessTime = 0;
  1014. if (attrs->ia_valid & ATTR_MTIME) {
  1015. set_time = true;
  1016. info_buf.LastWriteTime =
  1017. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
  1018. } else
  1019. info_buf.LastWriteTime = 0;
  1020. /*
  1021. * Samba throws this field away, but windows may actually use it.
  1022. * Do not set ctime unless other time stamps are changed explicitly
  1023. * (i.e. by utimes()) since we would then have a mix of client and
  1024. * server times.
  1025. */
  1026. if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
  1027. cifs_dbg(FYI, "CIFS - CTIME changed\n");
  1028. info_buf.ChangeTime =
  1029. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
  1030. } else
  1031. info_buf.ChangeTime = 0;
  1032. info_buf.CreationTime = 0; /* don't change */
  1033. info_buf.Attributes = cpu_to_le32(dosattr);
  1034. return server->ops->set_file_info(inode, full_path, &info_buf, xid);
  1035. }
  1036. /*
  1037. * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
  1038. * and rename it to a random name that hopefully won't conflict with
  1039. * anything else.
  1040. */
  1041. int
  1042. cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
  1043. const unsigned int xid)
  1044. {
  1045. int oplock = 0;
  1046. int rc;
  1047. struct cifs_fid fid;
  1048. struct cifs_open_parms oparms;
  1049. struct inode *inode = d_inode(dentry);
  1050. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  1051. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1052. struct tcon_link *tlink;
  1053. struct cifs_tcon *tcon;
  1054. __u32 dosattr, origattr;
  1055. FILE_BASIC_INFO *info_buf = NULL;
  1056. tlink = cifs_sb_tlink(cifs_sb);
  1057. if (IS_ERR(tlink))
  1058. return PTR_ERR(tlink);
  1059. tcon = tlink_tcon(tlink);
  1060. /*
  1061. * We cannot rename the file if the server doesn't support
  1062. * CAP_INFOLEVEL_PASSTHRU
  1063. */
  1064. if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
  1065. rc = -EBUSY;
  1066. goto out;
  1067. }
  1068. oparms.tcon = tcon;
  1069. oparms.cifs_sb = cifs_sb;
  1070. oparms.desired_access = DELETE | FILE_WRITE_ATTRIBUTES;
  1071. oparms.create_options = CREATE_NOT_DIR;
  1072. oparms.disposition = FILE_OPEN;
  1073. oparms.path = full_path;
  1074. oparms.fid = &fid;
  1075. oparms.reconnect = false;
  1076. rc = CIFS_open(xid, &oparms, &oplock, NULL);
  1077. if (rc != 0)
  1078. goto out;
  1079. origattr = cifsInode->cifsAttrs;
  1080. if (origattr == 0)
  1081. origattr |= ATTR_NORMAL;
  1082. dosattr = origattr & ~ATTR_READONLY;
  1083. if (dosattr == 0)
  1084. dosattr |= ATTR_NORMAL;
  1085. dosattr |= ATTR_HIDDEN;
  1086. /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
  1087. if (dosattr != origattr) {
  1088. info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
  1089. if (info_buf == NULL) {
  1090. rc = -ENOMEM;
  1091. goto out_close;
  1092. }
  1093. info_buf->Attributes = cpu_to_le32(dosattr);
  1094. rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
  1095. current->tgid);
  1096. /* although we would like to mark the file hidden
  1097. if that fails we will still try to rename it */
  1098. if (!rc)
  1099. cifsInode->cifsAttrs = dosattr;
  1100. else
  1101. dosattr = origattr; /* since not able to change them */
  1102. }
  1103. /* rename the file */
  1104. rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
  1105. cifs_sb->local_nls,
  1106. cifs_remap(cifs_sb));
  1107. if (rc != 0) {
  1108. rc = -EBUSY;
  1109. goto undo_setattr;
  1110. }
  1111. /* try to set DELETE_ON_CLOSE */
  1112. if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
  1113. rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
  1114. current->tgid);
  1115. /*
  1116. * some samba versions return -ENOENT when we try to set the
  1117. * file disposition here. Likely a samba bug, but work around
  1118. * it for now. This means that some cifsXXX files may hang
  1119. * around after they shouldn't.
  1120. *
  1121. * BB: remove this hack after more servers have the fix
  1122. */
  1123. if (rc == -ENOENT)
  1124. rc = 0;
  1125. else if (rc != 0) {
  1126. rc = -EBUSY;
  1127. goto undo_rename;
  1128. }
  1129. set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
  1130. }
  1131. out_close:
  1132. CIFSSMBClose(xid, tcon, fid.netfid);
  1133. out:
  1134. kfree(info_buf);
  1135. cifs_put_tlink(tlink);
  1136. return rc;
  1137. /*
  1138. * reset everything back to the original state. Don't bother
  1139. * dealing with errors here since we can't do anything about
  1140. * them anyway.
  1141. */
  1142. undo_rename:
  1143. CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
  1144. cifs_sb->local_nls, cifs_remap(cifs_sb));
  1145. undo_setattr:
  1146. if (dosattr != origattr) {
  1147. info_buf->Attributes = cpu_to_le32(origattr);
  1148. if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
  1149. current->tgid))
  1150. cifsInode->cifsAttrs = origattr;
  1151. }
  1152. goto out_close;
  1153. }
  1154. /* copied from fs/nfs/dir.c with small changes */
  1155. static void
  1156. cifs_drop_nlink(struct inode *inode)
  1157. {
  1158. spin_lock(&inode->i_lock);
  1159. if (inode->i_nlink > 0)
  1160. drop_nlink(inode);
  1161. spin_unlock(&inode->i_lock);
  1162. }
  1163. /*
  1164. * If d_inode(dentry) is null (usually meaning the cached dentry
  1165. * is a negative dentry) then we would attempt a standard SMB delete, but
  1166. * if that fails we can not attempt the fall back mechanisms on EACCESS
  1167. * but will return the EACCESS to the caller. Note that the VFS does not call
  1168. * unlink on negative dentries currently.
  1169. */
  1170. int cifs_unlink(struct inode *dir, struct dentry *dentry)
  1171. {
  1172. int rc = 0;
  1173. unsigned int xid;
  1174. char *full_path = NULL;
  1175. struct inode *inode = d_inode(dentry);
  1176. struct cifsInodeInfo *cifs_inode;
  1177. struct super_block *sb = dir->i_sb;
  1178. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  1179. struct tcon_link *tlink;
  1180. struct cifs_tcon *tcon;
  1181. struct TCP_Server_Info *server;
  1182. struct iattr *attrs = NULL;
  1183. __u32 dosattr = 0, origattr = 0;
  1184. cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
  1185. tlink = cifs_sb_tlink(cifs_sb);
  1186. if (IS_ERR(tlink))
  1187. return PTR_ERR(tlink);
  1188. tcon = tlink_tcon(tlink);
  1189. server = tcon->ses->server;
  1190. xid = get_xid();
  1191. /* Unlink can be called from rename so we can not take the
  1192. * sb->s_vfs_rename_mutex here */
  1193. full_path = build_path_from_dentry(dentry);
  1194. if (full_path == NULL) {
  1195. rc = -ENOMEM;
  1196. goto unlink_out;
  1197. }
  1198. if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  1199. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  1200. rc = CIFSPOSIXDelFile(xid, tcon, full_path,
  1201. SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
  1202. cifs_remap(cifs_sb));
  1203. cifs_dbg(FYI, "posix del rc %d\n", rc);
  1204. if ((rc == 0) || (rc == -ENOENT))
  1205. goto psx_del_no_retry;
  1206. }
  1207. retry_std_delete:
  1208. if (!server->ops->unlink) {
  1209. rc = -ENOSYS;
  1210. goto psx_del_no_retry;
  1211. }
  1212. rc = server->ops->unlink(xid, tcon, full_path, cifs_sb);
  1213. psx_del_no_retry:
  1214. if (!rc) {
  1215. if (inode)
  1216. cifs_drop_nlink(inode);
  1217. } else if (rc == -ENOENT) {
  1218. d_drop(dentry);
  1219. } else if (rc == -EBUSY) {
  1220. if (server->ops->rename_pending_delete) {
  1221. rc = server->ops->rename_pending_delete(full_path,
  1222. dentry, xid);
  1223. if (rc == 0)
  1224. cifs_drop_nlink(inode);
  1225. }
  1226. } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
  1227. attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
  1228. if (attrs == NULL) {
  1229. rc = -ENOMEM;
  1230. goto out_reval;
  1231. }
  1232. /* try to reset dos attributes */
  1233. cifs_inode = CIFS_I(inode);
  1234. origattr = cifs_inode->cifsAttrs;
  1235. if (origattr == 0)
  1236. origattr |= ATTR_NORMAL;
  1237. dosattr = origattr & ~ATTR_READONLY;
  1238. if (dosattr == 0)
  1239. dosattr |= ATTR_NORMAL;
  1240. dosattr |= ATTR_HIDDEN;
  1241. rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
  1242. if (rc != 0)
  1243. goto out_reval;
  1244. goto retry_std_delete;
  1245. }
  1246. /* undo the setattr if we errored out and it's needed */
  1247. if (rc != 0 && dosattr != 0)
  1248. cifs_set_file_info(inode, attrs, xid, full_path, origattr);
  1249. out_reval:
  1250. if (inode) {
  1251. cifs_inode = CIFS_I(inode);
  1252. cifs_inode->time = 0; /* will force revalidate to get info
  1253. when needed */
  1254. inode->i_ctime = current_time(inode);
  1255. }
  1256. dir->i_ctime = dir->i_mtime = current_time(dir);
  1257. cifs_inode = CIFS_I(dir);
  1258. CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
  1259. unlink_out:
  1260. kfree(full_path);
  1261. kfree(attrs);
  1262. free_xid(xid);
  1263. cifs_put_tlink(tlink);
  1264. return rc;
  1265. }
  1266. static int
  1267. cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
  1268. const char *full_path, struct cifs_sb_info *cifs_sb,
  1269. struct cifs_tcon *tcon, const unsigned int xid)
  1270. {
  1271. int rc = 0;
  1272. struct inode *inode = NULL;
  1273. if (tcon->unix_ext)
  1274. rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
  1275. xid);
  1276. else
  1277. rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
  1278. xid, NULL);
  1279. if (rc)
  1280. return rc;
  1281. /*
  1282. * setting nlink not necessary except in cases where we failed to get it
  1283. * from the server or was set bogus. Also, since this is a brand new
  1284. * inode, no need to grab the i_lock before setting the i_nlink.
  1285. */
  1286. if (inode->i_nlink < 2)
  1287. set_nlink(inode, 2);
  1288. mode &= ~current_umask();
  1289. /* must turn on setgid bit if parent dir has it */
  1290. if (parent->i_mode & S_ISGID)
  1291. mode |= S_ISGID;
  1292. if (tcon->unix_ext) {
  1293. struct cifs_unix_set_info_args args = {
  1294. .mode = mode,
  1295. .ctime = NO_CHANGE_64,
  1296. .atime = NO_CHANGE_64,
  1297. .mtime = NO_CHANGE_64,
  1298. .device = 0,
  1299. };
  1300. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  1301. args.uid = current_fsuid();
  1302. if (parent->i_mode & S_ISGID)
  1303. args.gid = parent->i_gid;
  1304. else
  1305. args.gid = current_fsgid();
  1306. } else {
  1307. args.uid = INVALID_UID; /* no change */
  1308. args.gid = INVALID_GID; /* no change */
  1309. }
  1310. CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
  1311. cifs_sb->local_nls,
  1312. cifs_remap(cifs_sb));
  1313. } else {
  1314. struct TCP_Server_Info *server = tcon->ses->server;
  1315. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
  1316. (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
  1317. server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
  1318. tcon, xid);
  1319. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
  1320. inode->i_mode = (mode | S_IFDIR);
  1321. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  1322. inode->i_uid = current_fsuid();
  1323. if (inode->i_mode & S_ISGID)
  1324. inode->i_gid = parent->i_gid;
  1325. else
  1326. inode->i_gid = current_fsgid();
  1327. }
  1328. }
  1329. d_instantiate(dentry, inode);
  1330. return rc;
  1331. }
  1332. static int
  1333. cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
  1334. const char *full_path, struct cifs_sb_info *cifs_sb,
  1335. struct cifs_tcon *tcon, const unsigned int xid)
  1336. {
  1337. int rc = 0;
  1338. u32 oplock = 0;
  1339. FILE_UNIX_BASIC_INFO *info = NULL;
  1340. struct inode *newinode = NULL;
  1341. struct cifs_fattr fattr;
  1342. info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
  1343. if (info == NULL) {
  1344. rc = -ENOMEM;
  1345. goto posix_mkdir_out;
  1346. }
  1347. mode &= ~current_umask();
  1348. rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
  1349. NULL /* netfid */, info, &oplock, full_path,
  1350. cifs_sb->local_nls, cifs_remap(cifs_sb));
  1351. if (rc == -EOPNOTSUPP)
  1352. goto posix_mkdir_out;
  1353. else if (rc) {
  1354. cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
  1355. d_drop(dentry);
  1356. goto posix_mkdir_out;
  1357. }
  1358. if (info->Type == cpu_to_le32(-1))
  1359. /* no return info, go query for it */
  1360. goto posix_mkdir_get_info;
  1361. /*
  1362. * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
  1363. * need to set uid/gid.
  1364. */
  1365. cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
  1366. cifs_fill_uniqueid(inode->i_sb, &fattr);
  1367. newinode = cifs_iget(inode->i_sb, &fattr);
  1368. if (!newinode)
  1369. goto posix_mkdir_get_info;
  1370. d_instantiate(dentry, newinode);
  1371. #ifdef CONFIG_CIFS_DEBUG2
  1372. cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n",
  1373. dentry, dentry, newinode);
  1374. if (newinode->i_nlink != 2)
  1375. cifs_dbg(FYI, "unexpected number of links %d\n",
  1376. newinode->i_nlink);
  1377. #endif
  1378. posix_mkdir_out:
  1379. kfree(info);
  1380. return rc;
  1381. posix_mkdir_get_info:
  1382. rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
  1383. xid);
  1384. goto posix_mkdir_out;
  1385. }
  1386. int cifs_mkdir(struct inode *inode, struct dentry *direntry, umode_t mode)
  1387. {
  1388. int rc = 0;
  1389. unsigned int xid;
  1390. struct cifs_sb_info *cifs_sb;
  1391. struct tcon_link *tlink;
  1392. struct cifs_tcon *tcon;
  1393. struct TCP_Server_Info *server;
  1394. char *full_path;
  1395. cifs_dbg(FYI, "In cifs_mkdir, mode = %04ho inode = 0x%p\n",
  1396. mode, inode);
  1397. cifs_sb = CIFS_SB(inode->i_sb);
  1398. tlink = cifs_sb_tlink(cifs_sb);
  1399. if (IS_ERR(tlink))
  1400. return PTR_ERR(tlink);
  1401. tcon = tlink_tcon(tlink);
  1402. xid = get_xid();
  1403. full_path = build_path_from_dentry(direntry);
  1404. if (full_path == NULL) {
  1405. rc = -ENOMEM;
  1406. goto mkdir_out;
  1407. }
  1408. server = tcon->ses->server;
  1409. if ((server->ops->posix_mkdir) && (tcon->posix_extensions)) {
  1410. rc = server->ops->posix_mkdir(xid, inode, mode, tcon, full_path,
  1411. cifs_sb);
  1412. d_drop(direntry); /* for time being always refresh inode info */
  1413. goto mkdir_out;
  1414. }
  1415. if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  1416. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  1417. rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
  1418. tcon, xid);
  1419. if (rc != -EOPNOTSUPP)
  1420. goto mkdir_out;
  1421. }
  1422. if (!server->ops->mkdir) {
  1423. rc = -ENOSYS;
  1424. goto mkdir_out;
  1425. }
  1426. /* BB add setting the equivalent of mode via CreateX w/ACLs */
  1427. rc = server->ops->mkdir(xid, tcon, full_path, cifs_sb);
  1428. if (rc) {
  1429. cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
  1430. d_drop(direntry);
  1431. goto mkdir_out;
  1432. }
  1433. rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
  1434. xid);
  1435. mkdir_out:
  1436. /*
  1437. * Force revalidate to get parent dir info when needed since cached
  1438. * attributes are invalid now.
  1439. */
  1440. CIFS_I(inode)->time = 0;
  1441. kfree(full_path);
  1442. free_xid(xid);
  1443. cifs_put_tlink(tlink);
  1444. return rc;
  1445. }
  1446. int cifs_rmdir(struct inode *inode, struct dentry *direntry)
  1447. {
  1448. int rc = 0;
  1449. unsigned int xid;
  1450. struct cifs_sb_info *cifs_sb;
  1451. struct tcon_link *tlink;
  1452. struct cifs_tcon *tcon;
  1453. struct TCP_Server_Info *server;
  1454. char *full_path = NULL;
  1455. struct cifsInodeInfo *cifsInode;
  1456. cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
  1457. xid = get_xid();
  1458. full_path = build_path_from_dentry(direntry);
  1459. if (full_path == NULL) {
  1460. rc = -ENOMEM;
  1461. goto rmdir_exit;
  1462. }
  1463. cifs_sb = CIFS_SB(inode->i_sb);
  1464. tlink = cifs_sb_tlink(cifs_sb);
  1465. if (IS_ERR(tlink)) {
  1466. rc = PTR_ERR(tlink);
  1467. goto rmdir_exit;
  1468. }
  1469. tcon = tlink_tcon(tlink);
  1470. server = tcon->ses->server;
  1471. if (!server->ops->rmdir) {
  1472. rc = -ENOSYS;
  1473. cifs_put_tlink(tlink);
  1474. goto rmdir_exit;
  1475. }
  1476. rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
  1477. cifs_put_tlink(tlink);
  1478. if (!rc) {
  1479. spin_lock(&d_inode(direntry)->i_lock);
  1480. i_size_write(d_inode(direntry), 0);
  1481. clear_nlink(d_inode(direntry));
  1482. spin_unlock(&d_inode(direntry)->i_lock);
  1483. }
  1484. cifsInode = CIFS_I(d_inode(direntry));
  1485. /* force revalidate to go get info when needed */
  1486. cifsInode->time = 0;
  1487. cifsInode = CIFS_I(inode);
  1488. /*
  1489. * Force revalidate to get parent dir info when needed since cached
  1490. * attributes are invalid now.
  1491. */
  1492. cifsInode->time = 0;
  1493. d_inode(direntry)->i_ctime = inode->i_ctime = inode->i_mtime =
  1494. current_time(inode);
  1495. rmdir_exit:
  1496. kfree(full_path);
  1497. free_xid(xid);
  1498. return rc;
  1499. }
  1500. static int
  1501. cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
  1502. const char *from_path, struct dentry *to_dentry,
  1503. const char *to_path)
  1504. {
  1505. struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
  1506. struct tcon_link *tlink;
  1507. struct cifs_tcon *tcon;
  1508. struct TCP_Server_Info *server;
  1509. struct cifs_fid fid;
  1510. struct cifs_open_parms oparms;
  1511. int oplock, rc;
  1512. tlink = cifs_sb_tlink(cifs_sb);
  1513. if (IS_ERR(tlink))
  1514. return PTR_ERR(tlink);
  1515. tcon = tlink_tcon(tlink);
  1516. server = tcon->ses->server;
  1517. if (!server->ops->rename)
  1518. return -ENOSYS;
  1519. /* try path-based rename first */
  1520. rc = server->ops->rename(xid, tcon, from_path, to_path, cifs_sb);
  1521. /*
  1522. * Don't bother with rename by filehandle unless file is busy and
  1523. * source. Note that cross directory moves do not work with
  1524. * rename by filehandle to various Windows servers.
  1525. */
  1526. if (rc == 0 || rc != -EBUSY)
  1527. goto do_rename_exit;
  1528. /* Don't fall back to using SMB on SMB 2+ mount */
  1529. if (server->vals->protocol_id != 0)
  1530. goto do_rename_exit;
  1531. /* open-file renames don't work across directories */
  1532. if (to_dentry->d_parent != from_dentry->d_parent)
  1533. goto do_rename_exit;
  1534. oparms.tcon = tcon;
  1535. oparms.cifs_sb = cifs_sb;
  1536. /* open the file to be renamed -- we need DELETE perms */
  1537. oparms.desired_access = DELETE;
  1538. oparms.create_options = CREATE_NOT_DIR;
  1539. oparms.disposition = FILE_OPEN;
  1540. oparms.path = from_path;
  1541. oparms.fid = &fid;
  1542. oparms.reconnect = false;
  1543. rc = CIFS_open(xid, &oparms, &oplock, NULL);
  1544. if (rc == 0) {
  1545. rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
  1546. (const char *) to_dentry->d_name.name,
  1547. cifs_sb->local_nls, cifs_remap(cifs_sb));
  1548. CIFSSMBClose(xid, tcon, fid.netfid);
  1549. }
  1550. do_rename_exit:
  1551. cifs_put_tlink(tlink);
  1552. return rc;
  1553. }
  1554. int
  1555. cifs_rename2(struct inode *source_dir, struct dentry *source_dentry,
  1556. struct inode *target_dir, struct dentry *target_dentry,
  1557. unsigned int flags)
  1558. {
  1559. char *from_name = NULL;
  1560. char *to_name = NULL;
  1561. struct cifs_sb_info *cifs_sb;
  1562. struct tcon_link *tlink;
  1563. struct cifs_tcon *tcon;
  1564. FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
  1565. FILE_UNIX_BASIC_INFO *info_buf_target;
  1566. unsigned int xid;
  1567. int rc, tmprc;
  1568. if (flags & ~RENAME_NOREPLACE)
  1569. return -EINVAL;
  1570. cifs_sb = CIFS_SB(source_dir->i_sb);
  1571. tlink = cifs_sb_tlink(cifs_sb);
  1572. if (IS_ERR(tlink))
  1573. return PTR_ERR(tlink);
  1574. tcon = tlink_tcon(tlink);
  1575. xid = get_xid();
  1576. /*
  1577. * we already have the rename sem so we do not need to
  1578. * grab it again here to protect the path integrity
  1579. */
  1580. from_name = build_path_from_dentry(source_dentry);
  1581. if (from_name == NULL) {
  1582. rc = -ENOMEM;
  1583. goto cifs_rename_exit;
  1584. }
  1585. to_name = build_path_from_dentry(target_dentry);
  1586. if (to_name == NULL) {
  1587. rc = -ENOMEM;
  1588. goto cifs_rename_exit;
  1589. }
  1590. rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
  1591. to_name);
  1592. /*
  1593. * No-replace is the natural behavior for CIFS, so skip unlink hacks.
  1594. */
  1595. if (flags & RENAME_NOREPLACE)
  1596. goto cifs_rename_exit;
  1597. if (rc == -EEXIST && tcon->unix_ext) {
  1598. /*
  1599. * Are src and dst hardlinks of same inode? We can only tell
  1600. * with unix extensions enabled.
  1601. */
  1602. info_buf_source =
  1603. kmalloc_array(2, sizeof(FILE_UNIX_BASIC_INFO),
  1604. GFP_KERNEL);
  1605. if (info_buf_source == NULL) {
  1606. rc = -ENOMEM;
  1607. goto cifs_rename_exit;
  1608. }
  1609. info_buf_target = info_buf_source + 1;
  1610. tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
  1611. info_buf_source,
  1612. cifs_sb->local_nls,
  1613. cifs_remap(cifs_sb));
  1614. if (tmprc != 0)
  1615. goto unlink_target;
  1616. tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
  1617. info_buf_target,
  1618. cifs_sb->local_nls,
  1619. cifs_remap(cifs_sb));
  1620. if (tmprc == 0 && (info_buf_source->UniqueId ==
  1621. info_buf_target->UniqueId)) {
  1622. /* same file, POSIX says that this is a noop */
  1623. rc = 0;
  1624. goto cifs_rename_exit;
  1625. }
  1626. }
  1627. /*
  1628. * else ... BB we could add the same check for Windows by
  1629. * checking the UniqueId via FILE_INTERNAL_INFO
  1630. */
  1631. unlink_target:
  1632. /* Try unlinking the target dentry if it's not negative */
  1633. if (d_really_is_positive(target_dentry) && (rc == -EACCES || rc == -EEXIST)) {
  1634. if (d_is_dir(target_dentry))
  1635. tmprc = cifs_rmdir(target_dir, target_dentry);
  1636. else
  1637. tmprc = cifs_unlink(target_dir, target_dentry);
  1638. if (tmprc)
  1639. goto cifs_rename_exit;
  1640. rc = cifs_do_rename(xid, source_dentry, from_name,
  1641. target_dentry, to_name);
  1642. }
  1643. /* force revalidate to go get info when needed */
  1644. CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
  1645. source_dir->i_ctime = source_dir->i_mtime = target_dir->i_ctime =
  1646. target_dir->i_mtime = current_time(source_dir);
  1647. cifs_rename_exit:
  1648. kfree(info_buf_source);
  1649. kfree(from_name);
  1650. kfree(to_name);
  1651. free_xid(xid);
  1652. cifs_put_tlink(tlink);
  1653. return rc;
  1654. }
  1655. static bool
  1656. cifs_inode_needs_reval(struct inode *inode)
  1657. {
  1658. struct cifsInodeInfo *cifs_i = CIFS_I(inode);
  1659. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1660. if (cifs_i->time == 0)
  1661. return true;
  1662. if (CIFS_CACHE_READ(cifs_i))
  1663. return false;
  1664. if (!lookupCacheEnabled)
  1665. return true;
  1666. if (!cifs_sb->actimeo)
  1667. return true;
  1668. if (!time_in_range(jiffies, cifs_i->time,
  1669. cifs_i->time + cifs_sb->actimeo))
  1670. return true;
  1671. /* hardlinked files w/ noserverino get "special" treatment */
  1672. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
  1673. S_ISREG(inode->i_mode) && inode->i_nlink != 1)
  1674. return true;
  1675. return false;
  1676. }
  1677. /*
  1678. * Zap the cache. Called when invalid_mapping flag is set.
  1679. */
  1680. int
  1681. cifs_invalidate_mapping(struct inode *inode)
  1682. {
  1683. int rc = 0;
  1684. if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
  1685. rc = invalidate_inode_pages2(inode->i_mapping);
  1686. if (rc)
  1687. cifs_dbg(VFS, "%s: could not invalidate inode %p\n",
  1688. __func__, inode);
  1689. }
  1690. cifs_fscache_reset_inode_cookie(inode);
  1691. return rc;
  1692. }
  1693. /**
  1694. * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
  1695. * @word: long word containing the bit lock
  1696. */
  1697. static int
  1698. cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
  1699. {
  1700. freezable_schedule_unsafe();
  1701. if (signal_pending_state(mode, current))
  1702. return -ERESTARTSYS;
  1703. return 0;
  1704. }
  1705. int
  1706. cifs_revalidate_mapping(struct inode *inode)
  1707. {
  1708. int rc;
  1709. unsigned long *flags = &CIFS_I(inode)->flags;
  1710. rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
  1711. TASK_KILLABLE);
  1712. if (rc)
  1713. return rc;
  1714. if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
  1715. rc = cifs_invalidate_mapping(inode);
  1716. if (rc)
  1717. set_bit(CIFS_INO_INVALID_MAPPING, flags);
  1718. }
  1719. clear_bit_unlock(CIFS_INO_LOCK, flags);
  1720. smp_mb__after_atomic();
  1721. wake_up_bit(flags, CIFS_INO_LOCK);
  1722. return rc;
  1723. }
  1724. int
  1725. cifs_zap_mapping(struct inode *inode)
  1726. {
  1727. set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
  1728. return cifs_revalidate_mapping(inode);
  1729. }
  1730. int cifs_revalidate_file_attr(struct file *filp)
  1731. {
  1732. int rc = 0;
  1733. struct inode *inode = file_inode(filp);
  1734. struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
  1735. if (!cifs_inode_needs_reval(inode))
  1736. return rc;
  1737. if (tlink_tcon(cfile->tlink)->unix_ext)
  1738. rc = cifs_get_file_info_unix(filp);
  1739. else
  1740. rc = cifs_get_file_info(filp);
  1741. return rc;
  1742. }
  1743. int cifs_revalidate_dentry_attr(struct dentry *dentry)
  1744. {
  1745. unsigned int xid;
  1746. int rc = 0;
  1747. struct inode *inode = d_inode(dentry);
  1748. struct super_block *sb = dentry->d_sb;
  1749. char *full_path = NULL;
  1750. int count = 0;
  1751. if (inode == NULL)
  1752. return -ENOENT;
  1753. if (!cifs_inode_needs_reval(inode))
  1754. return rc;
  1755. xid = get_xid();
  1756. /* can not safely grab the rename sem here if rename calls revalidate
  1757. since that would deadlock */
  1758. full_path = build_path_from_dentry(dentry);
  1759. if (full_path == NULL) {
  1760. rc = -ENOMEM;
  1761. goto out;
  1762. }
  1763. cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
  1764. full_path, inode, inode->i_count.counter,
  1765. dentry, cifs_get_time(dentry), jiffies);
  1766. again:
  1767. if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext)
  1768. rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
  1769. else
  1770. rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
  1771. xid, NULL);
  1772. if (rc == -EAGAIN && count++ < 10)
  1773. goto again;
  1774. out:
  1775. kfree(full_path);
  1776. free_xid(xid);
  1777. return rc;
  1778. }
  1779. int cifs_revalidate_file(struct file *filp)
  1780. {
  1781. int rc;
  1782. struct inode *inode = file_inode(filp);
  1783. rc = cifs_revalidate_file_attr(filp);
  1784. if (rc)
  1785. return rc;
  1786. return cifs_revalidate_mapping(inode);
  1787. }
  1788. /* revalidate a dentry's inode attributes */
  1789. int cifs_revalidate_dentry(struct dentry *dentry)
  1790. {
  1791. int rc;
  1792. struct inode *inode = d_inode(dentry);
  1793. rc = cifs_revalidate_dentry_attr(dentry);
  1794. if (rc)
  1795. return rc;
  1796. return cifs_revalidate_mapping(inode);
  1797. }
  1798. int cifs_getattr(const struct path *path, struct kstat *stat,
  1799. u32 request_mask, unsigned int flags)
  1800. {
  1801. struct dentry *dentry = path->dentry;
  1802. struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
  1803. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  1804. struct inode *inode = d_inode(dentry);
  1805. int rc;
  1806. /*
  1807. * We need to be sure that all dirty pages are written and the server
  1808. * has actual ctime, mtime and file length.
  1809. */
  1810. if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
  1811. inode->i_mapping->nrpages != 0) {
  1812. rc = filemap_fdatawait(inode->i_mapping);
  1813. if (rc) {
  1814. mapping_set_error(inode->i_mapping, rc);
  1815. return rc;
  1816. }
  1817. }
  1818. rc = cifs_revalidate_dentry_attr(dentry);
  1819. if (rc)
  1820. return rc;
  1821. generic_fillattr(inode, stat);
  1822. stat->blksize = CIFS_MAX_MSGSIZE;
  1823. stat->ino = CIFS_I(inode)->uniqueid;
  1824. /* old CIFS Unix Extensions doesn't return create time */
  1825. if (CIFS_I(inode)->createtime) {
  1826. stat->result_mask |= STATX_BTIME;
  1827. stat->btime =
  1828. cifs_NTtimeToUnix(cpu_to_le64(CIFS_I(inode)->createtime));
  1829. }
  1830. stat->attributes_mask |= (STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED);
  1831. if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_COMPRESSED)
  1832. stat->attributes |= STATX_ATTR_COMPRESSED;
  1833. if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_ENCRYPTED)
  1834. stat->attributes |= STATX_ATTR_ENCRYPTED;
  1835. /*
  1836. * If on a multiuser mount without unix extensions or cifsacl being
  1837. * enabled, and the admin hasn't overridden them, set the ownership
  1838. * to the fsuid/fsgid of the current process.
  1839. */
  1840. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
  1841. !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
  1842. !tcon->unix_ext) {
  1843. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
  1844. stat->uid = current_fsuid();
  1845. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
  1846. stat->gid = current_fsgid();
  1847. }
  1848. return rc;
  1849. }
  1850. static int cifs_truncate_page(struct address_space *mapping, loff_t from)
  1851. {
  1852. pgoff_t index = from >> PAGE_SHIFT;
  1853. unsigned offset = from & (PAGE_SIZE - 1);
  1854. struct page *page;
  1855. int rc = 0;
  1856. page = grab_cache_page(mapping, index);
  1857. if (!page)
  1858. return -ENOMEM;
  1859. zero_user_segment(page, offset, PAGE_SIZE);
  1860. unlock_page(page);
  1861. put_page(page);
  1862. return rc;
  1863. }
  1864. static void cifs_setsize(struct inode *inode, loff_t offset)
  1865. {
  1866. struct cifsInodeInfo *cifs_i = CIFS_I(inode);
  1867. spin_lock(&inode->i_lock);
  1868. i_size_write(inode, offset);
  1869. spin_unlock(&inode->i_lock);
  1870. /* Cached inode must be refreshed on truncate */
  1871. cifs_i->time = 0;
  1872. truncate_pagecache(inode, offset);
  1873. }
  1874. static int
  1875. cifs_set_file_size(struct inode *inode, struct iattr *attrs,
  1876. unsigned int xid, char *full_path)
  1877. {
  1878. int rc;
  1879. struct cifsFileInfo *open_file;
  1880. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  1881. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1882. struct tcon_link *tlink = NULL;
  1883. struct cifs_tcon *tcon = NULL;
  1884. struct TCP_Server_Info *server;
  1885. /*
  1886. * To avoid spurious oplock breaks from server, in the case of
  1887. * inodes that we already have open, avoid doing path based
  1888. * setting of file size if we can do it by handle.
  1889. * This keeps our caching token (oplock) and avoids timeouts
  1890. * when the local oplock break takes longer to flush
  1891. * writebehind data than the SMB timeout for the SetPathInfo
  1892. * request would allow
  1893. */
  1894. open_file = find_writable_file(cifsInode, true);
  1895. if (open_file) {
  1896. tcon = tlink_tcon(open_file->tlink);
  1897. server = tcon->ses->server;
  1898. if (server->ops->set_file_size)
  1899. rc = server->ops->set_file_size(xid, tcon, open_file,
  1900. attrs->ia_size, false);
  1901. else
  1902. rc = -ENOSYS;
  1903. cifsFileInfo_put(open_file);
  1904. cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
  1905. } else
  1906. rc = -EINVAL;
  1907. if (!rc)
  1908. goto set_size_out;
  1909. if (tcon == NULL) {
  1910. tlink = cifs_sb_tlink(cifs_sb);
  1911. if (IS_ERR(tlink))
  1912. return PTR_ERR(tlink);
  1913. tcon = tlink_tcon(tlink);
  1914. server = tcon->ses->server;
  1915. }
  1916. /*
  1917. * Set file size by pathname rather than by handle either because no
  1918. * valid, writeable file handle for it was found or because there was
  1919. * an error setting it by handle.
  1920. */
  1921. if (server->ops->set_path_size)
  1922. rc = server->ops->set_path_size(xid, tcon, full_path,
  1923. attrs->ia_size, cifs_sb, false);
  1924. else
  1925. rc = -ENOSYS;
  1926. cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
  1927. if (tlink)
  1928. cifs_put_tlink(tlink);
  1929. set_size_out:
  1930. if (rc == 0) {
  1931. cifsInode->server_eof = attrs->ia_size;
  1932. cifs_setsize(inode, attrs->ia_size);
  1933. cifs_truncate_page(inode->i_mapping, inode->i_size);
  1934. }
  1935. return rc;
  1936. }
  1937. static int
  1938. cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
  1939. {
  1940. int rc;
  1941. unsigned int xid;
  1942. char *full_path = NULL;
  1943. struct inode *inode = d_inode(direntry);
  1944. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  1945. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1946. struct tcon_link *tlink;
  1947. struct cifs_tcon *pTcon;
  1948. struct cifs_unix_set_info_args *args = NULL;
  1949. struct cifsFileInfo *open_file;
  1950. cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
  1951. direntry, attrs->ia_valid);
  1952. xid = get_xid();
  1953. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
  1954. attrs->ia_valid |= ATTR_FORCE;
  1955. rc = setattr_prepare(direntry, attrs);
  1956. if (rc < 0)
  1957. goto out;
  1958. full_path = build_path_from_dentry(direntry);
  1959. if (full_path == NULL) {
  1960. rc = -ENOMEM;
  1961. goto out;
  1962. }
  1963. /*
  1964. * Attempt to flush data before changing attributes. We need to do
  1965. * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
  1966. * ownership or mode then we may also need to do this. Here, we take
  1967. * the safe way out and just do the flush on all setattr requests. If
  1968. * the flush returns error, store it to report later and continue.
  1969. *
  1970. * BB: This should be smarter. Why bother flushing pages that
  1971. * will be truncated anyway? Also, should we error out here if
  1972. * the flush returns error?
  1973. */
  1974. rc = filemap_write_and_wait(inode->i_mapping);
  1975. if (is_interrupt_error(rc)) {
  1976. rc = -ERESTARTSYS;
  1977. goto out;
  1978. }
  1979. mapping_set_error(inode->i_mapping, rc);
  1980. rc = 0;
  1981. if (attrs->ia_valid & ATTR_SIZE) {
  1982. rc = cifs_set_file_size(inode, attrs, xid, full_path);
  1983. if (rc != 0)
  1984. goto out;
  1985. }
  1986. /* skip mode change if it's just for clearing setuid/setgid */
  1987. if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
  1988. attrs->ia_valid &= ~ATTR_MODE;
  1989. args = kmalloc(sizeof(*args), GFP_KERNEL);
  1990. if (args == NULL) {
  1991. rc = -ENOMEM;
  1992. goto out;
  1993. }
  1994. /* set up the struct */
  1995. if (attrs->ia_valid & ATTR_MODE)
  1996. args->mode = attrs->ia_mode;
  1997. else
  1998. args->mode = NO_CHANGE_64;
  1999. if (attrs->ia_valid & ATTR_UID)
  2000. args->uid = attrs->ia_uid;
  2001. else
  2002. args->uid = INVALID_UID; /* no change */
  2003. if (attrs->ia_valid & ATTR_GID)
  2004. args->gid = attrs->ia_gid;
  2005. else
  2006. args->gid = INVALID_GID; /* no change */
  2007. if (attrs->ia_valid & ATTR_ATIME)
  2008. args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
  2009. else
  2010. args->atime = NO_CHANGE_64;
  2011. if (attrs->ia_valid & ATTR_MTIME)
  2012. args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
  2013. else
  2014. args->mtime = NO_CHANGE_64;
  2015. if (attrs->ia_valid & ATTR_CTIME)
  2016. args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
  2017. else
  2018. args->ctime = NO_CHANGE_64;
  2019. args->device = 0;
  2020. open_file = find_writable_file(cifsInode, true);
  2021. if (open_file) {
  2022. u16 nfid = open_file->fid.netfid;
  2023. u32 npid = open_file->pid;
  2024. pTcon = tlink_tcon(open_file->tlink);
  2025. rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
  2026. cifsFileInfo_put(open_file);
  2027. } else {
  2028. tlink = cifs_sb_tlink(cifs_sb);
  2029. if (IS_ERR(tlink)) {
  2030. rc = PTR_ERR(tlink);
  2031. goto out;
  2032. }
  2033. pTcon = tlink_tcon(tlink);
  2034. rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
  2035. cifs_sb->local_nls,
  2036. cifs_remap(cifs_sb));
  2037. cifs_put_tlink(tlink);
  2038. }
  2039. if (rc)
  2040. goto out;
  2041. if ((attrs->ia_valid & ATTR_SIZE) &&
  2042. attrs->ia_size != i_size_read(inode))
  2043. truncate_setsize(inode, attrs->ia_size);
  2044. setattr_copy(inode, attrs);
  2045. mark_inode_dirty(inode);
  2046. /* force revalidate when any of these times are set since some
  2047. of the fs types (eg ext3, fat) do not have fine enough
  2048. time granularity to match protocol, and we do not have a
  2049. a way (yet) to query the server fs's time granularity (and
  2050. whether it rounds times down).
  2051. */
  2052. if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
  2053. cifsInode->time = 0;
  2054. out:
  2055. kfree(args);
  2056. kfree(full_path);
  2057. free_xid(xid);
  2058. return rc;
  2059. }
  2060. static int
  2061. cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
  2062. {
  2063. unsigned int xid;
  2064. kuid_t uid = INVALID_UID;
  2065. kgid_t gid = INVALID_GID;
  2066. struct inode *inode = d_inode(direntry);
  2067. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  2068. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  2069. char *full_path = NULL;
  2070. int rc = -EACCES;
  2071. __u32 dosattr = 0;
  2072. __u64 mode = NO_CHANGE_64;
  2073. xid = get_xid();
  2074. cifs_dbg(FYI, "setattr on file %pd attrs->iavalid 0x%x\n",
  2075. direntry, attrs->ia_valid);
  2076. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
  2077. attrs->ia_valid |= ATTR_FORCE;
  2078. rc = setattr_prepare(direntry, attrs);
  2079. if (rc < 0) {
  2080. free_xid(xid);
  2081. return rc;
  2082. }
  2083. full_path = build_path_from_dentry(direntry);
  2084. if (full_path == NULL) {
  2085. rc = -ENOMEM;
  2086. free_xid(xid);
  2087. return rc;
  2088. }
  2089. /*
  2090. * Attempt to flush data before changing attributes. We need to do
  2091. * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
  2092. * ownership or mode then we may also need to do this. Here, we take
  2093. * the safe way out and just do the flush on all setattr requests. If
  2094. * the flush returns error, store it to report later and continue.
  2095. *
  2096. * BB: This should be smarter. Why bother flushing pages that
  2097. * will be truncated anyway? Also, should we error out here if
  2098. * the flush returns error?
  2099. */
  2100. rc = filemap_write_and_wait(inode->i_mapping);
  2101. if (is_interrupt_error(rc)) {
  2102. rc = -ERESTARTSYS;
  2103. goto cifs_setattr_exit;
  2104. }
  2105. mapping_set_error(inode->i_mapping, rc);
  2106. rc = 0;
  2107. if (attrs->ia_valid & ATTR_SIZE) {
  2108. rc = cifs_set_file_size(inode, attrs, xid, full_path);
  2109. if (rc != 0)
  2110. goto cifs_setattr_exit;
  2111. }
  2112. if (attrs->ia_valid & ATTR_UID)
  2113. uid = attrs->ia_uid;
  2114. if (attrs->ia_valid & ATTR_GID)
  2115. gid = attrs->ia_gid;
  2116. #ifdef CONFIG_CIFS_ACL
  2117. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
  2118. if (uid_valid(uid) || gid_valid(gid)) {
  2119. rc = id_mode_to_cifs_acl(inode, full_path, NO_CHANGE_64,
  2120. uid, gid);
  2121. if (rc) {
  2122. cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
  2123. __func__, rc);
  2124. goto cifs_setattr_exit;
  2125. }
  2126. }
  2127. } else
  2128. #endif /* CONFIG_CIFS_ACL */
  2129. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
  2130. attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
  2131. /* skip mode change if it's just for clearing setuid/setgid */
  2132. if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
  2133. attrs->ia_valid &= ~ATTR_MODE;
  2134. if (attrs->ia_valid & ATTR_MODE) {
  2135. mode = attrs->ia_mode;
  2136. rc = 0;
  2137. #ifdef CONFIG_CIFS_ACL
  2138. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
  2139. rc = id_mode_to_cifs_acl(inode, full_path, mode,
  2140. INVALID_UID, INVALID_GID);
  2141. if (rc) {
  2142. cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
  2143. __func__, rc);
  2144. goto cifs_setattr_exit;
  2145. }
  2146. } else
  2147. #endif /* CONFIG_CIFS_ACL */
  2148. if (((mode & S_IWUGO) == 0) &&
  2149. (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
  2150. dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
  2151. /* fix up mode if we're not using dynperm */
  2152. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
  2153. attrs->ia_mode = inode->i_mode & ~S_IWUGO;
  2154. } else if ((mode & S_IWUGO) &&
  2155. (cifsInode->cifsAttrs & ATTR_READONLY)) {
  2156. dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
  2157. /* Attributes of 0 are ignored */
  2158. if (dosattr == 0)
  2159. dosattr |= ATTR_NORMAL;
  2160. /* reset local inode permissions to normal */
  2161. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
  2162. attrs->ia_mode &= ~(S_IALLUGO);
  2163. if (S_ISDIR(inode->i_mode))
  2164. attrs->ia_mode |=
  2165. cifs_sb->mnt_dir_mode;
  2166. else
  2167. attrs->ia_mode |=
  2168. cifs_sb->mnt_file_mode;
  2169. }
  2170. } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
  2171. /* ignore mode change - ATTR_READONLY hasn't changed */
  2172. attrs->ia_valid &= ~ATTR_MODE;
  2173. }
  2174. }
  2175. if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
  2176. ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
  2177. rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
  2178. /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
  2179. /* Even if error on time set, no sense failing the call if
  2180. the server would set the time to a reasonable value anyway,
  2181. and this check ensures that we are not being called from
  2182. sys_utimes in which case we ought to fail the call back to
  2183. the user when the server rejects the call */
  2184. if ((rc) && (attrs->ia_valid &
  2185. (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
  2186. rc = 0;
  2187. }
  2188. /* do not need local check to inode_check_ok since the server does
  2189. that */
  2190. if (rc)
  2191. goto cifs_setattr_exit;
  2192. if ((attrs->ia_valid & ATTR_SIZE) &&
  2193. attrs->ia_size != i_size_read(inode))
  2194. truncate_setsize(inode, attrs->ia_size);
  2195. setattr_copy(inode, attrs);
  2196. mark_inode_dirty(inode);
  2197. cifs_setattr_exit:
  2198. kfree(full_path);
  2199. free_xid(xid);
  2200. return rc;
  2201. }
  2202. int
  2203. cifs_setattr(struct dentry *direntry, struct iattr *attrs)
  2204. {
  2205. struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
  2206. struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
  2207. if (pTcon->unix_ext)
  2208. return cifs_setattr_unix(direntry, attrs);
  2209. return cifs_setattr_nounix(direntry, attrs);
  2210. /* BB: add cifs_setattr_legacy for really old servers */
  2211. }
  2212. #if 0
  2213. void cifs_delete_inode(struct inode *inode)
  2214. {
  2215. cifs_dbg(FYI, "In cifs_delete_inode, inode = 0x%p\n", inode);
  2216. /* may have to add back in if and when safe distributed caching of
  2217. directories added e.g. via FindNotify */
  2218. }
  2219. #endif